Django SQL选择日志——为什么我看到这么多日志?

时间:2021-04-06 22:01:46

I'm trying to understand how many SQL select queries each request on my app requires (Django+Tastypie). I changed the logging configuration so I can see the relevant logs. For some reason I see a vast number of these logs. For example, for simple get_list - I see ~100 select lines. When I tried to debug, to see where they all come from, I couldn't step into the lines that generate these logs. I also noticed the numbers on the left are very low (usually 0.001). I assume that this number is time to perform the query (in sec).

我试图理解我的应用程序上的每个请求需要多少SQL select查询(Django+无味派)。我更改了日志配置,以便查看相关日志。出于某种原因,我看到了大量这样的日志。例如,对于简单的get_list—我看到了~100个选择行。当我尝试调试时,为了查看它们都来自哪里,我无法进入生成这些日志的行。我还注意到左边的数字很低(通常是0.001)。我假设这个数字是执行查询的时间(秒)。

Any idea what's the explanation for all these lines?

你知道这些线的解释是什么吗?

1 个解决方案

#1


1  

In your tastypie api.py file you probably need to change the default queryset to either include prefetch_related or select_related. Which you use depends on your actual model. Here's an example:

在你tastypie api。py文件可能需要将默认的queryset更改为包含prefetch_related或select_related。你使用什么取决于你的实际模型。这里有一个例子:

class OfferResource(ModelResource):
    user = fields.ForeignKey(UserResource, 'user', full=False)
    country = fields.ForeignKey(CountryResource, 'country', full=True) 
    campaign = fields.ForeignKey(CampaignResource, 'campaign', full=False)    
    network = fields.ForeignKey(NetworkResource, 'network', full=False, null=True)       
    class Meta:
        queryset = Offer.objects.prefetch_related('offerstat').select_related('country', 'campaign', 'network').all()   

#1


1  

In your tastypie api.py file you probably need to change the default queryset to either include prefetch_related or select_related. Which you use depends on your actual model. Here's an example:

在你tastypie api。py文件可能需要将默认的queryset更改为包含prefetch_related或select_related。你使用什么取决于你的实际模型。这里有一个例子:

class OfferResource(ModelResource):
    user = fields.ForeignKey(UserResource, 'user', full=False)
    country = fields.ForeignKey(CountryResource, 'country', full=True) 
    campaign = fields.ForeignKey(CampaignResource, 'campaign', full=False)    
    network = fields.ForeignKey(NetworkResource, 'network', full=False, null=True)       
    class Meta:
        queryset = Offer.objects.prefetch_related('offerstat').select_related('country', 'campaign', 'network').all()