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 %}
{{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 %}
{{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