Replace direct use of Gitlab::SQL::Union with a "from_union" method
What does this MR do?
This MR adds the module FromUnion
, which provides the class method
from_union
. This simplifies the process of selecting data from the result of a
UNION, and reduces the likelihood of making mistakes. As a result, instead of
this:
union = Gitlab::SQL::Union.new([foo, bar])
Foo.from("(#{union.to_sql}) #{Foo.table_name}")
We can now write this instead:
Foo.from_union([foo, bar])
This MR also includes some changes to make this new setup work properly. For
example, a bug in Rails 4 (https://github.com/rails/rails/issues/24193) would
break the use of from("sub-query-here").includes(:relation)
in certain cases.
There was also a CI query which appeared to repeat a lot of conditions from an
outer query on an inner query, which isn't necessary.
These changes will likely also improve performance of various queries. For example,
MergeRequest.in_projects
used to use WHERE IN
for filtering, but now uses a UNION
. This new approach
can be up to 11 times faster than the WHERE IN
approach.
EE MR: https://gitlab.com/gitlab-org/gitlab-ee/merge_requests/7384
TODO
-
Rebase with master once https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/21391 is merged -
Add tests for FromUnion
What are the relevant issue numbers?
https://gitlab.com/gitlab-org/gitlab-ce/issues/51307
Does this MR meet the acceptance criteria?
-
Documentation created/updatedRuboCop cop added instead -
Tests added for this feature/bug -
Conforms to the code review guidelines -
Conforms to the merge request performance guidelines -
Conforms to the style guides -
Conforms to the database guides