Postgres SELECT DISTINCT Does Not Scale
Thread
Loading the complete thread in the background. This saved snapshot is available now. Refresh
Unofficial Hacker News client; not affiliated with Y Combinator.
Postgres SELECT DISTINCT Does Not Scale
Loading the complete thread in the background. This saved snapshot is available now. Refresh
Unofficial Hacker News client; not affiliated with Y Combinator.
DiabloD3 · · focus · HN ↗
Correct. This is documented in depth: DISTINCT sorts the results first.
The article's use case seems to imply the author did not know about GROUP BY, nor does it imply the author knew about indexes, nor ANALYZE. Postgres 18's new skip scan indexing also could help here, so ensuring the planner chooses that could help.
Dylan16807 · · focus · HN ↗
The article explains that skip scan doesn't do anything here.
> nor does it imply the author knew about indexes, nor ANALYZE
Indexes were talked about a lot, and they explicitly mentioned looking at the query plan.
gfody · · focus · HN ↗
DiabloD3 · · focus · HN ↗
Twisell · · focus · HN ↗
I guess we just had to patiently wait for the OP to hopefully read Postgres documentation from postgreSQL 10.x before correcting the article again.
Dylan16807 · · focus · HN ↗
SkiFire13 · · focus · HN ↗
Because it has the same issue.
SELECT DISTINCT and GROUP BY are equivalent from a query planner perspective.
Dylan16807 · · focus · HN ↗
tpetry · · focus · HN ↗
DiabloD3 · · focus · HN ↗
joinHNtheysaid · · focus · HN ↗
[dead]
nattaylor · · focus · HN ↗
Postgres doesn't have it yet <a href="https://wiki.postgresql.org/wiki/Loose_indexscan" rel="nofollow">https://wiki.postgresql.org/wiki/Loose_indexscan
supermatt · · focus · HN ↗
Tepix · · focus · HN ↗
Dylan16807 · · focus · HN ↗
nikolatt · · focus · HN ↗
thesuperevil · · focus · HN ↗
[dead]
sandeepkd · · focus · HN ↗
Based on my experience queries like these cannot scale, whatever you do. However if you are already on a path where you had invested a lot in such queries then hire a DBA, if you are not far off then hire an architect to model the data for better performance.
Dylan16807 · · focus · HN ↗
sandeepkd · · focus · HN ↗
> If you have m distinct values in an index, then listing them this way takes m log(n) time, which is fine for many use cases no matter how much data you have.
And NO the runtimes are not right away applicable on machines at scale. You are dealing with DB locks, page sizes, available memory, existing data in memory, queue depth. Experienced folks get paid to short circuit such learnings
adrianN · · focus · HN ↗
Dylan16807 · · focus · HN ↗
stemchar · · focus · HN ↗
atombender · · focus · HN ↗
The original idea that he worked on with the DBOS people at MIT and Stanford was very different and much, much more ambitious, which is why the name DBOS seems a little out of place now. The original idea was much closer to a "database OS".
Here [1] is the paper, which proposes that "all operating system state should be represented uniformly as database tables, and operations on this state should be made via queries from otherwise stateless tasks." The team later published another paper based on their prototype work [2].
Instead, they basically implemented Temporal as a client library with Postgres as the state layer. It's good, but only tangentially related to the original vision.
Maybe the long-term plan is an actual database OS, but it kind of looks like they decided they had to pivot to something much simpler, and slapped on an "for AI" like everyone is doing these days.
[1] <a href="https://arxiv.org/abs/2007.11112" rel="nofollow">https://arxiv.org/abs/2007.11112
[2] <a href="https://dl.acm.org/doi/10.14778/3485450.3485454" rel="nofollow">https://dl.acm.org/doi/10.14778/3485450.3485454
procaryote · · focus · HN ↗
Some use it because they don't understand uniqueness constraints and try to fix it in post so to say. Some use it because they forgot a join condition and are absolute amateurs. Some use it because it fixed a problem for them once and now they add it everywhere
These people seem to outnumber the people who use SELECT DISTINCT in a well thought out manner
Dylan16807 · · focus · HN ↗
photios · · focus · HN ↗
thom · · focus · HN ↗
wvbdmp · · focus · HN ↗
I wouldn’t use it in application code where I control the schema, but it’s great for exploratory stuff and custom reports over third-party databases.
gregw2 · · focus · HN ↗
I'd only add one more point. Sometimes the root cause is poor table design (or for analytic use cases ETL design without proper preconditions or postconditions) where uniqueness is not enforced and that is the root cause that should be fixed if at all possible. A "first normal form" violation in the database design so to speak. Otherwise the SELECT DISTINCT disease in the code base on top of the database just spreads.
colenikol2 · · focus · HN ↗
atemerev · · focus · HN ↗
SkiFire13 · · focus · HN ↗
martinlucas1994 · · focus · HN ↗
[dead]