Мне нужна помощь в оптимизации запроса PostgreSQL, который использует предложение BETWEEN
с полем timestamp
.
У меня есть 2 таблицы:
ONE(int id_one(PK), datetime cut_time, int f1 . . .)
содержащий около 3394 строк
TWO(int id_two(PK), int id_one(FK), int f2 . . .)
содержащий около 4000000 строк
Индексы btree есть на ПК id_one
и id_two
, на FK id_one
и cut_time
.
Я хочу выполнить запрос типа:
select o.id_one, Date(o.cut_time), o.f1, t.f2
from one o
inner join two t ON (o.id_one = t.id_one)
where o.cut_time between '2013-01-01' and '2013-01-31';
Этот запрос извлекает около 1 700 000 строк примерно за 7 секунд.
Ниже приводится отчет об анализе объяснения:
"Merge Join (cost=20000000003.53..20000197562.38 rows=1680916 width=24) (actual time=0.017..741.718 rows=1692345 loops=1)"
" Merge Cond: (c.coilid = hf.coilid)"
" -> Index Scan using pk_coils on coils c (cost=10000000000.00..10000000382.13 rows=1420 width=16) (actual time=0.008..4.539 rows=1404 loops=1)"
" Filter: ((cut_time >= '2013-01-01 00:00:00'::timestamp without time zone) AND (cut_time <= '2013-01-31 00:00:00'::timestamp without time zone))"
" Rows Removed by Filter: 1990"
" -> Index Scan using idx_fk_lf_data on hf_data hf (cost=10000000000.00..10000166145.90 rows=4017625 width=16) (actual time=0.003..392.535 rows=1963386 loops=1)"
"Total runtime: 768.473 ms"
Индекс в столбце отметки времени не используется. Как оптимизировать этот запрос?