title: Efficiently selecting distinct (a, b) from big table
body: <p>I have a table with around 54 million rows in a Postgres 9.6 DB and would like to find all distinct pairs of two columns (there are around 4 million such values). I have an index over the two columns of interest:</p>
  
  <pre><code>create index ab_index on tbl (a, b)
  </code></pre>
  
  <p>What is the most efficient way to get such pairs? I have tried:</p>
  
  <pre><code>select a,b
  from tbl
  where a&gt;$previouslargesta
  group by a,b
  order by a,b
  limit 1000
  </code></pre>
  
  <p>And also:</p>
  
  <pre><code>select distinct(a,b)
  from tbl
  where a&gt;previouslargesta
  order by a,b
  limit 1000
  </code></pre>
  
  <p>Also this recursive query:</p>
  
  <pre><code>with recursive t AS (
    select min(a) AS a from tbl
    union all
    select (select min(a) from tickets where a &gt; t.a)
    FROM t)
  select a FROM t
  </code></pre>
  
  <p>But all are slooooooow.</p>
  
  <p><strong>Is there a faster way to get this information?</strong></p>