Skip to content

Replace direct use of Gitlab::SQL::Union with a "from_union" method

Yorick Peterse requested to merge select-from-union into master

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

What are the relevant issue numbers?

https://gitlab.com/gitlab-org/gitlab-ce/issues/51307

Does this MR meet the acceptance criteria?

Edited by Yorick Peterse

Merge request reports