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

refactor: Issue #25040; Refactored sync_role_definition function in order to reduce number of query. #25340

Merged
merged 6 commits into from
Oct 11, 2023

Conversation

sandeep-patel11
Copy link
Contributor

SUMMARY

Earlier we were fetching PVMS one by one which made too many queries to database. So to avoid it I have added eagerload so that we can get all the data with single query.

Also for each role we were doing the same call so to avoid that I fetched all the data in main function and then passed it as parameter to set_role function.

TESTING INSTRUCTIONS

Inorder to test the functionality we need to add SQLALCHEMY_ECHO = True in config.py and then run superset init. Before this changes we will able to see too many queries getting logged. After refactoring number of queries have decreased.

ADDITIONAL INFORMATION

  • Has associated issue:
  • Required feature flags:
  • Changes UI
  • Includes DB Migration (follow approval process in SIP-59)
    • Migration is atomic, supports rollback & is backwards-compatible
    • Confirm DB migration upgrade and downgrade tested
    • Runtime estimates and downtime expectations provided
  • Introduces new feature or API
  • Removes existing feature or API

@sandeep-patel11 sandeep-patel11 changed the title Refactor : Refactored sync_role_definition function in order to reduce number of query. refactor: Issue #25040; Refactored sync_role_definition function in order to reduce number of query. Sep 19, 2023
@justinpark justinpark added the review:checkpoint Last PR reviewed during the daily review standup label Sep 19, 2023
Copy link
Contributor

@giftig giftig left a comment

Choose a reason for hiding this comment

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

Couple of small things + please run the pre-commit hook, I can see some formatting issues which black would fix up, which is probably why the build failed.

pvms = self.get_session.query(PermissionView).options(
eagerload(PermissionView.permission),
eagerload(PermissionView.view_menu)).all()
pvms = [p for p in pvms if p.permission and p.view_menu]
Copy link
Contributor

Choose a reason for hiding this comment

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

Does this check do anything? These are required relations, aren't they?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes this is required relation but as this condition was written in previous query I thought of not removing it.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah yeah, I see you haven't changed this. Fair enough.

@@ -864,7 +872,7 @@ def copy_role(
self.get_session.commit()

def set_role(
self, role_name: str, pvm_check: Callable[[PermissionView], bool]
self, role_name: str, pvm_check: Callable[[PermissionView], bool], pvms:[]
Copy link
Contributor

Choose a reason for hiding this comment

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

Type should be list[PermissionView] here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Correct. Changes Done.

@justinpark justinpark removed the review:checkpoint Last PR reviewed during the daily review standup label Sep 21, 2023
@pull-request-size pull-request-size bot added size/M and removed size/S labels Sep 25, 2023
eagerload(self.permissionview_model.view_menu),
)
.all()
):
Copy link
Contributor

Choose a reason for hiding this comment

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

One small thing, I see this is effectively same as the change in the other location, except here you're using self.permissionview_model and there you're using the name directly. I think using self.permissionview_model is the right way to go based on patterns I've seen elsewhere, but seems we probably need to stay DRY if we've already done it two different ways, and especially since the eagerload stuff is a bit fiddly.

Maybe just separate it into a def _get_all_pvms or similar? Gives us an opportunity to mention that we're doing it eagerly to avoid too many queries that way too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

Copy link
Contributor

@giftig giftig left a comment

Choose a reason for hiding this comment

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

One minor suggestion but it's quite pedantic so LGTM

)
.all()
)
pvms = [p for p in pvms if p.permission and p.view_menu]
Copy link
Contributor

Choose a reason for hiding this comment

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

pedantic comment but you could just return this line instead of updating the var and returning the var.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done, Thanks for the the reviews @giftig .

Copy link
Member

@villebro villebro left a comment

Choose a reason for hiding this comment

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

LGTM. Out of curiosity, which backend actions were most affected by this?

Edit: never mind, I just read the original issue where this is documented.

@villebro villebro requested a review from dpgaspar October 4, 2023 17:18
@rusackas rusackas merged commit eb9cd2a into apache:master Oct 11, 2023
29 checks passed
cccs-rc pushed a commit to CybercentreCanada/superset that referenced this pull request Mar 6, 2024
@mistercrunch mistercrunch added 🏷️ bot A label used by `supersetbot` to keep track of which PR where auto-tagged with release labels 🚢 3.1.0 labels Mar 8, 2024
sfirke pushed a commit to sfirke/superset that referenced this pull request Mar 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🏷️ bot A label used by `supersetbot` to keep track of which PR where auto-tagged with release labels size/M 🚢 3.1.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants