Speed up query-heavy requests in Django - Deepstash

Explore the World's Best Ideas

Join today and uncover 100+ curated journeys from 50+ topics. Unlock access to our mobile app with extensive features.

Finding query bottlenecks

Finding query bottlenecks

Review your API endpoints and views for:

  • Duplicate queries
  • Slow queries
  • ORM calls that resolve more queries than you expect

3

49 reads

Tips to reduce query bottlenecks

Tips to reduce query bottlenecks

  1. Reduce number of queries
  2. Speed up common queries
  3. Cache results of heavy queries
  4. Turn off ATOMIC_REQUESTS -- This settings makes all queries run in a transaction. Read Django docs before changing it to understand the trade-offs.
  5. Tune database settings

3

33 reads

Reduce number of queries

Reduce number of queries

  • Use select_related() to retrieve Foreign Key relations in the same query.
  • Use prefetch_related() for many-to-many and many-to-one relations.
  • Re-use query results across the lifetime of the request if you need them in more places.

3

24 reads

Speed up common queries

Speed up common queries

  • Add indexes on fields you frequently filter/sort on.
  • Analyze query plans generated by common queries and identify bottlenecks.
  • Rewrite queries to return smaller results sets when possible.
  • Re-model data in a way that allows indexes to work more efficiently.
  • As a last resort, use raw SQL when you can't generate an efficient query through the ORM.

3

11 reads

CURATED BY

ocpodariu

Alt account of @ocp. I use it to stash ideas about software engineering

CURATOR'S NOTE

๐Ÿ“– Two Scoops of Django

โ€œ

Read & Learn

20x Faster

without
deepstash

with
deepstash

with

deepstash

Access to 200,000+ ideas

โ€”

Access to the mobile app

โ€”

Unlimited idea saving & library

โ€”

โ€”

Unlimited history

โ€”

โ€”

Unlimited listening to ideas

โ€”

โ€”

Downloading & offline access

โ€”

โ€”

Personalized recommendations

โ€”

โ€”

Supercharge your mind with one idea per day

Enter your email and spend 1 minute every day to learn something new.

Email

I agree to receive email updates