r/PostgreSQL • • Jun 23 '20

An average day using an ORM

Post image
84 Upvotes

25 comments sorted by

15

u/[deleted] Jun 23 '20

Shitposts infecting /r/postgresql, now that's a first.

3

u/execrator Jun 24 '20

I've used Django for years. I can only imagine the shudder of horror a Postgres DBA would feel when picking up a Django database. Why'd you do this at the application level? Why are you selecting every bloody column everywhere? Etc.

I'm only guessing, but assume there is some eyerolling in the Postgres world when it comes to Django. And yet in the Django world there is a sort of fanatical love for Postgres!

1

u/[deleted] Jun 24 '20

So try out PgRoutiner for .net -> https://github.com/vbilopav/PgRoutiner

It will give you a static type checking while still preserving the freedom to use as much as SQL or PLPGSQL as you want.

0

u/[deleted] Jun 23 '20

1

u/[deleted] Jun 23 '20

How is this better than writing raw SQL?

1

u/jk3us Programmer Jun 23 '20

ORMs can write the simple stuff for you that you need to use pretty frequently. The trick is to know when you know when to either write raw queries to create db views or functions to abstract away sql complexity to simplify usage.

2

u/Randommaggy Jun 23 '20

ORMs are for basic crud. Never seen it being successfully used for anything else without sacrificing at least an order of magnitude of performance.

3

u/alcalde Jun 23 '20

What about human performance?

2

u/Randommaggy Jun 24 '20

ORMs introduce more work over the life cycle of a project if used outside of simple operations. Devs that can't be trained to write decent SQL tend to produce technical dept and should be kept away from large quantities of data. Most devs need a day or two max, of good training to be more productive with SQL than ORMs.

1

u/alcalde Jun 28 '20

A day? Aren't developers used to thinking imperatively rather than declaratively?

1

u/Randommaggy Jun 29 '20

Any good developer is able to work in both mindsets.
I've observed a huge gap in productivity and skill between developers that are only able to work in one and those that are able to work in both.

With an average teacher and average motivation it might take more than a day, maybe a week at worst. If you're able to describe the data you expect as a response to your query the translation to SQL is quite simple. Just imagine the levels of subqueries as the steps in a fluent chain.

ORMs such as LINQ expect what's basically upside-down inside-out SQL with added pain and confusion.

1

u/not-enough-failures Jun 26 '20

And they're fine for that.

-6

u/alcalde Jun 23 '20

Raw SQL is like the regex expressions of databases. No sane human could have designed it.

Create a computed field in the select portion of the select statement? You have to repeat that formula over and over throughout the statement. Want to filter a query that contains a window function? No can do... now you need to query your query to filter it.

ORMs let you work in ways natural to programmers' thinking.

11

u/execrator Jun 24 '20 edited Jun 24 '20

SQL is incredibly readable given its power and especially it's vintage. From that point of view it's been well designed. Sure it can get hard to understand but that tends to be when the underlying concepts are getting hard. If you don't understand the concepts the syntax can't magically understand it for you.

Regex, which I love dearly, is not very readable even for simple concepts and goes downhill from there. The rough design goal seems to have been to reduce the noise of metacharacters amongst the signal of literal characters. When you've got very few literals you sort of wish for a different tradeoff though.

Definitely agree there are some failings in the SQL design. Not being able to WHERE on a SELECT you've defined is frustrating.

2

u/alcalde Jun 28 '20

SQL is incredibly readable given its power and especially it's vintage.

I believe the vintage is the problem. Heck, the full standard mandates an Algol/Pascal-style imperative programming language. :-) As the language has grown, more and more complex ideas have had to be squeezed into an initial design that really wasn't built to contain them. If you were starting over from scratch, I believe there are several things that would be done differently today.

From that point of view it's been well designed. Sure it can get hard to understand but that tends to be when the underlying concepts are getting hard. If you don't understand the concepts the syntax can't magically understand it for you.

It's 2020... computers are very good at understanding syntax. Take the examples I gave....

SELECT something, something_else, returns / total_sales AS return_ratio

There's no logical reason in the world that I shouldn't be able to use "return_ratio" elsewhere in the query. Heck, a preprocessor could parse the first part of the SELECT statement then replace every instance of "return_ratio" with "returns / total sales" in the rest of the of statement and the SQL query parser wouldn't need to be changed at all! This is just silly and an annoyance that has to be blatantly obvious but bafflingly never resolved. I was half-expecting when I posted this for someone to say "Oh, the awesome PostgreSQL had additions to the syntax so these problems don't exist in PostgreSQL" but apparently not. Weirdly, people are defending glaring, trivially-fixed language design flaws that were probably introduced because of some ancient hardware limitation that no longer exists. One source says this problem exists because the SELECT statement is parsed from the bottom up. I'm reminded of the old joke:

