Performance queries helps

20 views
Skip to first unread message

carlos

unread,
Jul 11, 2020, 5:16:37 AM7/11/20
to django...@googlegroups.com
Hi, i need any advice for performance queries
i have a model name Post
post have m2m categories and fk author

the index page show 7 categories in separated row
i try performance 
this example
queryset_global = Post.objects.filter(status__contains='publish').prefetch_related('category').select_related('author')
then i make 7 queries for 7 categories show in index page.

newscat1 = queryset_global.filter(category__slug__contains="cat1_name").order_by('-date')[:5]
newscat2 = queryset_global.filter(category__slug__contains="cat2_name").order_by('-date')[:5]
newscat3 = queryset_global.filter(category__slug__contains="cat3_name").order_by('-date')[:5]
newscat4 = queryset_global.filter(category__slug__contains="cat4_name").order_by('-date')[:5]
newscat5 = queryset_global.filter(category__slug__contains="cat5_name").order_by('-date')[:5]
newscat6 = queryset_global.filter(category__slug__contains="cat6_name").order_by('-date')[:5]
newscat7 = queryset_global.filter(category__slug__contains="cat7_name").order_by('-date')[:5]

i retrieve 35 post for show in index page

according debug toolbar this produce SQL
2599.59 ms (44 queries including 34 duplicates )

Why? 

but when i loop in template index.html
{% for news1 in newscat1 %}
    {{news.name}}
    {{newa.date|date:'Y-m-d'}}
    {{news.body|safe|truncatewords:20}}
    {{news.category.all.0}}
   <a href="{{news.get_absolute_url}}">Read More</a>
{% endfor %}
 ...
this repeat in the template 7 time
...
{% for news7 in newscat7 %}
    {{news.name}}
    {{newa.date|date:'Y-m-d'}}
    {{news.body|safe|truncatewords:20}}
    {{news.category.all.0}}
   <a href="{{news.get_absolute_url}}">Read More</a>
{% endfor %}

according debug toolbar this produce SQL
3463.65 ms (86 queries including 77 duplicates )

this is incredible it doubles the amount of queries to the db that may be happening?

i need some ideas or tips

Cheers

Budi Hermansyah

unread,
Jul 11, 2020, 6:06:31 AM7/11/20
to django...@googlegroups.com
di you ever try an elastic search for indexing our categories product to speed up?


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAM-7rO1nzzqWf_cpQOrjYwQmxkd77H0AJ8W03FS%3D%2BoA3gEE4zg%40mail.gmail.com.

damanjeet kaur

unread,
Jul 11, 2020, 4:59:23 PM7/11/20
to django...@googlegroups.com
{% for news7 in newscat7 %}
    {{news7.name}}
    {{news7.date|date:'Y-m-d'}}
    {{news7.body|safe|truncatewords:20}}
    {{news7.category.all.0}}
   <a href="{{news7.get_absolute_url}}">Read More</a>
{% endfor %}



carlos

unread,
Jul 15, 2020, 7:53:02 PM7/15/20
to django...@googlegroups.com

Budi Hermansyah  

no, but I think i can improve the queries but I don't know how: /
the debug toolbar show many many queries to the database  (44 queries including 34 duplicates ) i don't know duplicates



--
att.
Carlos Rocha
Reply all
Reply to author
Forward
0 new messages