Skip to content

Optimize now() in WHERE clauses #66

@megadawn

Description

@megadawn

Is it just me or...
SELECT public.create_range_partitions('dev.meter_read_delta_pm1', 'time_key', '2016-11-14'::date, '1 day'::interval, 10);

explain analyze
select * from meter_read_delta_pm1
where time_key >= timestamp '2016-11-14 ' and time_key < timestamp '2016-11-17' ;

-> Seq Scan on meter_read_delta_pm1_1 (cost=0.00..13.80 rows=380 width=182) (actual time=0.005..0.005 rows=0 loops=1)
-> Seq Scan on meter_read_delta_pm1_2 (cost=0.00..13.80 rows=380 width=182) (actual time=0.000..0.000 rows=0 loops=1)
-> Seq Scan on meter_read_delta_pm1_3 (cost=0.00..13.80 rows=380 width=182) (actual time=0.001..0.001 rows=0 loops=1)

all good. Now let's put now() function:
explain analyze
select * from meter_read_delta_pm1
where time_key >= now() - interval '2' day and time_key < now()

-> Seq Scan on meter_read_delta_pm1_1 (cost=0.00..18.55 rows=2 width=182) (actual time=0.005..0.005 rows=0 loops=1)"
" Filter: ((time_key <= now()) AND (time_key >= (now() - '2 days'::interval day)))"
" -> Seq Scan on meter_read_delta_pm1_2 (cost=0.00..18.55 rows=2 width=182) (actual time=0.001..0.001 rows=0 loops=1)"
" Filter: ((time_key <= now()) AND (time_key >= (now() - '2 days'::interval day)))"
" -> Seq Scan on meter_read_delta_pm1_3 (cost=0.00..18.55 rows=2 width=182) (actual time=0.001..0.001 rows=0 loops=1)"
" Filter: ((time_key <= now()) AND (time_key >= (now() - '2 days'::interval day)))"
" -> Seq Scan on meter_read_delta_pm1_4 (cost=0.00..18.55 rows=2 width=182) (actual time=0.001..0.001 rows=0 loops=1)"
" Filter: ((time_key <= now()) AND (time_key >= (now() - '2 days'::interval day)))"
" -> Seq Scan on meter_read_delta_pm1_5 (cost=0.00..18.55 rows=2 width=182) (actual time=0.001..0.001 rows=0 loops=1)"
" Filter: ((time_key <= now()) AND (time_key >= (now() - '2 days'::interval day)))"
" -> Seq Scan on meter_read_delta_pm1_6 (cost=0.00..18.55 rows=2 width=182) (actual time=0.002..0.002 rows=0 loops=1)"
" Filter: ((time_key <= now()) AND (time_key >= (now() - '2 days'::interval day)))"
" -> Seq Scan on meter_read_delta_pm1_7 (cost=0.00..18.55 rows=2 width=182) (actual time=0.001..0.001 rows=0 loops=1)"
" Filter: ((time_key <= now()) AND (time_key >= (now() - '2 days'::interval day)))"
" -> Seq Scan on meter_read_delta_pm1_8 (cost=0.00..18.55 rows=2 width=182) (actual time=0.001..0.001 rows=0 loops=1)"
" Filter: ((time_key <= now()) AND (time_key >= (now() - '2 days'::interval day)))"
" -> Seq Scan on meter_read_delta_pm1_9 (cost=0.00..18.55 rows=2 width=182) (actual time=0.001..0.001 rows=0 loops=1)"
" Filter: ((time_key <= now()) AND (time_key >= (now() - '2 days'::interval day)))"
" -> Seq Scan on meter_read_delta_pm1_10 (cost=0.00..18.55 rows=2 width=182) (actual time=0.001..0.001 rows=0 loops=1)"
" Filter: ((time_key <= now()) AND (time_key >= (now() - '2 days'::interval day)))"

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions