У меня есть запрос, который выполняется слишком долго. Я использую PostgreSQL 10.3. В моих таблицах, задействованных в этом запросе, у меня около 3,5 миллионов записей в каждой. Запрос:
SELECT thf.attr1, thf.attr2, thf.attr3, thf.attr4
FROM tb_one AS thf
INNER JOIN tb_two AS ths
ON ths.tb_hit_hitid = thf.tb_hit_hitid
WHERE ths.source IN ('source1', 'source2')
В этих таблицах у меня есть индекс:
CREATE INDEX tb_two_idx_1 on tb_two (Source ASC, attr5 ASC);
CREATE INDEX tb_one_idx_1 on tb_one USING btree (attr1 ASC,attr2 ASC,attr3 ASC,attr4 ASC);
CREATE INDEX tb_one_idx_2 on tb_hit_feature (tb_hit_HitId ASC);
CREATE INDEX tb_two_idx_2 on tb_hit_source (tb_hit_HitId ASC);
Это ПЛАН ЗАПРОСА (explain (analyse, buffers)
):
QUERY PLAN
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
Merge Join (cost=3.85..287880.35 rows=1771004 width=44) (actual time=0.091..3894.024 rows=1726970 loops=1)
Merge Cond: (thf.tb_hit_hitid = ths.tb_hit_hitid)
Buffers: shared hit=354821
-> Index Scan using tb_one_idx_2 on tb_one thf (cost=0.43..124322.43 rows=3230800 width=52) (actual time=0.014..655.036 rows=1726946 loops=1)
Buffers: shared hit=27201
-> Index Scan using tb_two_idx_2 on tb_two ths (cost=0.43..139531.97 rows=1771004 width=8) (actual time=0.069..1604.789 rows=1726973 loops=1)
Filter: ((source)::text = ANY ('{source1,source2}'::text[]))
Rows Removed by Filter: 1651946
Buffers: shared hit=327620
Planning time: 2.737 ms
Execution time: 4117.573 ms
(11 rows)
explain (analyze, verbose, buffers) select ...
, чтобы подтвердить это. - person Tometzky   schedule 17.04.2018