Postgres SELECT DISTINCT performs surprisingly poorly. We explain why and how we mitigated it.
28 comments
Postgres doesn't have it yet https://wiki.postgresql.org/wiki/Loose_indexscan
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
I'd only add one more observation.
Sometimes the root cause is poor table design (or in analytic/OLAP use cases poor ETL design without proper data validation checks or handling) 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.
If that root cause is not addressed, then SELECT DISTINCT is more often necessary and the SELECT DISTINCT disease to be safe culture and behavior in the code base on top of the database just spreads.
Read the full thread on Hacker News →
Related stories
- DEV Community · 7 points · 5 days ago
- Hacker News · 1 points · 1 day ago
- DEV Community · 0 points · 6 days ago
- Can your Postgres survive a bad query?clickhouse.comHacker News · 1 points · 1 day ago
- Can your Postgres survive a bad query?clickhouse.comHacker News · 2 points · 2 days ago
- Can SELECT * Make a Query Go Faster?brentozar.comLobsters · 9 points · over 6 years ago