Postgresql 9.3
我有一张带有date_field的表格:
date_field timestamp without time zone
CREATE INDEX ix__table__date_field ON table USING btree (date_field)
WHERE date_field IS NOT NULL;
然后我尝试使用我的部分索引:
EXPLAIN ANALYZE SELECT count(*) from table where date_field is not null;
Aggregate (cost=29048.22..29048.23 rows=1 width=0) (actual time=41.714..41.714 rows=1 loops=1)
-> Seq Scan on table (cost=0.00..28138.83 rows=363755 width=0) (actual time=41.711..41.711 rows=0 loops=1)
Filter: (date_field IS NOT NULL)
Rows Removed by Filter: 365583
Total runtime: 41.744 ms
但是它使用部分索引来比较日期:
EXPLAIN ANALYZE SELECT count(*) from table where date_field > '2015-1-1';
Aggregate (cost=26345.51..26345.52 rows=1 width=0) (actual time=0.006..0.007 rows=1 loops=1)
-> Bitmap Heap Scan on table (cost=34.60..26040.86 rows=121861 width=0) (actual time=0.005..0.005 rows=0 loops=1)
Recheck Cond: (date_field > '2015-01-01 00:00:00'::timestamp without time zone)
-> Bitmap Index Scan on ix__table__date_field (cost=0.00..4.13 rows=121861 width=0) (actual time=0.003..0.003 rows=0 loops=1)
Index Cond: (date_field > '2015-01-01 00:00:00'::timestamp without time zone)
Total runtime: 0.037 ms
那么,为什么它不使用date_field is not null
上的索引?
提前致谢!