如何实现不调用count(*)的分页器

编程入门 行业动态 更新时间:2024-10-27 19:16:11
本文介绍了如何实现不调用count(*)的分页器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在django网站上工作,该网站具有MySQL innodb后端.我们的几个表中都有成千上万的记录,这在管理员中引起了一些站点稳定性/性能问题.具体来说,django喜欢在创建分页程序时进行count(*)查询,这会引起很多问题.

I am working on a django website that has a MySQL innodb backend. We have hundreds of thousands of records in several of our tables and this is causing some site stability/performance issues in the admin. Specifically, django likes to make count(*) queries when creating the paginators, and this is causing lots of problems.

使用Django 1.3.x,他们开始允许提供自定义的分页类.因此,我对寻找一种适当地加快或消除这些查询的方法感兴趣.到目前为止,我一直在看这两页: code.google/p/django-pagination/source/browse/trunk/pagination/paginator.py gist.github/1094682 并没有真正找到他们正是我要找的东西.任何建议,帮助等.将不胜感激.

With Django 1.3.x, they started to allow for custom pagination classes to be provided. So, I'm interested in finding a way to appropriately speed up or eliminate these queries. So far, I've been looking at these two pages: code.google/p/django-pagination/source/browse/trunk/pagination/paginator.py gist.github/1094682 and have not really found them to be what I'm looking for. Any suggestions, help, ect. would be much appreciated.

推荐答案

您可以在分页器中定义_count变量

You can define _count variable in your paginator

paginator = Paginator(QuerySet, 300) paginator._count = 9000 # or use some query here

这是Django分页器代码的一部分,可帮助您了解此变量的作用以及页数的工作原理

And here is the part of django paginator code to help you understand what this variable do and how page count works

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)

更多推荐

如何实现不调用count(*)的分页器

本文发布于:2023-10-15 12:14:38,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1494305.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:分页   如何实现   count

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!