我正在开发一个具有 MySQL innodb 后端的 django 网站。我们的几个表中有数十万条记录,这导致管理员出现一些站点稳定性/性能问题。具体来说,django 喜欢在创建分页器时进行 count(*) 查询,这会导致很多问题。
在 Django 1.3.x 中,他们开始允许提供自定义分页类。因此,我有兴趣找到一种方法来适当加速或消除这些查询。到目前为止,我一直在查看这两页:http://code.google.com/p/django-pagination/source/browse/trunk/pagination/paginator.py http://code.google.com/p/django-pagination/source/browse/trunk/pagination/paginator.py
https://gist.github.com/1094682 https://gist.github.com/1094682并没有真正发现它们就是我正在寻找的东西。任何建议、帮助等。将不胜感激。
您可以在分页器中定义 _count 变量
paginator = Paginator(QuerySet, 300)
paginator._count = 9000 # or use some query here
这是 django 分页器代码的一部分,可帮助您了解此变量的作用以及页数计数的工作原理
def _get_count(self):
"Returns the total number of objects, across all pages."
if self._count is None:
try:
self._count = self.object_list.count()
except (AttributeError, TypeError):
# AttributeError if object_list has no count() method.
# TypeError if object_list.count() requires arguments
# (i.e. is of type list).
self._count = len(self.object_list)
return self._count
count = property(_get_count)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)