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

Reduce the number of queries used in tagging API [FC-00036] #157

Merged
merged 8 commits into from
Feb 15, 2024

Conversation

pomegranited
Copy link
Contributor

@pomegranited pomegranited commented Feb 8, 2024

Description

Updates the base tagging library to reduce database queries when using the views or models.

Supporting information

Closes openedx/modular-learning#185

Internal-ref: FAL-3646

Testing instructions

The changes made by this PR are validated by the reduced query counts in the tests.

TO DO BEFORE MERGE:

  • update release version: 0.6.2

@openedx-webhooks openedx-webhooks added the open-source-contribution PR author is not from Axim or 2U label Feb 8, 2024
@openedx-webhooks
Copy link

Thanks for the pull request, @pomegranited! Please note that it may take us up to several weeks or months to complete a review and merge your PR.

Feel free to add as much of the following information to the ticket as you can:

  • supporting documentation
  • Open edX discussion forum threads
  • timeline information ("this must be merged by XX date", and why that is)
  • partner information ("this is a course on edx.org")
  • any other information that can help Product understand the context for the PR

All technical communication about the code itself will be done via the GitHub pull request interface. As a reminder, our process documentation is here.

Please let us know once your PR is ready for our review and all tests are green.

Overrides the permissions.has_permissions method to avoid using
view.get_queryset(), since that method accesses the database.
instead of prefetching all the tags.
"""
Returns True if the request user is allowed the given view on the Taxonomy model.

We override this method to avoid calling our view's get_queryset(), which performs database queries.
Copy link
Contributor

Choose a reason for hiding this comment

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

Just out of curiosity, do you know why this is happening?

I didn't find a place where we are actually evaluating the query. Were the queries from the Taxonomy model or some other tables?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's being called by Django Rest Framework: APIView.initial calls DjangoModelPermissions.has_permission()

Copy link
Member

@yusuf-musleh yusuf-musleh left a comment

Choose a reason for hiding this comment

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

👍 Great work here @pomegranited ! Just had a question.

  • I tested this: Checked that the updated tests were passing
  • I read through the code
  • I checked for accessibility issues
  • Includes documentation
  • I made sure any change in configuration variables is reflected in the corresponding client's configuration-secure repository.

Comment on lines +108 to +109
if hasattr(instance, 'tags_count'):
return instance.tags_count
Copy link
Member

Choose a reason for hiding this comment

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

Did this help reduce the query count? The reason I ask is because on the commit that this was added in, the query counts in the tests did not change, so I was just wondering what effect this may have had.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The query counts dropped here when this branch was used: openedx/edx-platform@016f2a5

if hasattr(taxonomy, '_prefetched_objects_cache'):
# pylint: disable=protected-access,attribute-defined-outside-init
self._prefetched_objects_cache: dict = taxonomy._prefetched_objects_cache

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note: I used code from this blog to make this change. That blog post also copies the caches for one-to-many fields that were cached using select_related, but since Taxonomy doesn't have any one-to-many fields, I omitted that part.

"""
Returns True if the request user is allowed the given view on the Taxonomy model.

We override this method to avoid calling our view's get_queryset(), which performs database queries.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's being called by Django Rest Framework: APIView.initial calls DjangoModelPermissions.has_permission()

Copy link
Contributor

@bradenmacdonald bradenmacdonald left a comment

Choose a reason for hiding this comment

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

Looks great, thanks!

and renames the permission class to better represent the view it guards.
@pomegranited pomegranited merged commit f3779de into openedx:main Feb 15, 2024
7 checks passed
@openedx-webhooks
Copy link

@pomegranited 🎉 Your pull request was merged! Please take a moment to answer a two question survey so we can improve your experience in the future.

@bradenmacdonald bradenmacdonald deleted the jill/tagging-less-queries branch February 15, 2024 18:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
open-source-contribution PR author is not from Axim or 2U
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

[Tagging] Reduce the number of queries used in content tagging REST API
5 participants