logoalt Hacker News

DiabloD3 • today at 2:42 AM • 2 replies • view on HN

"Postgres SELECT DISTINCT Does Not Scale"

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.


Replies

Dylan16807 • today at 2:47 AM

Would GROUP BY fix the issue?

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.

➕ show 2 replies
tpetry • today at 4:16 AM

Did you even read the article? They show that a perfect index for their query didnt help because Skip Scan is currently not used for DISTINCT queries.

➕ show 1 reply