Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django 大型数据库中的高效计数查询?_Django_Postgresql - Fatal编程技术网

Django 大型数据库中的高效计数查询?

Django 大型数据库中的高效计数查询?,django,postgresql,Django,Postgresql,我有一个Django 1.7应用程序,带有Postgres 9.3后端。我的数据库中有大约4亿行的支出项目,每个项目都有一个相关的组织。模型如下: class Organisation(models.Model): code = models.CharField(max_length=9, primary_key=True, db_index=True) name = models.CharField(max_length=200) class SpendingItem(mod

我有一个Django 1.7应用程序,带有Postgres 9.3后端。我的数据库中有大约4亿行的支出项目,每个项目都有一个相关的组织。模型如下:

class Organisation(models.Model):
    code = models.CharField(max_length=9, primary_key=True, db_index=True)
    name = models.CharField(max_length=200)

class SpendingItem(models.Model):
    organisation = models.ForeignKey(Organisation)
    total_items = models.IntegerField() ... plus other info attached
我现在为每个组织编写一个视图,返回所附支出项目的总数。对于大型结果集,速度非常慢,例如,对于返回1m结果的查询,速度超过150秒

这是视图代码:

def organisation(request, org_code):
    org = get_object_or_404(Organisation, code=org_code)
    num_spending_items = SpendingItem.objects.filter(organisation=org).count()
    context = {
        'organisation': org,
        'num_spending_items': num_spending_items
    }
    return render(request, 'organisation.html', context)
我一直在阅读,但我想知道是否有什么简单的事情我可以做,例如使用索引来加速事情,或者我是否需要回到原始SQL并使用文档中提到的一些技术。或者预先生成结果并将其存储在其他位置

我的支出项目数据库表已经有以下索引,尽管我一直在用COPY语句加载数据,所以我不知道它们是否最新,也不知道我如何判断:

=# \d frontend_spendingitem;
                                       Table "public.frontend_spendingitem"
      Column       |          Type           |                             Modifiers
-------------------+-------------------------+--------------------------------------------------------------------
 id                | integer                 | not null default nextval('frontend_spendingitem_id_seq'::regclass)
 total_items       | integer                 | not null
 organisation_id   | character varying(9)    | not null
Indexes:
    "frontend_spendingitem_pkey" PRIMARY KEY, btree (id)
    "frontend_spendingitem_a69d813a" btree (organisation_id)
    "frontend_spendingitem_organisation_id_4619f68f65c49a8_like" btree (organisation_id varchar_pattern_ops)
Foreign-key constraints:
    "front_organisation_id_4619f68f65c49a8_fk_frontend_organisation_code" FOREIGN KEY (organisation_id) REFERENCES frontend_organisation(code) DEFERRABLE INITIALLY DEFERRED

您的索引总是最新的。您可能希望在批量加载后进行分析,以便规划人员了解数据的分布。如果需要性能帮助,您可能需要提供解释分析或至少是正在运行的查询。