Skip to content
This repository has been archived by the owner on Mar 15, 2018. It is now read-only.

Drastically reduce the number of SQL queries made by the ratings API (bug 1135979) #2937

Merged
merged 1 commit into from Feb 25, 2015

Conversation

diox
Copy link
Member

@diox diox commented Feb 24, 2015

# FIXME: This is only right when ?app= filtering is applied, if no
# filtering or if ?user= filtering is done, it's completely wrong.
@property
def count(self):
Copy link
Member Author

Choose a reason for hiding this comment

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

count is used 4 times in django's paginator code. (The default paginator uses _count to calculate the results once). This means that at least 4 times, we loaded the whole object list, and 4 times, we loaded the related addon instance. Because the our models have transformers loading extra stuff, that's roughly 25 extra queries per review in the list.

# `addon` property on the instance with it, saving some costly queries.
app = getattr(self, 'app', None)
if app is not None:
obj.addon = app
Copy link
Member Author

Choose a reason for hiding this comment

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

Saves us ~ 16 queries per review.

@diox
Copy link
Member Author

diox commented Feb 24, 2015

We're saving a lot of queries there, this should improve performance quite a bit. We still need to check indexes, but that can be done separately.

I still need to test whether cache-machine will like the prefetch_related/select_related or not.

@diox diox force-pushed the ratings-api-perf branch 2 times, most recently from 0ea91be to 27db069 Compare February 24, 2015 21:11
self.app.update_version()

reset_queries()
with self.assertNumQueries(5):
Copy link
Member Author

Choose a reason for hiding this comment

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

When I started, this test did more than a hundred queries.

@robhudson
Copy link
Member

I read it over a few times. r+

@diox
Copy link
Member Author

diox commented Feb 25, 2015

Checked with cache-machine enabled and it seems to be doing the right thing.

diox added a commit that referenced this pull request Feb 25, 2015
Drastically reduce the number of SQL queries made by the ratings API (bug 1135979)
@diox diox merged commit 502d760 into mozilla:master Feb 25, 2015
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
2 participants