title: Postgresql database getting slow (selection) for millions records
body: <p>Postgresql database getting slow when trying to fetch data for millions records.  I tried with <strong>Materialized view</strong>, however performance was drastically fast but it doesn't give real time data.</p>
  
  <p>I am also using aggregation ex. sum, count, group by clause etc...</p>
  
  <pre><code>SELECT offer_id as off_id,
    COUNT(distinct ip) as hosts,
    COUNT(distinct click_id) as clicks 
  FROM offer_affiliate_stats 
  WHERE 
    created_dt &gt;= '2019-06-01' 
  AND 
    created_dt  &lt;= '2019-06-30' 
  GROUP BY off_id;
  </code></pre>
  
  <p>I have tried with <strong>Materialized view</strong>.</p>
  
  <p>Indexes are applied on id, created_dt, click_id </p>
  
  <p>My output should be like this:</p>
  
  <pre><code>off_id               | 79
  hosts                | 4
  clicks               | 4
  offer_name           | "Testing Javelin"
  offer_id             | 
  total_conversions    | 
  total_income         | 
  optimised_count      | 
  optimised_income     | 
  approved_income      | 
  approved_conversions | 
  declined_income      | 
  declined_conversions | 
  total_payout         | 
  </code></pre>
  
  <p>Actually without using <strong>distinct</strong> keyword it work flawlessly but when I use <strong>distinct</strong> it takes long time.</p>