在 Django 视图中使用 select_related 会增加查询时间

问题描述

我想尝试在 Django 视图中使用 select_realted 来提高性能。我比较了使用 select_realted 之前和使用之后的结果。

虽然我看到大量查询减少了,但时间却在增加。所以我不确定是否在每个视图中使用 select_related 或不使用它们。

我只想知道什么时候用,什么时候不用。

我的观点是:

之前:

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,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 使用前后的图片

enter image description here

之后:

enter image description here

这里我们可以看到查询次数减少到 124,但时间增加到 227。那么,我应该使用 select_related 吗?什么时候用,什么时候不用??

解决方法

Django 中的 select_related 实际上是 SQL 中的 JOIN
select_related 将仅在一个查询中获取您的相关字段以及您的对象。
例如,假设您有一个 Book Model 并且您有以下代码:

# 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 也会在一个查询中获得作者。

# 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 中的此 link 以了解 select_relatedprefetch_related 之间的区别以及何时使用它们。
另请查看此 link 以了解有关 n + 1 查询问题的更多信息。