我正在开发一个拥有MySQL innodb后端的django网站。我们在几个表中有数十万条记录,这导致管理员出现一些站点稳定性/性能问题。具体来说,django喜欢在创建分页器时进行count(*)查询,这会导致很多问题。
使用Django 1.3.x,他们开始允许提供自定义分页类。所以,我有兴趣找到一种方法来适当加快或消除这些查询。到目前为止,我一直在查看这两页:http://code.google.com/p/django-pagination/source/browse/trunk/pagination/paginator.py https://gist.github.com/1094682 并没有真正发现它们是我正在寻找的。任何建议,帮助等。非常感谢。
答案 0 :(得分:10)
您可以在paginator中定义_count变量
paginator = Paginator(QuerySet, 300)
paginator._count = 9000 # or use some query here
以下是django paginator代码的一部分,可帮助您了解此变量的作用以及页数如何工作
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)
答案 1 :(得分:0)
您还可以查看django-endless-pagination。endless_pagination.paginator.LazyPaginator
也不错,但您可能需要添加一些调整。