▲ | KronisLV 6 days ago | |||||||||||||||||||
> Schema design should be flexible, because once you have thousands or millions of records, it can be an enormous pain to change the schema. However, if you make it too flexible (e.g. by sticking everything in a “value” JSON column, or using “keys” and “values” tables to track arbitrary data) you load a ton of complexity into the application code (and likely buy some very awkward performance constraints). Drawing the line here is a judgment call and depends on specifics, but in general I aim to have my tables be human-readable: you should be able to go through the database schema and get a rough idea of what the application is storing and why. I’m surprised that the drawbacks of EAV or just using JSON in your relational database don’t get called out more. I’d very much rather have like 20 tables with clear purpose than seeing that colleagues have once more created a “classifier” mechanism and are using polymorphic links (without actual foreign keys, columns like “section” and “entity_id”) and are treating it as a grab bag of stuff. One that you also need to read the application code a bunch to even hope to understand. Whenever I see that, I want to change careers. I get that EAV has its use cases, but in most other cases fuck EAV. It’s right up there with N+1 issues, complex dynamically generated SQL when views would suffice and also storing audit data in the same DB and it inevitably having functionality written against it, your audit data becoming a part of the business logic. Oh and also shared database instances and not having the ability to easily bootstrap your own, oh and also working with Oracle in general. And also putting things that’d be better off in the app inside of the DB and vice versa. There are so many ways to decrease your quality of life when it comes to storing and accessing data. | ||||||||||||||||||||
▲ | dondraper36 6 days ago | parent | next [-] | |||||||||||||||||||
There's a great book SQL Antipatterns, by Bill Karwin where this specific antipattern is discussed and criticized. That said, sometimes when I realize there's no way for me to come up even with a rough schema (say, some settings object that is returned to the frontend), I use JSONB columns in Postgres. As a rule of thumb, however, if something can be normalized, it should be, since, after all, that's still a relational database despite all the JSON(B) conveniences and optimizations in Postgres. | ||||||||||||||||||||
▲ | quibono 5 days ago | parent | prev [-] | |||||||||||||||||||
> storing audit data in the same DB and it inevitably having functionality written against it, your audit data becoming a part of the business logic What's the "proper" way to do this? Separate DB? Separate data store? | ||||||||||||||||||||
|