PostgreSQL整数索引未用于值1

时间:2019-01-11 08:14:47

标签: postgresql indexing

我在整数和日期列上有一个复合索引。分析查询时,索引用于大于1的值。但是,与值1比较时,不使用索引。

另外,当使用负值时,Postgres使用的是位图堆扫描

结果如下:

使用的索引

explain analyze select * from journal where status = 2 and deletedat is null;

Index Scan using journal_status_idx on journal  (cost=0.28..25.03 rows=9 width=1333) (actual time=0.008..0.008 rows=0 loops=1)
   Index Cond: ((status = 2) AND (deletedat IS NULL))
 Planning time: 0.153 ms
 Execution time: 0.052 ms

未使用索引

explain analyze select * from journal where status = 1 and deletedat is null;

Seq Scan on journal  (cost=0.00..154.71 rows=1957 width=1333) (actual time=0.012..2.654 rows=2034 loops=1)
   Filter: ((deletedat IS NULL) AND (status = 1))
   Rows Removed by Filter: 743
 Planning time: 0.125 ms
 Execution time: 4.714 ms

位图堆扫描

explain analyze select * from journal where status = -1 and deletedat is null;

Bitmap Heap Scan on journal  (cost=5.72..133.52 rows=140 width=1333) (actual time=0.100..0.299 rows=146 loops=1)
   Recheck Cond: ((status = '-1'::integer) AND (deletedat IS NULL))
   Heap Blocks: exact=49
   ->  Bitmap Index Scan on journal_status_idx  (cost=0.00..5.68 rows=140 width=0) (actual time=0.075..0.075 rows=146 loops=1)
         Index Cond: ((status = '-1'::integer) AND (deletedat IS NULL))
 Planning time: 0.000 ms
 Execution time: 1.291 ms

有人有什么主意吗?

0 个答案:

没有答案