7 comments

  • nattaylor6 hours ago
    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...</a><p>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</a>
    • nikolatt6 minutes ago
      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-...</a>.
    • Tepix4 hours ago
      As is mentioned in the article (now).
      • Dylan168073 hours ago
        (since it was originally posted)
  • procaryote2 hours ago
    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<p>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<p>These people seem to outnumber the people who use SELECT DISTINCT in a well thought out manner
    • photios1 hour ago
      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
    • Dylan168072 hours ago
      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.
  • sandeepkd3 hours ago
    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.<p>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.
    • Dylan168073 hours ago
      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.
      • sandeepkd3 hours ago
        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.<p>&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.<p>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
        • Dylan168072 hours ago
          Selecting distinct values of a single column with a simple condition is hardly pushing complexity into the database.
        • adrianN3 hours ago
          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.
    • stemchar3 hours ago
      It&#x27;s Stonebraker, he has a history of doing this to sell shit to people who don&#x27;t need it.
  • atemerev1 hour ago
    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.
  • thesuperevil4 hours ago
    [flagged]
  • colenikol22 hours ago
    [dead]
  • DiabloD37 hours ago
    &quot;Postgres SELECT DISTINCT Does Not Scale&quot;<p>Correct. This is documented in depth: DISTINCT sorts the results first.<p>The article&#x27;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&#x27;s new skip scan indexing also could help here, so ensuring the planner chooses that could help.
    • Dylan168077 hours ago
      Would GROUP BY fix the issue?<p>The article explains that skip scan doesn&#x27;t do anything here.<p>&gt; nor does it imply the author knew about indexes, nor ANALYZE<p>Indexes were talked about a lot, and they explicitly mentioned looking at the query plan.
      • gfody4 hours ago
        nope - either gets you a HashAggregate, GroupAggregate, or Unique depending on whether the column&#x2F;input is indexed&#x2F;ordered
      • DiabloD34 hours ago
        The article seems to have changed since I commented.
        • Dylan168073 hours ago
          <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....</a><p>It had the same stuff a month ago.
        • Twisell3 hours ago
          Why don&#x27;t he use GROUP BY on indexed columns was my first thought also.<p>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.
          • SkiFire1317 minutes ago
            &gt; Why don&#x27;t he use GROUP BY on indexed columns was my first thought also.<p>Because it has the same issue.<p>SELECT DISTINCT and GROUP BY are equivalent from a query planner perspective.
          • Dylan168073 hours ago
            Does that fix it? Why would people be trying to add new index scanning modes if that&#x27;s enough to fix it?<p>Someone else said it doesn&#x27;t.
    • tpetry5 hours ago
      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.