Skip to content
Snippets Groups Projects
Commit b1192689 authored by Andreas Brandl's avatar Andreas Brandl
Browse files

Ditch LATERAL join approach.

Turns out this is not faster than a normal join with the example I had.

Also, LATERAL is not supported by all postgres versions.
parent b8a61ca5
No related branches found
No related tags found
Loading
Pipeline #
This commit is part of merge request !17454. Comments created here will be created in the context of that merge request.
......@@ -30,30 +30,16 @@ def execute
private
def recent_events(offset)
sql = if Gitlab::Database.join_lateral_supported?
lateral_join(offset)
else
normal_join
end
sql = <<~SQL
(#{projects}) AS projects_for_join
JOIN (#{target_events.to_sql}) AS #{Event.table_name}
ON #{Event.table_name}.project_id = projects_for_join.id
SQL
# Workaround for https://github.com/rails/rails/issues/24193
Event.from([Arel.sql(sql)])
end
def lateral_join(offset)
lateral = target_events
.where('events.project_id = projects_for_lateral.id')
.order('id DESC')
.limit(LIMIT + Integer(offset)) # Consider this is used in LATERAL JOIN and we paginate/offset the outer relation
.to_sql
"(#{projects}) AS projects_for_lateral JOIN LATERAL (#{lateral}) AS #{Event.table_name} ON true"
end
def normal_join
"(#{projects}) AS projects_for_join JOIN (#{target_events.to_sql}) AS #{Event.table_name} ON #{Event.table_name}.project_id = projects_for_join.id"
end
def target_events
Event.where(author: target_user)
end
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment