title: PostgreSQL optimization: Sequantial Scan VS Index Scan
body: <p>I have one interesting case with select on postgres table:</p>
  
  <pre><code>advert (~2.5 million records)
      id serial,
      user_id integer (foreign key),
      ...
  </code></pre>
  
  <p>Here is my select:</p>
  
  <pre><code>select count(*) from advert where user_id in USER_IDS_ARRAY
  </code></pre>
  
  <p>And if <code>USER_IDS_ARRAY</code> length &lt;= 100 I have next explain analyze:</p>
  
  <pre><code>Aggregate  (cost=18063.36..18063.37 rows=1 width=0) (actual time=0.362..0.362 rows=1 loops=1)
    -&gt;  Index Only Scan using ix__advert__user_id on advert  (cost=0.55..18048.53 rows=5932 width=0) (actual time=0.030..0.351 rows=213 loops=1)
          Index Cond: (user_id = ANY ('{(...)}'))
          Heap Fetches: 213
  Planning time: 0.457 ms
  Execution time: 0.392 ms
  </code></pre>
  
  <p>But when <code>USER_IDS_ARRAY</code> length > 100:</p>
  
  <pre><code>Aggregate  (cost=424012.09..424012.10 rows=1 width=0) (actual time=867.438..867.438 rows=1 loops=1)
    -&gt;  Seq Scan on advert  (cost=0.00..423997.11 rows=5992 width=0) (actual time=0.375..867.345 rows=213 loops=1)
          Filter: (user_id = ANY ('{(...)}'))
          Rows Removed by Filter: 2201318
  Planning time: 0.261 ms
  Execution time: 867.462 ms
  </code></pre>
  
  <p>No matter what user_ids in USER_IDS_ARRAY, only it's length matters.</p>
  
  <p>Does anybody have ideas how to optimize this select for more then 100 user_ids?</p>