title: postgresql COUNT(DISTINCT ...) very slow
body: <p>I have a very simple SQL query:</p>
  
  <pre><code>SELECT COUNT(DISTINCT x) FROM table;
  </code></pre>
  
  <p>My table has about 1.5 million rows. This query is running pretty slowly; it takes about 7.5s, compared to</p>
  
  <pre><code> SELECT COUNT(x) FROM table;
  </code></pre>
  
  <p>which takes about 435ms. Is there any way to change my query to improve performance? I've tried grouping and doing a regular count, as well as putting an index on x; both have the same 7.5s execution time.</p>