Would you cache this queryset or optimize the ORM instead?

Been working on a small side project and noticed one page is starting to slow down as the dataset grows. Right now I’m using select_related() in a couple of places, but I’m wondering if I’m just avoiding the real problem.

The project itself is pretty simple. It’s basically a booking system I’m using to learn Django with real world examples instead of fake data. I picked a hotel in Islamabad, Pakistan because it gave me enough relationships to work with, and I ended up looking at Grace Hospitality (https://gracehospitality.pk/grace-boutique-hotel/) to get an idea of how room listings and categories are usually organized.

At what point do you guys normally start caching query results instead of trying to squeeze more performance out of the ORM? Curious how you’d approach it.

There’s too many things that could be the cause of the slow downs.

can you provide some environment info, without it any advice provided is just a stab in the dark.

  • DB server resources
  • Table size (rows)
  • are the queries using indexes
  • query time for the problematic / slow queries

Hey chief how’re you?

Before jumping to caching I’d look at what’s actually happening on that page.

A few things that usually kill performance as the DB grows:

1. N+1 queries

Even with select_related(), check if you also need prefetch_related() for M2M/FK reverse relations. Use Django Debug Toolbar to see how many queries that page fires.

2. Imports and logic on the view
Are you importing heavy stuff or doing extra work inside the loop/template? Sometimes it’s not the ORM, it’s code running per object.

3. Database type + indexes

What DB are you using? SQLite vs Postgres makes a huge difference. Also check if the fields you filter/order by have db_index=True. EXPLAIN ANALYZE pon the slow query helps a lot.

4. Pagination

If you’re loading all reservations/rooms at once, paginate. Loading 50k rows will be slow no matter what.

Caching helps, but only after the queries are optimized. Otherwise you’re just caching a slow query.

What DB are you on, and how many rows are we talking about on the main table? Also how many queries does Django Debug Toolbar show for that page?

Good Analyze

I share the same opinion