292 points · by truth_seeker · 7y ago · 121 comments
Am I understanding this guy's presentation correctly? (I left around the 20 min mark of the 50 min presentation).
Oh yea, ORM.
I've always veiwed it as a legitimate (though always over engineered) solution to wrap some repetitive database operations in a consistent manner.
The biggest issue with ORM isn't the solution they came up with, half baked and buggy as they are, but the problem they are actually employeed to "solve": developers afraid of SQL. Whether they can't write it, can't learn it, or just straight hate the string representation in their code, it's the main reason people sign on for ORM.
Then they prop it up with all manner of baseless reasoning.
Theb there is the leakyness. ORMs are usually quite fluid in their language, they make it easy to represent the database in code. The only issue is that it isn't there. It's a separate thing. When you see database code it should be obvious and very far away from business logic. What does ORM do? Mix it all up On purpose. Have fun with all the mocks.
This sounds awfully like a straw man. I've worked on several projects that used an ORM, and it was never because developers couldn't write SQL.
ORMs solve a real problem: SQL isn't composable. That's it.
Sooner or later, every non-trivial program needs to compose arbitrary pieces of one query with arbitrary pieces of another, so you'll either use an ORM (i.e., native data structures instead of raw strings), or essentially write your own ad-hoc one.
It has nothing to do with lack of ability. The staunchest ORM advocate I've worked with was also the first to pull out the debugger and single-step the MySQL driver to track down a bug (that one was nasty -- it was indeed in the driver).
I use an HTML generator, and a compiler, too, and it's not because I'm "afraid" of assembly language or that I "can't learn" HTML. These abstractions have real value.
Anyway..
> ORMs solve a real problem: SQL isn't composable. That's it.
It's a DSL. It's literally the most composeble thing you could build as a solution. Hell it's so composable people decided they could build ORMs on top of it.
SQL was created to solve the problem of interfacing, composing and reasoning about tabular data.
ORMs were built because?
I feel like your thought that SQL isn't composable is due to a reluctance to create a function that executes a new query?
Or are you really looking for a way to compose queries over objects? Because SQL has that too..
How so? I've always found the opposite. I can build up a compilex query referencing many tables in SQL without impediment but am unable to do the same with an ORM.
>ORMs solve a real problem: SQL isn't composable. That's it.
But really though, these are at odds. The question is never "how can we compose this SQL." ORMs are always about writing less SQL.
I agree that if you are building ORM with the intention of hiding so many SQL details that the developers don't need to learn SQL, you'll run into problems.
SQLAlchemy on the other hand starts simple, but is flexible enough that you can do inner/outer joins, you have different loading strategies etc. It is quite nice if you are proficient enough in SQL.
When writing complicated select queries, I'd start by writing the raw SQL then spend my time representing it in SQLAlchemy. It's a bit less than double the work sometimes, but working with the SQLAlchemy objects in Python is quite nice as long as you pull them in an efficient manner. SQLAlchemy just feels like a well thought out ORM.
If you define your database properly typed, defaulted, and not null unless an update field that sort of layer isn't needed at all.
Using the correct native types on your function params or class structures means you'll never run into type mismatch or cast failures loading or unloading from the db.
So I just don't get why the need for ORM. The only thing that needs mapping is the table schema to your data type object/structs.
ORMs don't offer anything of value their and often make things you need to do in your job harder.
In my own work, the ORM is there for lots of boring stuff, like fetch the data and push it into an edit form and then push the contents of the edit form back into the database. When it comes to q query of any consequence I end up writing SQL and then presenting a data set to the ORM, something that it can understand.
Maybe it's my age, but I'm still not a big stored procedure guy. But you look at some of the interesting things you can do with SQL, it seems silly to try and interact with it only through someone else's ORM tool.
This is the only mapping I need from an ORM:
result = (object) row
row = (array) result
SQL does everything an ORM does better.I'm not sure how you can call moving from a low level programming language to a domain specific language like SQL "going lower". It's not and ORMs are usually so bad they're a regression in what you can do.
Also, when was the last time a change to the SQL spec broke your app?what about the last times you had to migrate ORM code?
Overall, I tend to agree with Uncle Bob's "Clean Architecture" [1][2] approach when it comes to DBs, though: The DB usually (rembember that there is an exception to every rule!) has no business of being the "center" of an application. It should provide you with functionality, not dictate how your business logic works.
If you can pull out your DB and give it a sensible interface, you still might end up having a lot of inline SQL queries, but they are not littered all over your codebase; they live inside a narrow scope (a plugin, say). Also, the SQL schema and queries in this case often are quite boring -- the tricky bit is coming up with a good interface that provides all the guarantees you need to uphold (complicated transactions maybe?). And remember that you need these guarantees because of how your business domain works, not because of your DB. Perhaps you can get rid of some of them if you organize your storage (i.e. DB) in a specific way?
[1]: http://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-ar... [2]: https://youtu.be/o_TH-Y78tt4?t=2565
Edit: Also note that there's no reason not to use a ORM in this way, too (if it suits you). The important part is the same: The ORM has no business dictating how your business logic should be implemented, and if you try to remove it from your core application, you will end up doing the same things. Now, if your ORM makes the implementation easier for you, go ahead and use it. But don't expect things to change if you put ORM-generated code instead of inline SQL at the center of your application.
[0] https://github.com/krisajenkins/yesql [1] https://github.com/honza/anosql
I finally gave up, and now most of the ORM usage in the codebase is just to communicate my custom queries to the underlying database and translate to/from domain entities. I feel like this is the sweet spot―give me a thin abstraction over SQL and do the gruntwork of converting domain entities to tables and vice-versa, but nothing else, thanks.
I'm still unhappy about the performance (when everything is in the working set, query takes 2ms and returns a couple of rows but the whole API call, which doesn't do much except massage the response to some kind of JSON, takes 30ms!), but that's on me for my ORM/language/runtime choice, I suppose.
Most languages plain sql drivers have something like a cast result row into object as you cursor over the set.
E.g. PHP has fetchObject https://www.php.net/manual/en/pdostatement.fetchobject.php
Which ORM was it, if I may ask?
Something like iBatis/nBatis? (Haven't used this since 2004)
No, you weren't.
> ...queries where I didn't need 90% of the info.
Corporate management, consulting and engineering subfields are made of people who don't check out from their personal needs when they work. Hype cycles, fashions, scenes and subcultures exist in software engineering just like they exist in corporate management and consulting. People want new and change.
SQL is the best for most database needs, but not for everything. It may seem boring if there is no marketing push that connects to emotions. SQL was established and boring for new software engineers. The emergence of "Web 2.0" in early 2000s started hype cycle for no-SQL. The 'scene' was so emotionally engaging that enterprises were abandoning SQL databases because "they don't scale" and because that's what their developers wanted to work with.
If you do fast prototyping this is not needed, you just need some quick and dirty persistence. Unfortunately the prototype is always the 1.0 version to ship.
—Fred Brooks, The Mythical Man Month
The situation is so bad, I would actually like learn more about fashion and fabric than deal with another trend. Where is the objectivity ?
Just to be clear: the talk uses hyperbolic language to make a point being that SQL is perfect for data processing, not for everything
Your presentation was excellent. I'm not attacking you. I agreed with everything you said (at least the parts that I watched). My _rant_ (or attack), instead is with the _phenomenon_ we were so strongly and needlessly gripped in for two whole decades. A cult phenomenon that had all the hallmarks of religious indoctrination.
You said: "Just to be clear: the talk uses hyperbolic language to make a point being that SQL is perfect for data processing, not for everything"
I agree. SQL isn't useful for all cases. But those cases are few.
So, there's the point.
I feel that much of what we do with map, reduce, sort and filter on arrays in modern JS is similar to SQL. Unfortunately, there is no query optimisation, because that would break your procedural code.
What I'd like to see more of is domain-specific languages embedded in general-purpose languages, like React with JSX, or just a better bridge between the two. We have these two worlds of in-memory data structures on one side, and databases on the other, that we keep separate. NoSQL was an attempt at bringing them together, but it put far too little emphasis on complex queries and filters.
It would be nice if we could just define data structures and their relations once and not have to do it again. It would be nice if foreign keys neatly mapped to object references in OOP and everything stayed live, so if you change a reference, that happens instantly in the database too, unless you wrap it in a transaction block.
Now pull this off while still retaining the ability to open an SQL REPL to test out statements, or manipulate field definitions.
I think that's what everyone wants, really. A seamless way for code to interact with data on all levels, with no perceptible boundary once you've opened a session to your DB server.
"Database as a value" had me jaw dropping and then crying most of the way through..
maybe that, but then again, what we do with custom data structures that are required for certain things to be halfway efficient, is much harder or impossible to do in SQL, correct?
Neat. I hadn't thought of it that way before.
Python comprehensions, etc. are mostly just SQL -- likewise most languages where "map" is part of the collections API.
SQL motivates the utility of functional programming for data-oriented applications.
See TransformListComprehension section. Haskell uses tricks from SQL while still having nice syntax.
http://ekmett.github.io/discrimination/ - a library to perform what SQL engines do for optimization of relational queries, online and in Haskell.
And a map can be the same as a projection as found in sql.
The method-chains-vs-expression-tree semantics don't matter much when doing typical in-memory map/filter/reduce on lists but make LINQ to SQL and other more complex use cases much more powerful.
A lot of people recognise apples and oranges for what they are. The talk is meaningless to those people.
A lot of other people don't / can't make the distinction (yet). For those people, the talk is an eye opener.
* first 20 mins: Writing a contrived report query that's easy in SQL is easy in SQL.
* but: most real reports require many different fields collated into many parts of say a pdf or xls. Once you take this into account writing custom code to do the entire report is often the only way to do it. :<
* you often can't do some of what the author suggests bc in a "real" org you have db zealots that only allow access to their precious through stored procs and a surprisingly large number of meetings
* trying to do some non-trivial where clauses? Not in SQL - so you have to use an external lang. I call this "Works in my presentation" syndrome.
* he acts surprised that a lang that uses a db through a SQL api can't be as fast/efficient/terse as a system that uses SQL and has direct access to the db/storage.
Stopped after that.
PS Seems like a funny guy tho - liked his style! :>
Can't comment on db zealots. It must suck when that happens.
But disagree entirely on your other parts. Haven't run into such cases yet, wrote all my reports in SQL (and something like XSLT for presentation logic). I've mainly used Oracle.
Nobody sane uses oracle for new development.
If you chose to code your app to MongoDB or Dynamo or whatever you are 100% locked in unless you do a rewrite of your whole data access layer.
To top it off, in the real world you’ll still need a separate SQL DB for reporting and analytics.
That said, SQL doesn't lend itself to easy composability and you can end up with "4000 line monsters" if you really tried to put as much business logic into the query as possible.
Is anyone working on SQL extensions, ORM, or new 4th Gen language that could actually support something like 4000 lines of SQL in a maintainable way?
In my experience so far, this has usually been the actual problem. If you are not actively trying to "cleanly" separate your business logic from your storage, you tend to end up maintaining a mess. People correctly identify the problems with the mess, but incorrectly attribute it to SQL instead of architectural problems. So they switch to an ORM and wonder why it doesn't improve anything a little while later.
And that's not to say it is easy to come up with a useful way to interface between storage and the business logic -- on the contrary. It's just that the naive approach (strongly coupling the two everywhere) is kind of a worst-case scenario. Also, there are certainly situations where ORMs are very useful; it's just that a messily integrated ORM isn't any better than messily integrated SQL.
The thing is: Your business logic needs to be provided with ways of triggering certain operations (along with certain guarantees). I haven't seen cases where a single operation would take 4000 lines of "interesting" SQL though (ignoring very long lists of columns :) ). So, if the SQL is hard to maintain: Is this a problem of the SQL, or is this a byproduct of unclear (or ever changing) interface requirements? Sometimes, taking a couple of steps back and considering alternative approaches can yield much better results than optimizing a "wrong" solution.
(I guess this could be considered a form of the "X Y Problem": http://xyproblem.info/).
Usually when I'm using any technology, being able to understand how it works is my most important priority, because it prevents me from using it improperly. I have a hard time understanding what those "algorithms" really are after all. I heard that databases engines use backtracking, but I'm not really sure that's what it is talked about here. Maybe databases don't use BT?
Using another language that parses every time you do something doesn't feel very fast. It is fast for many applications, but I don't think it is for all of them.
In the end, it's the same old combat, either choose peak performance or development delays with good enough performance.
Although I have to admit that for GIS (geographic information system), databases with embedded R-trees and other things are very much welcome, since implementing those algorithms from the ground up is way too hard.
Most of the focus was on things like database normalization instead of practical usage. I recall upon starting my first job, a co-worker gave me a crash course in what exactly a LEFT JOIN was (though at this point it had been ~3 years since the college database course, which had been so lackluster I hadn't used one during those years).
I will discuss a few ORMs to make the students aware they are out there but we will not teach one in the course.
As a contrasting example, the experience you get with ElasticSearch is that queries have predictable performance even as your data changes. In that way it's much, much nicer. On the other hand, you lose joins, which is a huge downside.