[英]Django Models - SELECT DISTINCT(foo) FROM table is too slow
I have a MySQL table with 13M rows. 我有一个具有13M行的MySQL表。 I can query the db directly as
我可以直接查询数据库为
SELECT DISTINCT(refdate) FROM myTable
The query takes 0.15 seconds and is great. 该查询需要0.15秒,非常棒。
The equivalent table defined as a Django model and queried as 等效表定义为Django模型并查询为
myTable.objects.values(`refdate`).distinct()
takes a very long time. 需要很长时间。 Is it because there are too many items in the list before
distinct()
. 是因为在list
distinct()
之前列表中的项目太多。 How do I do this in a manner that doesn't bring everything down? 如何以一种不会降低一切的方式做到这一点?
Thank you @solarissmoke for the pointer to connection.queries
. 谢谢@solarissmoke提供的指向
connection.queries
的指针。
I was expecting to see 我期待看到
SELECT DISTINCT refdate FROM myTable
Instead, I got 相反,我得到了
SELECT DISTINCT refdate, itemIndex, itemType FROM myTable ORDER BY itemIndex, refdate, itemType.
I then looked at myTable defined in models.py
. 然后我看着在定义myTable的
models.py
。
unique_together = (('nodeIndex', 'refdate', 'nodeType'), )
ordering = ['nodeIndex', 'refdate', 'nodeType']
From Interaction with default ordering or order_by 通过默认顺序或order_by进行交互
normally you won't want extra columns playing a part in the result, so clear out the ordering, or at least make sure it's restricted only to those fields you also select in a values() call.
通常,您不希望多余的列在结果中起作用,因此请清除顺序,或者至少确保它仅限于您在values()调用中选择的那些字段。
So I tried order_by()
to flush the previously defined ordering and voila! 因此,我尝试使用
order_by()
刷新先前定义的顺序,瞧!
myTable.objects.values('refdate').order_by().distinct()
您可以尝试以下方法:
myTable.objects.all().distinct('refdate')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.