【问题标题】:Using select_related in django view increases query times在 django 视图中使用 select_related 会增加查询时间
【发布时间】:2021-08-23 21:58:51
【问题描述】:

我想过尝试在 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, 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 使用前后的图片。

之后:

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

【问题讨论】:

标签: django api django-rest-framework django-views django-select-related


【解决方案1】:

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 查询问题的更多信息。

【讨论】:

    猜你喜欢
    • 2017-05-25
    • 2021-12-16
    • 1970-01-01
    • 2014-08-11
    • 2013-09-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多