D
Deleted member 43669
Guest
Today at work, a report of mine was writing code equivalent to the following query:
, but using our ORM (Django's), by itering over items, extracting max using the ORM, adding the value and comparing. I took a look at the ORM docs, and it wasn't feasible to build the equivalent SQL query using the ORM without writing most of it in SQL (you can add SQL snippets to ORM queries).
He's unexperienced with SQL, and claimed that the SQL was harder to read than the equivalent Python/ORM code. I didn't agree, but that got me thinking.
At some point, SQL queries become quite complex and procedural code becomes easier to read at maintain. When would you say that happens?
Would you impose using the ORM over SQL (for queries which cannot be expressed in the ORM, of course)? I know the SQL is more efficient, but that rarely matters- and there's an advantage in writing everything in one language. What would you sacrifice to achieve single-language?
Code:
select items.id
from items
join subitems on items.id = subitems.item_id
where max(subitems.date) + items.interval < now()
group by items.id
, but using our ORM (Django's), by itering over items, extracting max using the ORM, adding the value and comparing. I took a look at the ORM docs, and it wasn't feasible to build the equivalent SQL query using the ORM without writing most of it in SQL (you can add SQL snippets to ORM queries).
He's unexperienced with SQL, and claimed that the SQL was harder to read than the equivalent Python/ORM code. I didn't agree, but that got me thinking.
At some point, SQL queries become quite complex and procedural code becomes easier to read at maintain. When would you say that happens?
Would you impose using the ORM over SQL (for queries which cannot be expressed in the ORM, of course)? I know the SQL is more efficient, but that rarely matters- and there's an advantage in writing everything in one language. What would you sacrifice to achieve single-language?