Skip to content

Replace .include? / .map with more efficient SQL statements#3041

Draft
philippthun wants to merge 1 commit intocloudfoundry:mainfrom
sap-contributions:efficient-include-map
Draft

Replace .include? / .map with more efficient SQL statements#3041
philippthun wants to merge 1 commit intocloudfoundry:mainfrom
sap-contributions:efficient-include-map

Conversation

@philippthun
Copy link
Member

Thanks for contributing to cloud_controller_ng. To speed up the process of reviewing your pull request please provide us with:

  • A short explanation of the proposed change:

  • An explanation of the use cases your change solves

  • Links to any other associated PRs

  • 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

!context.user.nil? && (
(context.user.managed_organizations.include? space_quota_definition.organization) ||
context.user.managed_organizations_dataset.where(id: space_quota_definition.organization_id).any? ||
!(context.user.managed_spaces & space_quota_definition.spaces).empty? ||
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is definitely an improvement, but you can go further and cut out organizations table by doing something like

OrganizationManager.where(user_id: context.user.id, organization_id: space_quota_definition.organization.id).any?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

managed organizations_dataset produces this query:

SELECT  "organizations".*
FROM    "organizations"
       INNER JOIN  "organizations_managers"
               ON (  "organizations_managers" . "organization_id" = 
                    "organizations" . "id" )
WHERE  (  "organizations_managers" . "user_id" = 118 ) 

space.has_member?(@user) || space.has_supporter?(@user) ||
@user.managed_organizations.map(&:id).include?(space.organization_id) ||
@user.audited_organizations.map(&:id).include?(space.organization_id)
@user.managed_organizations_dataset.where(id: space.organization_id).any? ||
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as my first comment - the OrganizationManager has user_id and organization_id columns, so if you use that you can cut organizations out entirely.


def validate_organization(org)
unless org && organizations.include?(org)
unless org && organizations_dataset.where(id: org).any?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I might be misunderstanding something here - and this logic already existed before you touched it - but is it actually possible to have a situation where org is true and organizations_dataset.where(id: org).any? is false?

visible_spaces_dataset = if @all_spaces_visible
space_quota.spaces_dataset
else
space_quota.spaces_dataset.where(guid: @visible_space_guids)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ultimately @visible_space_guids comes from permissions.readable_space_guids but maybe we should instead that should be permissions.readable_space_guids_query (though we changed that recently to raise an error if the user reads globally..)

@philippthun philippthun force-pushed the efficient-include-map branch 3 times, most recently from 2a885c1 to 6f0f056 Compare February 14, 2023 13:55
@philippthun philippthun force-pushed the efficient-include-map branch from 6f0f056 to 77955dd Compare February 5, 2024 17:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants