by _a_a_a_ · 3y ago · 0 comments
DB guy with 25+ years experience. Summary: it depends.
> joins are never cheap
it depends. On table size, indexes/table size vs how expensive the alternative is. Always!
> tables with billions of rows crossed with millions of rows just to find a single row with data is not something i would call cheap
indexes
> more often than not it is better to avoid joining large tables if you can live with duplicate data
1E9 x 1E6 = 1E15 (at worst anyway). A join via an index will save you colossal amounts of IO (though as ever, it depends).
Problem here isn't this mostly clueless advice (discarding/archiving unnecessary data is the only good idea here, and it's not used as often as it should be). Problem is strong opinions put forth by someone who doesn't have the necessary experience, or understanding of what's going on under the hood. Denormalising is a useful tool that IME rarely gains you more than it loses you, but this 'advice' is just going to lead people down the wrong alley, and I'm tired of suchlike n00b advice strongly (and incorrectly and arrogantly) expressed on HN.
(edited to fix maths error)
But those are details for the database engine to handle. And, as you said, indexes
select * from (select * from tbl) as subqry where subqry.col = 25
would be rewritten by any halfway decent optimiser to select * from (select * from tbl where tbl.col = 25)
(and FTR the outermost select * would be stripped off as well).Good DB optimisers do a whole load of that and much more.
Joining onto either table a or table b is something that REALLY trips optimizers up.
How else could I have posted better? Honest question.
> this mostly clueless advice
> strong opinions put forth by someone who doesn't have the necessary experience, or understanding of what's going on under the hood
> I'm tired of suchlike n00b advice strongly (and incorrectly and arrogantly) expressed on HN
You continue to just say it depends without giving any actual scenarios. You make it sound like magic, but it’s not: “under x and y, do z except when u” is better than “it depends, I’m sick of all these noobs”.
Also, your main points are against denormalization and avoiding large table joins which are 100% rational arguments under certain workloads.
His was stupid advice and had it should not have been given.
> You continue to just say it depends without giving any actual scenarios
it depends. Use your common sense and then use a stopwatch, is a good start. There are entire shelves of books on this, I won't repeat them.
> You make it sound like magic, but it’s not:
absolutely true!
> “under x and y, do z except when u” is better than
it's a multidimensional problems inc. memory size, disk size, the optimiser, sizes of particular tables joined, where the hotspot is, cost of updates of non-normalised tables, etc. I can't give general advice from here.
> Also, your main points are against denormalization and avoiding large table joins which are 100% rational arguments under certain workloads.
I said "Denormalising is a useful tool that IME rarely gains you more than it loses you,"
I don't accept your criticism.