The Django ORM is pretty good, and I like it. But, to squeeze out performance, wouldn't you say you have to actually understand what is going on in the database?
Agree! I like django's ORM and I love SQL databases. It is nice that your in-code model changes, will reflect the schema changes in the database. And migrations are a blessing. (Of course it has some foot guns.)
For optimizations:
my_queryset.explain(verbose=True, analyze=True) is very nice to understand what is going on. If you cannot get it done with django's database abstractions, there is always:
my_queryset.extra() or even my_model.objects.raw(). Where the last one maps the result to the model, for free.
To others responding that ORMs are bad:
IMHO saying that raw sql is better than ORMs is short sighted. Many simple things are nice in ORMs. Queries are often simple. Today, in django, complex queries are also nice. For edge cases raw sql can be better.
Extending queries is also super easy with a good ORM, where with raw sql you would have to maintain two almost the same queries.
Just remember to run and inspect queryset.explain().
Fully agree with this. These discussions are always this or the other, but you can easily have both in the same project. Django's ORM and raw SQL functions prove that.
And more importantly, you'd want to design your database to be optimal for complex SQL queries and not whatever generic structure that matches your programming objects.
For optimizations: my_queryset.explain(verbose=True, analyze=True) is very nice to understand what is going on. If you cannot get it done with django's database abstractions, there is always:
my_queryset.extra() or even my_model.objects.raw(). Where the last one maps the result to the model, for free.
To others responding that ORMs are bad: IMHO saying that raw sql is better than ORMs is short sighted. Many simple things are nice in ORMs. Queries are often simple. Today, in django, complex queries are also nice. For edge cases raw sql can be better.
Extending queries is also super easy with a good ORM, where with raw sql you would have to maintain two almost the same queries.
Just remember to run and inspect queryset.explain().