在 Django 视图中使用 select_related 会增加查询时间 [英] Using select_related in django view increases query times
问题描述
我想尝试在 Django 视图中使用 select_realted 来提高性能.我比较了使用 select_realted 之前和使用之后的结果.
I thought of trying to use select_realted in Django view to boost the performance. And I compare the results before using select_realted and after using it.
虽然我看到大量查询减少了,但时间却在增加.所以我不确定是否在每个视图中使用 select_related 或不使用它们.
Although I see the reduction of significant number of queries, the time however rises. So I am not sure whether to use select_related in every view or not use them.
我只想知道什么时候用,什么时候不用.
I just want to know when to use them and when not to.
我的观点是:
之前:
class ProductAPIView(ListAPIView):
permission_classes = [AllowAny]
serializer_class = ProductSerializer
queryset = Product.objects.all()
之后:
class ProductAPIView(ListAPIView):
permission_classes = [AllowAny]
serializer_class = ProductSerializer
#queryset = Product.objects.all()
queryset = Product.objects.select_related('merchant','brand','collection','sub_category')
pagination_class = CustomPagination
我的模型:
class Product(models.Model):
merchant = models.ForeignKey(Seller,on_delete=models.CASCADE,blank=True,null=True)
category = models.ManyToManyField(Category, blank=False)
sub_category = models.ForeignKey(Subcategory, on_delete=models.CASCADE,blank=True,null=True)
brand = models.ForeignKey(Brand, on_delete=models.CASCADE)
collection = models.ForeignKey(Collection, on_delete=models.CASCADE)
featured = models.BooleanField(default=False) # is product featured?
select_related 使用前后的图片.
The images before and after the use of select_related.
之后:
这里我们可以看到查询次数减少到 124,但时间增加到 227.那么,我应该使用 select_related 吗?什么时候用,什么时候不用??
Here we can see that the number of queries are reduced to 124, but the time is increased to 227. So, should i be using select_related? When to use it or when not to use it??
推荐答案
Django中的select_related
其实就是SQL中的JOIN
.select_related
将仅在一个查询中获取您的相关字段以及您的对象.
例如,假设您有一个 Book Model 并且您有以下代码:
the select_related
in Django is actually JOIN
in SQL.
select_related
will fetch your related fields as well with your object in only one query.
for example imagine if you have a Book Model and you have this following code:
# Django will execute another query for getting author name
book = Book.objects.get(id=1)
author = book.author.name
# 2 queries executed
在这个例子中,你只执行了一个查询,但是为了获得 author.name
Django 将执行另一个查询.但是如果你使用 select_related
Django 也会在一个查询中得到作者.
In this example you have executed only one query but for getting the author.name
Django will execute another query. But if you use select_related
Django will get the author too in just one query.
# Django won't execute another query for getting author name
book = Book.objects.select_related('author').get(id=1)
author = book.author.name
# 1 query executed
请注意,如果您不需要它们,则无需在 select_related
中选择所有相关对象.
请查看此链接 在 stackoverflow 中了解 select_related
和 prefetch_related
之间的区别以及何时使用它们.
另请查看此链接以了解有关 n + 1 的更多信息查询问题.
Note that you don't need to select all of your related objects in select_related
if you don't need them.
Please check out this link in stackoverflow to know what's the differences between select_related
and prefetch_related
and when to use them.
also check out this link to know more about n + 1 query problems.
这篇关于在 Django 视图中使用 select_related 会增加查询时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!