select_related以下两种情况下的性能差异是什么
What is the performance difference in the following two cases of select_related
我有以下两种情况。在这两个中,我无法判断哪个是最好的命令。它们中的任何一个都存在性能问题吗?
案例一
Model.objects.filter(item1=4,item2=5).select_related('store').get(id=1)
案例二
Model.objects.select_related('store').filter(item1=4,item2=5).get(id=1)
您可以通过查看 connections.queries
list 检查 Django 进行的查询。所以你可以打印最后一个查询:
>>> from django.db import connection
>>> print(connection.queries[-1]['sql'])
对于第一个查询,我们得到:
>>> Model.objects.filter(item1=4,item2=5).select_related('store').get(id=1)
<Model: Model object (1)>
>>> print(connection.queries[-1]['sql'])
SELECT "app_name_model"."id", "app_name_model"."store_id", "app_name_model"."item1", "app_name_model"."item2", "app_name_store"."id" FROM "app_name_model" INNER JOIN "app_name_store" ON ("app_name_model"."store_id" = "app_name_store"."id") WHERE ("app_name_model"."item1" = 4 AND "app_name_model"."item2" = 5 AND "app_name_model"."id" = 1) LIMIT 21
而对于后者,它是:
>>> Model.objects.select_related('store').filter(item1=4,item2=5).get(id=1)
<Model: Model object (1)>
>>> print(connection.queries[-1]['sql'])
SELECT "app_name_model"."id", "app_name_model"."store_id", "app_name_model"."item1", "app_name_model"."item2", "app_name_store"."id" FROM "app_name_model" INNER JOIN "app_name_store" ON ("app_name_model"."store_id" = "app_name_store"."id") WHERE ("app_name_model"."item1" = 4 AND "app_name_model"."item2" = 5 AND "app_name_model"."id" = 1) LIMIT 21
两者因此完全产生相同的查询:
SELECT "app_name_model"."id", "app_name_model"."store_id", "app_name_model"."item1", "app_name_model"."item2",
"app_name_store"."id"
FROM "app_name_model"
INNER JOIN "app_name_store" ON ("app_name_model"."store_id" = "app_name_store"."id")
WHERE ("app_name_model"."item1" = 4 AND "app_name_model"."item2" = 5 AND "app_name_model"."id" = 1)
其中粗体部分是.select_related(…)
call [Django-doc]的效果。
我有以下两种情况。在这两个中,我无法判断哪个是最好的命令。它们中的任何一个都存在性能问题吗?
案例一
Model.objects.filter(item1=4,item2=5).select_related('store').get(id=1)
案例二
Model.objects.select_related('store').filter(item1=4,item2=5).get(id=1)
您可以通过查看 connections.queries
list 检查 Django 进行的查询。所以你可以打印最后一个查询:
>>> from django.db import connection
>>> print(connection.queries[-1]['sql'])
对于第一个查询,我们得到:
>>> Model.objects.filter(item1=4,item2=5).select_related('store').get(id=1)
<Model: Model object (1)>
>>> print(connection.queries[-1]['sql'])
SELECT "app_name_model"."id", "app_name_model"."store_id", "app_name_model"."item1", "app_name_model"."item2", "app_name_store"."id" FROM "app_name_model" INNER JOIN "app_name_store" ON ("app_name_model"."store_id" = "app_name_store"."id") WHERE ("app_name_model"."item1" = 4 AND "app_name_model"."item2" = 5 AND "app_name_model"."id" = 1) LIMIT 21
而对于后者,它是:
>>> Model.objects.select_related('store').filter(item1=4,item2=5).get(id=1)
<Model: Model object (1)>
>>> print(connection.queries[-1]['sql'])
SELECT "app_name_model"."id", "app_name_model"."store_id", "app_name_model"."item1", "app_name_model"."item2", "app_name_store"."id" FROM "app_name_model" INNER JOIN "app_name_store" ON ("app_name_model"."store_id" = "app_name_store"."id") WHERE ("app_name_model"."item1" = 4 AND "app_name_model"."item2" = 5 AND "app_name_model"."id" = 1) LIMIT 21
两者因此完全产生相同的查询:
SELECT "app_name_model"."id", "app_name_model"."store_id", "app_name_model"."item1", "app_name_model"."item2", "app_name_store"."id" FROM "app_name_model" INNER JOIN "app_name_store" ON ("app_name_model"."store_id" = "app_name_store"."id") WHERE ("app_name_model"."item1" = 4 AND "app_name_model"."item2" = 5 AND "app_name_model"."id" = 1)
其中粗体部分是.select_related(…)
call [Django-doc]的效果。