News for hackers. Stuff that matters.
Re:Data denormalization is broken by barrkel on Friday September 30, 2016 @04:52PM (#12615220)
If someone's primary perspective of the database is through the lens of an OO graph, they will write code that performs abysmally. Graph navigation from row to row is antithetical to all the power relational algebra gives you. That's the biggest risk: viewing the DB primarily as an object store.

For the app I work on, the DB is only viewed as an object store for very small sets of data - configuration, specifically. It's impossible to map it to objects in any realistically performant way for our user's data, not least because that would require the data round-tripping through the application. It's much, much more efficient to execute my multi-million row updates and inserts on the database server directly rather than drag the data down to the app, tweak it slightly, then persist it back.

Re:Data denormalization is broken by ak39 on Friday September 30, 2016 @05:12PM (#12615318)
This is true.

ORMs force the thinking "objects first" (ie. work with objects, the SQL will be handled for you). This translates into a design of "per object" architecture. Business logic is abstracted for handling single rows only - one at a time - encapsulation being the design idiom. However, this is a trap that immediately becomes apparent when you realise that you've forgone the powerful set-based capabilities of SQL. SQL doesn't "need to see" a single row of a table as anything special to warrant ring-fencing in the name of encapsulation. The whole (related) data model is a single algebraic representation of state.

Re:Data denormalization is broken by icebraining on Friday September 30, 2016 @07:13PM (#12615787)
On the other hand, isn't that just a sign of a weak ORM? For example, in the platform I use, when I "fetch" some records by ID, it doesn't actually issue a SELECT; it just collects the ID and returns a proxy object. Only when I access an attribute of that object, it'll issue a SELECT for all the IDs in question.

One could extend that for more complex operations - instead of immediately executing the operations, it'd collect the operations and convert them into efficient queries. In a way, this is already done in "fluent interfaces" like LINQ, but with a more flexible language (Lisp?), I'm sure one could use standard control flow instead of a custom layer.

Re:Data denormalization is broken by GrinningFool on Saturday October 01, 2016 @05:47AM (#12617220)
> For example, in the platform I use, when I "fetch" some records by ID, it doesn't actually issue a SELECT; it just collects the ID and returns a proxy object. Only when I access an attribute of that object, it'll issue a SELECT for all the IDs in question.

I'm my opinion this is the least desirable behavior. It abstracts the data layer away to the point where you don't think about what's happening under the hood when you type foo = Klazz.new("id"); foo.bar; foo.baz;

Was that one, two or three separate queries ? Did a query run at all? Does it matter, after all you have an object to work on, right in your language of choice.

Sure the ORM may be smart about it and do the right thing - I have no doubt that most decent ones day. But that misses the point - as the person using this interface you get divorced from the direct awareness that this data has an origin, and there is a cost to access it. After all, you're just instantiating an object.

Patterns that use interfaces that make it so simple to do a thing that you don't need awareness that you're doing it tend to degrade quickly after the original engineers who did have that understanding leave a project.

Re:Data denormalization is broken by icebraining on Saturday October 01, 2016 @06:38AM (#12617304)
That's a fair point; on the other hand, aren't SQL queries themselves divorced from the actual data layer? Knowing whether the ORM issued one or two queries doesn't tell you if you're fetching data from disk or memory, whether it's making an index scan or full table scan, whether the join fits in working memory or will hit the disk, etc.

Isn't knowing what queries are issued giving you a false sense of knowledge about how the data is actually fetched?

And much the same way you can request a query plan, the ORM could provide the list of queries issued for a given piece of code.

Re:Data denormalization is broken by fdgdasfadsf on Saturday October 01, 2016 @04:40AM (#12617062)
This is what the Django ORM does when used with some degree of care.
Re:Data denormalization is broken by icebraining on Saturday October 01, 2016 @04:49AM (#12617082)
I know it uses lazy results, but can you actually loop over the results without triggering a query? Could you write something like:

  records = ORM.search(type='X')
  for record in records:
    record.a = record.b + 30
and only trigger a single UPDATE query?
Re:Data denormalization is broken by collyw on Saturday October 01, 2016 @08:52AM (#12617642)
There is an update method that can be used with F expressions to achieve this.

This is the example in their docs:

    Entry.objects.all().update(n_pingbacks=F('n_pingbacks') + 1)
Re:Data denormalization is broken by combatentropy on Saturday October 01, 2016 @01:04PM (#12618750)
To write it in pure SQL would ensure the single query you're looking for and be less to type:

  update records
  set a = b + 30
  where type = 'X'
Re:Data denormalization is broken by chris_wot on Friday September 30, 2016 @07:09PM (#12615778)
I've never quite worked out why graphs and relations can't be unified in some way. They both rely on first order logic, after all.
Re:Data denormalization is broken by DougBTX on Saturday October 01, 2016 @01:56AM (#12616673)
The select N+1 problem is a problem because of the overhead on each select, if that was free the graph navigation and relational set operations would match.

Reminds me of, "in theory, there's no difference between theory and practice, in practice there is".