{"id":5639,"date":"2014-06-30T16:12:36","date_gmt":"2014-06-30T08:12:36","guid":{"rendered":"https:\/\/kyle.ai\/blog\/?p=5639"},"modified":"2014-06-30T16:12:36","modified_gmt":"2014-06-30T08:12:36","slug":"%e4%bc%98%e5%8c%96django%e7%ae%a1%e7%90%86%e5%90%8e%e5%8f%b0%e7%9a%84select-count%e6%9f%a5%e8%af%a2","status":"publish","type":"post","link":"https:\/\/kyle.ai\/blog\/5639.html","title":{"rendered":"\u4f18\u5316Django\u7ba1\u7406\u540e\u53f0\u7684SELECT COUNT(*)\u67e5\u8be2"},"content":{"rendered":"<p>Django\u7ba1\u7406\u540e\u53f0\u6253\u5f00\u754c\u9762\u540e\uff0c\u4f1a\u5728\u4e0a\u9762\u9ed8\u8ba4\u663e\u793a\u51fa\u201c\u603b\u5171 515657\u201d\u8fd9\u6837\u7684\u603b\u6570\u636e\u6761\u6570\uff0c\u8981\u67e5\u8be2\u51fa\u8fd9\u4e2a\u6570\u5b57\uff0cdjango\u5c31\u4f1a\u5bf9\u8868\u8fdb\u884c select count(*) from table \u8fd9\u6837\u7684\u67e5\u8be2\uff0c\u800c\u5bf9\u4e8e\u6570\u636e\u91cf\u6bd4\u8f83\u5927\u7684InnoDB\u8868\u6765\u8bf4\uff0c\u8fd9\u4e2a\u67e5\u8be2\u8bed\u53e5\u4f1a\u6bd4\u8f83\u6162\uff0c\u81f3\u5c11\u5f97\u8981\u597d\u51e0\u79d2\u3002<\/p>\n<p>\u4e0b\u9762\u8fd9\u4e2a\u529e\u6cd5\u53ef\u4ee5\u89e3\u51b3\uff0c\u5f53\u53d1\u73b0\u4e0d\u5e26\u4efb\u4f55\u6761\u4ef6\u7684count\u67e5\u8be2\u65f6\uff0c\u5c31\u7528 show table status \u8bed\u53e5\u4e2d\u7684innodb\u7684\u4f30\u8ba1\u503c\u66ff\u4ee3\u3002\u65b9\u6cd5\u6765\u81ea\u4e8e stackoverflow\uff1a<br \/>\nhttp:\/\/stackoverflow.com\/questions\/10433173\/prevent-django-admin-from-running-select-count-on-the-list-form<\/p>\n<p>\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nclass ApproxCountQuerySet(QuerySet):\r\n    &quot;&quot;&quot;Counting all rows is very expensive on large Innodb tables. This\r\n    is a replacement for QuerySet that returns an approximation if count()\r\n    is called with no additional constraints. In all other cases it should\r\n    behave exactly as QuerySet.\r\n\r\n    Only works with MySQL. Behaves normally for all other engines.\r\n    &quot;&quot;&quot;\r\n\r\n    def count(self):\r\n        # Code from django\/db\/models\/query.py\r\n\r\n        if self._result_cache is not None and not self._iter:\r\n            return len(self._result_cache)\r\n\r\n        is_mysql = 'mysql' in connections&#x5B;self.db].client.executable_name.lower()\r\n\r\n        query = self.query\r\n        if (is_mysql and not query.where and\r\n                query.high_mark is None and\r\n                query.low_mark == 0 and\r\n                not query.select and\r\n                not query.group_by and\r\n                not query.having and\r\n                not query.distinct):\r\n            # If query has no constraints, we would be simply doing\r\n            # &quot;SELECT COUNT(*) FROM foo&quot;. Monkey patch so the we\r\n            # get an approximation instead.\r\n            cursor = connections&#x5B;self.db].cursor()\r\n            cursor.execute(&quot;SHOW TABLE STATUS LIKE %s&quot;,\r\n                    (self.model._meta.db_table,))\r\n            return cursor.fetchall()&#x5B;0]&#x5B;4]\r\n        else:\r\n            return self.query.get_count(using=self.db)\r\n<\/pre>\n<p>\u7136\u540e\u60f3\u529e\u6cd5\u91cd\u8f7dDjango\u539f\u751f\u7684QuerySet\u5c31\u597d\u4e86<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nclass ModelManager(models.Manager):\r\n\r\n    def get_query_set(self):\r\n        return CustomQuerySet(self.model, using=self._db)\r\n\r\n\u7136\u540e \r\n\r\nclass BaseModel(models.Model,):\r\n    objects = manager.ModelManager()\r\n\r\nmodels.Model = BaseModel\r\n<\/pre>\n<p>\u8fd9\u6837\u4e00\u6765\uff0c\u51e1\u662fdjango\u4e2d\u7684\u4e0d\u5e26\u6761\u4ef6count\u67e5\u8be2\u51fa\u6765\u7684\u503c\u90fd\u4f1a\u662f\u4e2a\u9884\u4f30\u503c\uff0c\u4f1a\u4e0d\u51c6\uff0c\u800c\u4e14\u6bcf\u6b21\u67e5\u8be2\u90fd\u4f1a\u53d8\u3002\u5982\u679c\u4f60\u60f3\u67e5\u4e00\u4e2a\u51c6\u786e\u503c\uff0c\u5c31\u5e26\u4e2a\u660e\u663e\u6210\u7acb\u7684\u6761\u4ef6\u597d\u4e86\uff0c\u6bd4\u5982id\u5927\u4e8e0\uff1a<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nIn &#x5B;4]: User.objects.all().count()\r\nOut&#x5B;4]: 519110L\r\n\r\nIn &#x5B;5]: User.objects.all().count()\r\nOut&#x5B;5]: 459258L\r\n\r\nIn &#x5B;6]: User.objects.all().count()\r\nOut&#x5B;6]: 454654L\r\n\r\nIn &#x5B;7]: User.objects.filter(id__gt=0).count()\r\nOut&#x5B;7]: 511441\r\n\r\nIn &#x5B;8]: User.objects.filter(id__gt=0).count()\r\nOut&#x5B;8]: 511441\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Django\u7ba1\u7406\u540e\u53f0\u6253\u5f00\u754c\u9762\u540e\uff0c\u4f1a\u5728\u4e0a\u9762\u9ed8\u8ba4\u663e\u793a\u51fa\u201c\u603b\u5171 515657\u201d\u8fd9\u6837\u7684\u603b\u6570\u636e\u6761\u6570\uff0c\u8981\u67e5\u8be2\u51fa\u8fd9\u4e2a\u6570\u5b57\uff0cd [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-5639","post","type-post","status-publish","format-standard","hentry","category-diary"],"_links":{"self":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/5639","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/comments?post=5639"}],"version-history":[{"count":1,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/5639\/revisions"}],"predecessor-version":[{"id":5640,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/posts\/5639\/revisions\/5640"}],"wp:attachment":[{"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/media?parent=5639"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/categories?post=5639"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kyle.ai\/blog\/wp-json\/wp\/v2\/tags?post=5639"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}