News for hackers. Stuff that matters.
Re:A terrible schema from a clueless programmer by wodenokoto on Sunday November 07, 2021 @03:01AM (#29137393)
I'm kinda shocked that relational databases work so poorly out the box, that you can't even have a simple table with 4 or 5 values you care about, but have to split it out into 5 different tables and then create business rules about how to connect those into the original table.
Re:A terrible schema from a clueless programmer by marginalia_nu on Sunday November 07, 2021 @06:51AM (#29138653)
The hitch with relational databases is that you need to know what you are doing. This is mostly true for programming in general and a big reason why a lot of software isn't much faster today than it was in 1995, even though processors are many orders of magnitude faster. It's much more visible with relational databases, as they tend to accumulate so much data that bad solutions immediately break.

In this case, I don't think the 3NF-solution they came up with was necessary, it typically ends up requiring really complicated transactions and in some cases it may even be slower because of cache thrashing. What they really needed was to index their table. You can index strings, that's fine. It's reasonably fast, like even with a hundred million rows it's doable.

"But why doesn't the database build indices automatically?", I hear someone about to type, it's because indices make inserts slower and require more memory. In many cases auto-indexing would probably help, but it would break a lot of use-cases too.

Re:A terrible schema from a clueless programmer by Tanjreeve on Sunday November 07, 2021 @08:15AM (#29139200)
There are databases out there that analyse usage and filtering patterns and will generate and drop indexes automatically. That is a benefit to some people (normally more analytical workloads than critical production workloads for obvious reasons).
Re:A terrible schema from a clueless programmer by marginalia_nu on Sunday November 07, 2021 @09:46AM (#29139818)
Most databases offer some sort of optimization tooling, not all of it automatic. You just gotta know it's there.
Re:A terrible schema from a clueless programmer by GrumpyNl on Sunday November 07, 2021 @04:08AM (#29137692)
Thats just bad design, indexes would have solved that.
Re:A terrible schema from a clueless programmer by names_are_hard on Sunday November 07, 2021 @11:34AM (#29140873)
How? My understanding was that the four strings formed a natural key, and so wouldn't the index just be the size of the table? What do you gain from that? I'm a database noob so please enlighten me.

I thought the benefit here was that each of the four tables was much smaller (the original table was potentially the Cartesian join of all four mini tables) so fewer string comparisons were done.

I would have considered hashing the values in those four columns. Not sure how it would compare but if the string comparisons are the issue it might eliminate the problem without creating extra tables with indexes that need to be scanned. I wonder how that would compare.

Re:A terrible schema from a clueless programmer by jhgb on Monday November 08, 2021 @10:23AM (#29150161)
In theory, you'd be performing fewer string comparisons. In practice, it's far from clear that the original schema was that bad, performance-wise (except for the nonsensical storage of IP addresses as strings). Even if you have separate indices for the columns, even just picking the index with the highest selectivity (which is something a good database engine will do naturally) will prune most of the comparisons in a logical conjunction that can't succeed. If you have a compound index, you can look for the exact values of multiple columns at once. And if the indices are implemented correctly in the database engine, they will be compressing the keys, so the index should be smaller than the table anyway.

> I would have considered hashing the values in those four columns.

That's a good idea that definitely works, but at least to me it's not clear that it's more efficient than a compound index or a computed index (where a compound index is basically the special case of a computed index on a concatenation that is supported natively even in RDBMS packages that don't support generalized computed indices). It might very well end up a wash.

Re:A terrible schema from a clueless programmer by panta82 on Sunday November 07, 2021 @03:50AM (#29137601)
I'm a bit rusty with MySQL, but it feels like he could have just indexed the original table...
Re:A terrible schema from a clueless programmer by hanche on Sunday November 07, 2021 @04:01AM (#29137650)
That was a she, not a he.
Re:A terrible schema from a clueless programmer by yobbo on Sunday November 07, 2021 @05:26AM (#29138102)
> work so poorly

They don't work poorly, and there is no alternative. The database should not monitor the contents and opportunistically normalise the schema itself.

It's not "business rules", it's just normalisation.