‹ BackHN Continuity

Thread

Postgres SELECT DISTINCT Does Not Scale

106 points · 28 comments · KraftyOne

  1. DiabloD3 · · focus · 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.

    1. Dylan16807 · · focus · HN ↗
      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.

      1. DiabloD3 · · focus · HN ↗
        The article seems to have changed since I commented.
        1. Twisell · · focus · HN ↗
          Why don't he use GROUP BY on indexed columns was my first thought also.

          I guess we just have to patiently wait for the OP to hopefully read Postgres documentation from postgreSQL 10.x before correcting the article again.

          1. Dylan16807 · · focus · HN ↗
            Does that fix it? Why would people be trying to add new index scanning modes if that's enough to fix it?

            Someone else said it doesn't.

          2. SkiFire13 · · focus · HN ↗
            > Why don't he use GROUP BY on indexed columns was my first thought also.

            Because it has the same issue.

            SELECT DISTINCT and GROUP BY are equivalent from a query planner perspective.

        2. Dylan16807 · · focus · HN ↗
          <a href="https:&#x2F;&#x2F;web.archive.org&#x2F;web&#x2F;20260814160231&#x2F;https:&#x2F;&#x2F;www.dbos.dev&#x2F;blog&#x2F;postgres-select-distinct-does-not-scale" rel="nofollow">https:&#x2F;&#x2F;web.archive.org&#x2F;web&#x2F;20260814160231&#x2F;https:&#x2F;&#x2F;www.dbos....

          It had the same stuff a month ago.

Open on Hacker News to reply ↗

Unofficial Hacker News client; not affiliated with Y Combinator.