this post was submitted on 04 Oct 2023
77 points (86.7% liked)
Programming
17326 readers
217 users here now
Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!
Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.
Hope you enjoy the instance!
Rules
Rules
- Follow the programming.dev instance rules
- Keep content related to programming in some way
- If you're posting long videos try to add in some form of tldr for those who don't want to watch videos
Wormhole
Follow the wormhole through a path of communities [email protected]
founded 1 year ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
view the rest of the comments
Isn't that looking at it the wrong way / missing the point?
If you're fine with simple queries, use an ORM of your tech stack. Once you have to understand querying performance and weigh usage patterns, constraints, and cache performance and size, then it's not about SQL anymore. It's about understanding the DBMS you use.
You may ask "why can't I use a different language for the querying part of it". But I haven't seen anything better yet.
Having a common, well-understood, established, documented, inter-product compatible language has a lot of value. Using a different language to the well-established industry standard is in itself an increased hurdle to adoption.
Getting back to the original quote: I don't think anything else would serve bad developers / non-experts any better.
Simple queries don't result in simple SQL. How many joins and subqueries do you think an SQL query would require in order fulfill "Give me the top 10 artists of the 90s whose albums were nominated for the MTV awards but didn't win"?
In Django looks something like
What if one method wants the result of that but only wants the artists' names, but another one wanted additional or other fields? In django you could simply use
artists.only(*field_names)
and each method would provide a different set of field names. What would that look like without a capable ORM? Do you think somebody would refactor the method to add afield_names
argument? In my experience the result is a bunch of copy pasted queries that modify the query itself to add the fieldnames.Another common thing is querying related objects. Say you simply wanted to have information about the record label of the aforementioned artists while handling the artists. A many-to-one relationship (artist has one record label, record label has many artists). You could either
artist.record_label
while in your for-loop, but that would trigger an query for every artist (1+n problem). Or in django that'sartists.select_related("record_label")
and it will get all the record_labels in the same query.If it's a many-to-many relationship for example "festivals", then
.prefetch_related()
will first select the artists, then make a second query of festivals of those artists, andartist.festivals
would be available.An ORM like django makes that simple. SQL, does not.
So, before we even get to the DB optimisation part (which indices to create, whether a view is better or now, which storage engine to use, WAL size, yadayadayada), there's an entire interface / language that makes writing bad code very easy.
I'm too lazy to convert that by hand, but here's what chatgpt converted that to for SQL, for the sake of discussion:
I like Django's ORM just fine, but that SQL isn't too bad (it's also slightly different than your version though, but works fine as an example). I also like PyPika sometimes for building queries when I'm not using Django or SQLAlchemy, and here's that version:
I think PyPika answers your concerns about
It's just regular Python code, same as the Django ORM.