Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify roles query for performance #2532

Merged
merged 1 commit into from
Oct 5, 2021

Conversation

andy-paine
Copy link
Contributor

@andy-paine andy-paine commented Oct 5, 2021

A short explanation of the proposed change:

Replace UNION-ed subqueries with single subquery and WHERE OR clause

An explanation of the use cases your change solves

The many UNIONs (14 in total) were causing these queries to be extremely complex and slow on our largest landscapes resulting in impact to other queries being made (lots of temp blocks being written). This change simplifies the query so much it no longer needs temp blocks.

Added context

The backing dataset for the Role class uses a UNION between all the
different org and space roles where the organization_id is -1 for space
roles and vice versa. This means that no role can have both a space_id
and an org_id so doing a SELECT WHERE space_id IN (space_ids) and UNION
that to SELECT WHERE org_id in (org_ids) can be replaced by just a
single select with an OR in the query clause

In our largest environments this reduced the complexity of this query
(using EXPLAIN ANALYZE) by nearly 30x

  • I have reviewed the contributing guide

  • I have viewed, signed, and submitted the Contributor License Agreement

  • I have made this pull request to the main branch

  • I have run all the unit tests using bundle exec rake

  • I have run CF Acceptance Tests

The backing dataset for the Role class uses a UNION between all the
different org and space roles where the organization_id is -1 for space
roles and vice versa. This means that no role can have both a space_id
and an org_id so doing a SELECT WHERE space_id IN (space_ids) and UNION
that to SELECT WHERE org_id in (org_ids) can be replaced by just a
single select with an OR in the query clause

In our largest environments this reduced the complexity of this query
(using EXPLAIN ANALYZE) by nearly 30x

Co-Authored-By: Andy Paine <[email protected]>
Co-Authored-By: Johannes Haass <[email protected]>
@MarcPaquette MarcPaquette self-assigned this Oct 5, 2021
@MarcPaquette
Copy link
Contributor

Ran CATS on PR. Hit the endpoint a few different ways. Examined CCNG's logs. Everything looks good!

@MarcPaquette MarcPaquette merged commit 7ff419b into cloudfoundry:main Oct 5, 2021
@andy-paine andy-paine deleted the readable-roles branch October 6, 2021 07:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants