‹ BackHN Continuity

Thread

Postgres SELECT DISTINCT Does Not Scale

103 points · 28 comments · KraftyOne

Loading the complete thread in the background. This saved snapshot is available now. Refresh

  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. gfody · · focus · HN ↗
        nope - either gets you a HashAggregate, GroupAggregate, or Unique depending on whether the column/input is indexed/ordered
      2. 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 had 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?
          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 ↗
          I was reading the article within a couple minutes of your first comment and it had those things.
    2. tpetry · · focus · HN ↗
      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.
      1. DiabloD3 · · focus · HN ↗
        The article seems to have changed since I commented.
        1. joinHNtheysaid · · focus · HN ↗

          [dead]

  2. nattaylor · · focus · HN ↗
    Loose index scan is made for this <a href="https:&#x2F;&#x2F;dev.mysql.com&#x2F;doc&#x2F;refman&#x2F;8.0&#x2F;en&#x2F;group-by-optimization.html#loose-index-scan" rel="nofollow">https:&#x2F;&#x2F;dev.mysql.com&#x2F;doc&#x2F;refman&#x2F;8.0&#x2F;en&#x2F;group-by-optimizatio...

    Postgres doesn&#x27;t have it yet <a href="https:&#x2F;&#x2F;wiki.postgresql.org&#x2F;wiki&#x2F;Loose_indexscan" rel="nofollow">https:&#x2F;&#x2F;wiki.postgresql.org&#x2F;wiki&#x2F;Loose_indexscan

    1. supermatt · · focus · HN ↗
      As mentioned in the article.
    2. Tepix · · focus · HN ↗
      As is mentioned in the article (now).
      1. Dylan16807 · · focus · HN ↗
        (since it was originally posted)
    3. nikolatt · · focus · HN ↗
      I haven&#x27;t followed the latest Postgres releases, but I thought it would be added by now - Timescale&#x2F;Tigerdata have already implemented a similar index scan in Postgres: <a href="https:&#x2F;&#x2F;www.tigerdata.com&#x2F;blog&#x2F;how-we-made-distinct-queries-up-to-8000x-faster-on-postgresql" rel="nofollow">https:&#x2F;&#x2F;www.tigerdata.com&#x2F;blog&#x2F;how-we-made-distinct-queries-....
  3. thesuperevil · · focus · HN ↗

    [dead]

  4. sandeepkd · · focus · HN ↗
    It says that the company is co-founded by Postgres creator. I find that bit hard to believe given that there is nothing novel in the article, probably discovery for them. I do understand that everyone has to go through their own journey to learn these things but at the same time when you are running business then seeking professional help isnt a bad idea.

    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.

    1. Dylan16807 · · focus · HN ↗
      Scale with what? 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.
      1. sandeepkd · · focus · HN ↗
        The way the OP is trying to achieve all the goals by pushing the complexity on the queries&#x2F;database is what I am referring to as non-scalable as data grows on SQL DB.

        &gt; 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

        1. adrianN · · focus · HN ↗
          Runtimes are usually pretty well applicable at scale, it’s just that most people don’t have a good intuition about asymptomatic notation. Constants and lower order terms matter a lot in practice but are hidden in asymptotic notation.
        2. Dylan16807 · · focus · HN ↗
          Selecting distinct values of a single column with a simple condition is hardly pushing complexity into the database.
    2. stemchar · · focus · HN ↗
      It&#x27;s Stonebraker, he has a history of doing this to sell shit to people who don&#x27;t need it.
    3. atombender · · focus · HN ↗
      My understanding (possibly flawed) is that Stonebraker isn&#x27;t directly involved anymore.

      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 &quot;database OS&quot;.

      Here [1] is the paper, which proposes that &quot;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.&quot; 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&#x27;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 &quot;for AI&quot; like everyone is doing these days.

      [1] <a href="https:&#x2F;&#x2F;arxiv.org&#x2F;abs&#x2F;2007.11112" rel="nofollow">https:&#x2F;&#x2F;arxiv.org&#x2F;abs&#x2F;2007.11112

      [2] <a href="https:&#x2F;&#x2F;dl.acm.org&#x2F;doi&#x2F;10.14778&#x2F;3485450.3485454" rel="nofollow">https:&#x2F;&#x2F;dl.acm.org&#x2F;doi&#x2F;10.14778&#x2F;3485450.3485454

  5. procaryote · · focus · HN ↗
    I&#x27;ve generally started to treat use of SELECT DISTINCT as a warning flag, as it&#x27;s very common that it indicates bad code

    Some use it because they don&#x27;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

    1. Dylan16807 · · focus · HN ↗
      It&#x27;s definitely a warning flag if you&#x27;re applying it to full-ish rows. This situation seems much more innocuous to me.
    2. photios · · focus · HN ↗
      Yeah, people have been advising against DISTINCT for ages. I guess that piece of common wisdom somehow got lost in this age of AI wonders. :D
    3. thom · · focus · HN ↗
      Yeah, it&#x27;s almost always more intention-revealing to use CTEs and WHERE EXISTS.
    4. wvbdmp · · focus · HN ↗
      As always, it’s worth distinguishing use cases. I use DISTINCT all the time in ad-hoc reporting queries and even in longer-lived reports if it doesn’t tank the query’s performance.

      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.

    5. gregw2 · · focus · HN ↗
      I agree 100% with each and every word in all six of your sentences.

      I&#x27;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 &quot;first normal form&quot; violation in the database design so to speak. Otherwise the SELECT DISTINCT disease in the code base on top of the database just spreads.

  6. colenikol2 · · focus · HN ↗
    Have watched the movie. One of the most addictive games ever. Still playing sometimes. But here is one that can play against your friend on a single phone where both players holds and play on a same phone faced against each other
  7. atemerev · · focus · HN ↗
    Well, if the solution is a manual workaround that forces a better query plan, this needs to be a part of Postgres itself so it builds better query plans automatically.
    1. SkiFire13 · · focus · HN ↗
      The manual workaround is even documented in postgres&#x27; wiki <a href="https:&#x2F;&#x2F;wiki.postgresql.org&#x2F;wiki&#x2F;Loose_indexscan" rel="nofollow">https:&#x2F;&#x2F;wiki.postgresql.org&#x2F;wiki&#x2F;Loose_indexscan
  8. martinlucas1994 · · focus · HN ↗

    [dead]

Open on Hacker News to reply ↗

Unofficial Hacker News client; not affiliated with Y Combinator.