divio/django-cms — pull request #1362 by piquadrat (2012-07-26 07:53:00+00:00)
Title: Reduce SQL queries for page views

This pull request contains a number of optimizations that should reduce the amount of SQL queries a CMS page generates

7bbe551 makes the ORM generate a much simpler query and avoids turning the query into page objects
b51bd74 and ec58439 avoid querying for the placeholder when the information is already there. This roughly reduces the total number of queries by one per plugin (because the particular query is cheap and should be cached by the database, the real gain in milliseconds isn't that great, but we take what we can get, right?)

5e1de3e is the big one. It changes the way plugins are loaded completely. At the moment, every placeholder queries for its own set of ``CMSPlugins``s, and casts them down to the correct subclass one by one. With the proposed changes, all ``CMSPlugin``s of a page would be fetched in one query, and then cast down in one query per plugin type. This helps a lot for pages with more than one placeholder, and a relatively homogeneous set of plugins. The worst case is a page with one placeholder, containing many different plugins, and no plugins having the same type. In this case, the amount of queries is the same as it is in 2.3.

With this PR applied, I saw reductions in the number of SQL queries anywhere between 10-60%, depending on the complexity and diversity of the page and template.

Please don't merge this quite yet, tests are forthcoming.