Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Page.get_object_queryset use of short-circuited and/or causes extraneous query-set evaluations #1671

SpComb opened this Issue Mar 23, 2013 · 2 comments


None yet
2 participants

SpComb commented Mar 23, 2013

The following logic in cms.models.pagemodel.Page.get_object_queryset():

return self.publisher_is_draft and qs.drafts() or qs.public()

Seems to cause extraneous queryset evaluations:

Function                                                                                   called...
                                                                                               ncalls  tottime  cumtime
/srv/web/www/lib/python2.6/site-packages/cms/models/pagemodel.py:914(get_object_queryset)  ->      42    0.001    0.062  /srv/web/www/lib/python2.6/site-packages/cms/models/managers.py:23(drafts)
                                                                                                   42    0.000    0.000  /srv/web/www/lib/python2.6/site-packages/django/db/models/manager.py:217(__get__)
                                                                                                   42    0.000    1.207  /srv/web/www/lib/python2.6/site-packages/django/db/models/query.py:120(__nonzero__)

i.e. full SELECT ... FROM cms_page INNER JOIN django_site queries.

Replacing the short-circuited logic with a ternary expression avoids the implict __nonzero__ on qs.drafts() and extraneous SELECT query (reducing page load time significantly):

return qs.drafts() if self.publisher_is_draft else qs.public()

Note that the Page.get_home_pk_cache() slowness in Issue #1623 seems to be largely exacerbated by this issue:

                                                                                             ncalls  tottime  cumtime
/srv/web/www/lib/python2.6/site-packages/cms/models/pagemodel.py:816(get_home_pk_cache)  ->      42    0.001    1.270  /srv/web/www/lib/python2.6/site-packages/cms/models/pagemodel.py:914(get_object_queryset)
                                                                                                 42    0.002    0.366  /srv/web/www/lib/python2.6/site-packages/cms/models/query.py:95(get_home)
                                                                                                 42    0.000    0.000  /srv/web/www/lib/python2.6/site-packages/django/db/models/base.py:425(_get_pk_val)
                                                                                                 42    0.002    0.128  /srv/web/www/lib/python2.6/site-packages/django/db/models/fields/related.py:331(__get__)
                                                                                                 56    0.000    0.000  {getattr}
                                                                                                 56    0.000    0.000  {hasattr}
                                                                                                 42    0.000    0.000  {setattr}

digi604 commented Jul 11, 2013

Wow... nice catch. Could you open a pull request?


digi604 commented Feb 10, 2014

this is fixed in develop as is_home is now saved on the page.

@digi604 digi604 closed this Feb 10, 2014

yakky added a commit that referenced this issue Jul 23, 2014

Merge pull request #3281 from laurafeier/issue_1671
Performance improvement for get_absolute_url (#1623, #1671)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment