在where条件里面尽量不要使用函数,使用函数会进行遍历操作,
如下:
--不使用时间函数
explain analyse
select c_index_code,sum(off_line_time) as off_line_time from resource_camera_info_merger
where merger_time between '2015-11-03' and '2015-11-11'
group by c_index_code;
HashAggregate (cost=9872.43..9906.05 rows=3362 width=28) (actual time=64.240..67.006 rows=12243 loops=1)
Group Key: c_index_code
-> Seq Scan on resource_camera_info_merger (cost=0.00..9368.59 rows=100766 width=28) (actual time=0.011..37.755 rows=99377 loops=1)
Filter: ((merger_time >= '2015-11-03'::date) AND (merger_time <= '2015-11-11'::date))
Rows Removed by Filter: 270996
Planning time: 0.158 ms
Execution time: 67.342 ms
--使用时间转换函数
explain analyze
select c_index_code,sum(off_line_time) as off_line_time from resource_camera_info_merger
where merger_time between to_date('2015-11-03', 'yyyy-mm-dd') and to_date('2015-11-11', 'yyyy-mm-dd')
group by c_index_code;
HashAggregate (cost=11724.29..11757.91 rows=3362 width=28) (actual time=519.805..522.717 rows=12243 loops=1)
Group Key: c_index_code
-> Seq Scan on resource_camera_info_merger (cost=0.00..11220.46 rows=100766 width=28) (actual time=0.017..490.643 rows=99377 loops=1)
Filter: ((merger_time >= to_date('2015-11-03'::text, 'yyyy-mm-dd'::text)) AND (merger_time <= to_date('2015-11-11'::text, 'yyyy-mm-dd'::text)))
Rows Removed by Filter: 270996
Planning time: 0.119 ms
Execution time: 523.208 ms