Patient: Doctor, it hurts when I do this.

Doctor: Then don't do that!

Isn't a basic rule of programming "don't repeat yourself"? SQL violates some basic rules of programming.

Want to filter a query that contains a window function? No can do... now you need to query your query to filter it.

Similar problem. SQLite spits out an error message about not being able to filter on a window function. So if it KNOWS I'm trying to filter on a window function, why doesn't it automatically rewrite the query as

SELECT * FROM
{original query}
WHERE {the filter expression I tried to use}

and then feed this to an untouched SQL parser? Apparently to comply with standard SQL, where again the impediment is annoying, the manual solution verbose and the software preprocessor solution almost trivial (a lot less work than introducing window functions in the first place!).

ORMs tend to clean up a lot of this unnecessary and baffling verbosity and make things more concise, like a high-level wrapper for a low-level API. They tend to follow DRY, good principles of abstraction, etc. SQL itself has introduced windows functions, CTEs and other features to try to cut down on SQL that otherwise used to look like Russian nesting dolls with subquery after subquery. If no one objects to those, well, I'm just suggesting it's still got a ways to go (and weirdly, some much simpler examples of verbosity have never been fixed).

3

u/[deleted] Jun 24 '20

Create a computed field in the select portion of the select statement? You have to repeat that formula over and over throughout the statement. Want to filter a query that contains a window function? No can do... now you need to query your query to filter it.

Both of those issues are easily resolved by using simple CTE or temp tables or even simple subquery I don't see an issue here.

You may call it querying a query but that's how SQL works, it's a query language. I understand that is not natural to trained programmer.

ORMs let you work in ways natural to programmers' thinking.

And that is far from optimal as far as the database goes.

There is impendence mismatch between not only types in your database and in your language but also an impendence mismatch between a way of thinking SQL/Database developer and typical programmer.

For example:

SQL/Database developer considers iteration loop an anti-pattern

The programmer thinks almost exclusively think in loops

You need to master both ways of thinking to be full stack developer.

2

u/Randommaggy Jun 24 '20

Iteration loops should be few and far between in most languages there days. If you can use a foreach instead of an old fashioned for loop you most likely should. Writing a good amount of SQL will help you write better procedural code.

Working on subqueries or CTEs are basically the same frame of mind as writing in fluent syntax except you can do multiple operations per step, a lot more efficiently. Also you nest them instead of chaining.

1

u/alcalde Jun 28 '20

Both of those issues are easily resolved by using simple CTE or temp tables or even simple subquery I don't see an issue here.

You may call it querying a query but that's how SQL works, it's a query language. I understand that is not natural to trained programmer.

It doesn't need to work that way, and I'd argue that it would be somewhat trivial to fix at least the first of those two problems with a simple query preprocessor without touching any lower-level parsing or query engines.

And that is far from optimal as far as the database goes.

I'd argue it is not humans who are supposed to bow to the software/machines, but the machines that are supposed to accommodate the humans (except for programming in Delphi, of which it was once said "the programmer acts as a sort of human precompiler").

3

u/[deleted] Jun 23 '20

wat

1

u/alcalde Jun 28 '20

You heard me.

0

u/[deleted] Jun 23 '20

[deleted]

2

u/[deleted] Jun 23 '20

Same! Been going on about 2 years. I want to say I love it for simple operations but whenever it gets beyond basic SQL the thing really tends to shit the bed. Not to mention if you see the queries it generates sometimes and just view the raw SQL... it's frightening.

1

u/not-enough-failures Jun 26 '20

I made my own ORM that simply allows one to bind properties of an entity class to columns. It also supports one-to-one and one-to-many references to other entities, but that's it. It doesn't generate tables nor queries (except for basic CRUD operations), there's no hidden magic.

It's extremely refreshing and allows me to use the full power of SQL (through the same DSL I built the ORM with (JetBrains Exposed, if anyone is wondering)) when needed which allows for powerful combinations of complicated queries + avoidance of boring, repetitive manual CRUD operations.

1

u/[deleted] Jun 24 '20

I've used many different ORMs in different languages but I think that Django ORM is worst.

3

u/Randommaggy Jun 24 '20

Have a look at sequelize, nhibernate or ef. They all produce shit SQL for any complexity.

0

u/[deleted] Jun 24 '20

[deleted]

1

u/[deleted] Jun 24 '20

Well that just my subjective opinion, there is always a worse...