Skip to content

RUN-AS-IF-FOSS: SQL set operators INTERSECT and EXCEPT

Alex Pooley requested to merge from-intersect into master

What does this MR do?

Provides the ability to SQL INTERSECT and EXCEPT like we already have with UNION.

The initial use case is to improve performance on queries that depend on the overlap of multiple namespace hierarchies.

For example, to improve the use case of:

hierarchies1 = query_with_lots_of_groups
hierarchies2 = query_with_lots_of_groups
hierarchies1.where(id: hierarchies2)

The INTERSECT operator improved query performance from 1 minute down to 2.9 seconds!

There were significant dependencies on existing derivatives of Gitlab::SQL::Union, FromUnion, #from_union class and method names which I did not want to change. I ended up having to meta-program in app/models/concerns/from_set_operator.rb which I could not avoid despite my efforts to keep things simple. FWIW I did try a few other methods first before I settled on this. I am happy to hear of a more simple solution.

Screenshots

Does this MR meet the acceptance criteria?

Conformity

Availability and Testing

Security

If this MR contains changes to processing or storing of credentials or tokens, authorization and authentication methods and other items described in the security review guidelines:

  • Label as security and @ mention @gitlab-com/gl-security/appsec
  • The MR includes necessary changes to maintain consistency between UI, API, email, or other methods
  • Security reports checked/validated by a reviewer from the AppSec team
Edited by Alex Pooley

Merge request reports