by any626 · 8y ago · 0 comments
id, name, age, gender
1213, fake, 60, female
they would have property_id, user_id, value
1 (assume age), 1213, 60
2 (gender), 1213, female
This gives them the freedom to add more properties to the user without always having to add a column to the users table. When querying the database you'll have to do unions or joins.Still, a JSON column will arguably be faster than a pure KV table since you can more efficiently query it, especially any non-JSON columns.
"Relational databases don't scale"
Well they worked fine for decades before Mongo's marketing claimed so.
Normalize it properly. If this is not possible, ensure that both the userid and the property are indexed
[0]https://en.wikipedia.org/wiki/Entity%E2%80%93attribute%E2%80...
Edit: Had some bad attempt at writing this query but it's rather late and it made no sense.
SELECT DISTINCT loyaltyMemberID
from members as m
INNER JOIN properties as p1 on m.id = p1.user_id
INNER JOIN properties as p2 on m.id = p2.user_id
INNER JOIN properties as p3 on m.id = p3.user_id
AND (p1.prop = 'gender' AND p1.value = x)
AND ((p2.prop = 'age' AND p2.value = y) OR (p3.prop = 'censor' AND p3.value = z)) SELECT DISTINCT loyaltyMemberID
from members as m
INNER JOIN properties as p on m.id=p.user_id
WHERE (p.prop='name' AND p.value = value) AND
...etc.And they mention in the post that most queries don't use that many fields.
Then years later I ran into the guy who had to do some work on it after I left that company. I must have scarred him pretty badly, because he remembered it enough to bring it up as pretty much the first topic after the obligatory 'hey so what are you up to nowadays'. When I think back about it now, it was a cringey solution - then again, this was at a company where nobody had ever heard of a 'database index' (or if they did, never mentioned or implemented them).
[0]: https://en.wikipedia.org/wiki/Entity%E2%80%93attribute%E2%80...
"can be useful in various scenarios, such as rows with many attributes that are rarely examined"
They are querying these quite heavily, they aren't just random attributes they need to retrieve.
You could have
property_id, user_id, value(string)
1 (assume age), 1213, 60
2 (gender), 1213, female
or property_id, user_id, value_str, value_int
1 (assume age), 1213, null, 60
2 (gender), 1213, female, null
or have a mapping in the application to get the type of the property. Plenty of ways to handle it.I think you're right. Oh ... my ... god ...
I wish I could say this is the worst example of a database schema I've ever seen, but it isn't.
Technology cycle:
X gets invented -> idiots abuse it -> X "is bad" -> Y (strictly worse than X) is "so much better" -> idiots abuse it -> Y "is bad" -> ...
In Mysql, it is JSON data type [1], in Postgres JSON/JSONB [2].
Creating indexes across them is doable, through a workaround (involving what is generally referred to as "VIEWS", but can be called calculated columns or something like that).
And, frankly, in the worst case for indexing, these databases still perform comparable to key-value stores in speed (especially SQLite).
[1] https://dev.mysql.com/doc/refman/5.7/en/json.html#json-paths
[2] https://www.postgresql.org/docs/9.4/static/datatype-json.htm...
Using an EVA, you can have a "keys (id, key_name)" table, and then only use the IDs in the values table, reducing that waste.
By the way, you don't need views for indexing on JSONB fields, it's supported out of the box in PG.
And I don't think you will necessarily get better performance with json fields vs an EAV model. Yes, you can index json fields by creating virtual views, but that requires that you know the field ahead of time. With an EAV model, you can have your values table indexed and then join.
But I am excited to start using the json field types. In many cases, it will really simplify things over the traditional EAV stuff.