I am using Postgres 9.2.24.
I have a table named _order
with about 100,000,000 rows. The table has a column named merged_id int8
. About 2,000,000 of the _order
rows have a merged_id
value, the rest has null.
I find two different Postgres behavior where I search _order
use the query
select * from _order where merged_id in ( 10001 ,10002 ,10003 ....., 11000);
If I create an index like this:
create index order_merged_id_index on _order(merged_id);
No matter how many ids in in clause (test from 1 to 50 to 100 to 200 to 1000) EXPLAIN
shows the search will use index_scan
.
But if I create this partial index instead:
create index order_merged_id_index on _order(merged_id) where merged_id is not null;
EXPLAIN
shows a seq_scan
for more than 100 id numbers in the WHERE
clause.
Why is this?
And is there any way to fix it?