title: PostgreSQL query not using index in production
body: <p>I'm noticing something strange/weird:</p>
  
  <p>The exact same query in development/production are not using the same query path. In particular, the development version is using indexes which are omitted in production (in favor of seqscan).</p>
  
  <p>The only real difference is that the dataset is production is significantly larger--the index size is 1034 MB, vs 29 MB in production. Would PostgreSQL abstain from using indexes if they (or the table) are too big? </p>
  
  <p>EDIT: <code>EXPLAIN ANALYZE</code> for both queries:</p>
  
  <p>Development:</p>
  
  <pre><code>Limit  (cost=41638.15..41638.20 rows=20 width=154) (actual time=159.576..159.581 rows=20 loops=1)
    -&gt;  Sort  (cost=41638.15..41675.10 rows=14779 width=154) (actual time=159.575..159.577 rows=20 loops=1)
          Sort Key: (sum(scenario_ad_group_performances.clicks))
          Sort Method: top-N heapsort  Memory: 35kB
          -&gt;  GroupAggregate  (cost=0.00..41244.89 rows=14779 width=154) (actual time=0.040..151.535 rows=14197 loops=1)
                -&gt;  Nested Loop Left Join  (cost=0.00..31843.75 rows=93800 width=154) (actual time=0.022..82.509 rows=50059 loops=1)
                      -&gt;  Merge Left Join  (cost=0.00..4203.46 rows=14779 width=118) (actual time=0.017..27.103 rows=14197 loops=1)
                            Merge Cond: (scenario_ad_groups.id = scenario_ad_group_vendor_instances.ad_group_id)
                            -&gt;  Index Scan using scenario_ad_groups_pkey on scenario_ad_groups  (cost=0.00..2227.06 rows=14779 width=114) (actual time=0.009..12.085 rows=14197 loops=1)
                                  Filter: (scenario_id = 22)
                            -&gt;  Index Scan using index_scenario_ad_group_vendor_instances_on_ad_group_id on scenario_ad_group_vendor_instances  (cost=0.00..1737.02 rows=27447 width=8) (actual time=0.007..7.021 rows=16528 loops=1)
                                  Filter: (ad_vendor_id = ANY ('{1,2,3}'::integer[]))
                      -&gt;  Index Scan using index_ad_group_performances_on_vendor_instance_id_and_date on scenario_ad_group_performances  (cost=0.00..1.73 rows=11 width=44) (actual time=0.002..0.003 rows=3 loops=14197)
                            Index Cond: ((vendor_instance_id = scenario_ad_group_vendor_instances.id) AND (date &gt;= '2012-02-01'::date) AND (date &lt;= '2012-02-28'::date))
  Total runtime: 159.710 ms
  </code></pre>
  
  <p>Production:</p>
  
  <pre><code>Limit  (cost=822401.35..822401.40 rows=20 width=179) (actual time=21279.547..21279.591 rows=20 loops=1)
    -&gt;  Sort  (cost=822401.35..822488.42 rows=34828 width=179) (actual time=21279.543..21279.560 rows=20 loops=1)
          Sort Key: (sum(scenario_ad_group_performances.clicks))
          Sort Method: top-N heapsort  Memory: 33kB
          -&gt;  GroupAggregate  (cost=775502.60..821474.59 rows=34828 width=179) (actual time=19126.783..21226.772 rows=34495 loops=1)
                -&gt;  Sort  (cost=775502.60..776739.48 rows=494751 width=179) (actual time=19125.902..19884.164 rows=675253 loops=1)
                      Sort Key: scenario_ad_groups.id
                      Sort Method: external merge  Disk: 94200kB
                      -&gt;  Hash Right Join  (cost=25743.86..596796.70 rows=494751 width=179) (actual time=1155.491..16720.460 rows=675253 loops=1)
                            Hash Cond: (scenario_ad_group_performances.vendor_instance_id = scenario_ad_group_vendor_instances.id)
                            -&gt;  Seq Scan on scenario_ad_group_performances  (cost=0.00..476354.29 rows=4158678 width=44) (actual time=0.043..8949.640 rows=4307019 loops=1)
                                  Filter: ((date &gt;= '2012-02-01'::date) AND (date &lt;= '2012-02-28'::date))
                            -&gt;  Hash  (cost=24047.72..24047.72 rows=51371 width=143) (actual time=1123.896..1123.896 rows=34495 loops=1)
                                  Buckets: 1024  Batches: 16  Memory Usage: 392kB
                                  -&gt;  Hash Right Join  (cost=6625.90..24047.72 rows=51371 width=143) (actual time=92.257..1070.786 rows=34495 loops=1)
                                        Hash Cond: (scenario_ad_group_vendor_instances.ad_group_id = scenario_ad_groups.id)
                                        -&gt;  Seq Scan on scenario_ad_group_vendor_instances  (cost=0.00..11336.31 rows=317174 width=8) (actual time=0.020..451.496 rows=431770 loops=1)
                                              Filter: (ad_vendor_id = ANY ('{1,2,3}'::integer[]))
                                        -&gt;  Hash  (cost=5475.55..5475.55 rows=34828 width=139) (actual time=88.311..88.311 rows=34495 loops=1)
                                              Buckets: 1024  Batches: 8  Memory Usage: 726kB
                                              -&gt;  Bitmap Heap Scan on scenario_ad_groups  (cost=798.20..5475.55 rows=34828 width=139) (actual time=4.451..44.065 rows=34495 loops=1)
                                                    Recheck Cond: (scenario_id = 276)
                                                    -&gt;  Bitmap Index Scan on index_scenario_ad_groups_on_scenario_id  (cost=0.00..789.49 rows=34828 width=0) (actual time=4.232..4.232 rows=37006 loops=1)
                                                          Index Cond: (scenario_id = 276)
  Total runtime: 21306.697 ms
  </code></pre>