top | item 45675259

(no title)

vecter | 4 months ago

What're some DB query performance issues you've run across in the past and how did you resolve them?

discuss

order

pryelluw|4 months ago

People generally don’t take the time to learn the framework and miss out on the tooling it provides. select_related for example. If I had a dollar for every project I’ve been hired to work on that didn’t use it, well, I actually do.

Also, people in general don’t seem to be able to do more than very basic SQL, so creating views is seen as a little known performance “trick”.

In general, people who don’t read the manual.

nojs|4 months ago

FYI you can avoid the manual sprinkling of select_related somewhat with https://pypi.org/project/django-auto-prefetch/, which avoids the lowest hanging N+1 loops automatically. Definitely not a cure all but it helps.

vecter|4 months ago

Ah yes. That’s the one thing I need to teach everyone when they’re new to Django. I was wondering if there were other quirks to the ORM beyond avoiding N+1 queries.

ranger_danger|4 months ago

I assume they are referring to the default behavior of the ORM fetching all fields for a model by default, and the frequent need to use select_related/prefetch_related to group your queries into larger ones that are (usually) much faster than making many small queries for related tables.