Django Query optimizing performance (HUGE DATA)

105 views
Skip to first unread message

karthik challa

unread,
Jul 18, 2020, 11:42:00 PM7/18/20
to Django developers (Contributions to Django itself)
Hi Experts,

I am trying to execute the below query and the query is taking more than 5 minutes.

Here are the details

Model.py

class Url(models.Model):
 subdomain
= models.ForeignKey(Subdomain, null=True, blank=True, related_name='url_subdomain', on_delete=models.SET_NULL,db_index=True)
 full_url
= models.CharField(max_length=1000, unique=True, db_index=True)
 
class Meta:
 ordering
= ['full_url']

 
def __str__(self):
 
return self.full_url

1>Django query ORM 

subdomains = Subdomain.objects.all().annotate(numItems=Count('url_subdomain')).order_by('name')

2>SQL Query 

SELECT "urls_subdomain"."id", "urls_subdomain"."created_at", "urls_subdomain"."name", COUNT("urls_url"."id") AS "numItems" FROM "urls_subdomain" LEFT OUTER JOIN "urls_url" ON ("urls_subdomain"."id" = "urls_url"."subdomain_id") GROUP BY "urls_subdomain"."id" ORDER BY "urls_subdomain"."name" ASC

3>Templates

{% if subdomains %}
     
<div id="step-filter-tag" class="mb4">
         
<div class="b mb3">Subdomain</div>
             <div class="overflow-auto" style="max-height: 16rem;">
                 {% for item in subdomains %}
                     item
                 {% endfor %}
             </
div>
     
</div>

 
{% endif %}

Please let me know how can i optimize the preformance Thanks & Regards,
Karthik

Aymeric Augustin

unread,
Jul 19, 2020, 3:06:57 AM7/19/20
to django-d...@googlegroups.com
Hello,

I think you've found the wrong mailing list for this post. This mailing list is for discussing the development of Django itself, not for support using Django. This means the discussions of bugs and features in Django itself, rather than in your code using it. People on this list are unlikely to answer your support query with their limited time and energy.

For support, please follow the "Getting Help" page: https://docs.djangoproject.com/en/3.0/faq/help/ . This will help you find people who are willing to support you, and to ask your question in a way that makes it easy for them to answer.

Thanks for your understanding!

-- 
Aymeric.



--
You received this message because you are subscribed to the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-develop...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/996db826-fe18-4557-977b-58493c1f9481o%40googlegroups.com.

Vishnu Thuletiya

unread,
Jul 19, 2020, 7:16:37 AM7/19/20
to django-d...@googlegroups.com
I think you can use 'select_related(name of foreign key class) ' insted of all(). 

--

karthik challa

unread,
Jul 19, 2020, 12:47:14 PM7/19/20
to Django developers (Contributions to Django itself)
My Apologies .

-- 
Aymeric.



To unsubscribe from this group and stop receiving emails from it, send an email to django-d...@googlegroups.com.

chinna

unread,
Jul 19, 2020, 2:03:24 PM7/19/20
to django-d...@googlegroups.com
Thank you Vishnu
 

You received this message because you are subscribed to a topic in the Google Groups "Django developers (Contributions to Django itself)" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/django-developers/tEIXmhCXXO4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to django-develop...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-developers/CACggCHuGf%2BwUA4pYcB9%2BfDt3XLpVsjqQaw%3DyoL6WrfcwKp%2B5bA%40mail.gmail.com.

Sci Mithilesh

unread,
Jul 24, 2020, 3:19:10 PM7/24/20
to django-d...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages