▲ 0 ▼ c/django@programming.dev · norambna@programming.dev · 9 Jan 2025 13:24 Database optimization isn’t always obvious #django Whitesell on whatever… Database optimization isn’t always obvious One of the issues of working with abstractions is that what you think may be logically true, isn’t. We use abstractions to hide underlying complexity of systems. Why is this an issue? I recently was reminded of this as the result of a blog post that was brought to my attention. The current (27 December 2024) issue of Django News highlighted an article titled “Django: avoid using .first() when retrieving a unique object”. Briefly, the article points out that if you use the construct “SomeModel.objects.filter(field=’value’).first()”, it adds an “order by” clause to the generated SQL that the expression “SomeModel.objects.get(field=’value’)” avoids. This is accurate. However, the author then goes on to say “No more ordering, it is faster!” – and it’s the conclusion in this statement that I question. Is it faster? I’m not sure. It is easy to lose sight of the fact that SQL itself is an abstraction layer for the database. Making judgments about the relative performance of two SQL statements is not trivial and cannot be accurately or definitively made without going beyond the SQL statement itself. kenwhitesell.github.io check post at origin via fosstodon.org/@carlton/113798310641582715
0 comments
log in to comment.