У меня есть две таблицы, к которым я хочу присоединиться:
CREATE TABLE public."order" (
id uuid NOT NULL,
side varchar(4) NOT NULL,
product varchar(7) NOT NULL,
price numeric(18,8) NOT NULL,
close_time timestamp NULL,
CONSTRAINT order_pkey PRIMARY KEY (id)
);
CREATE TABLE public.order_history (
id serial NOT NULL,
amount numeric(18,8) NOT NULL,
"time" timestamp NOT NULL,
order_id uuid NOT NULL,
CONSTRAINT order_history_pkey PRIMARY KEY (id),
CONSTRAINT order_history_order_id_fkey FOREIGN KEY (order_id) REFERENCES "order"(id)
);
CREATE INDEX order_history_order_id ON public.order_history USING btree (order_id);
CREATE INDEX order_history_time_idx ON public.order_history USING btree ("time");
Мой запрос довольно прост, но он занимает буквально минуты на моем жестком диске (мой друг хранит одну и ту же БД на твердотельном накопителе и, очевидно,быстрее, но это все еще далеко за разумное время, я готов ждать):
select
"t1"."id",
"t1"."side",
"t1"."price",
"t1"."close_time",
"t2"."time",
"t2"."amount"
from
"order" as "t1"
inner join "order_history" as "t2" on
("t2"."order_id" = "t1"."id")
where
((("t2"."time" <= '2018-03-28 08:00:00')
and (("t1"."close_time" > '2018-03-28 07:00:00')
or ("t1"."close_time" is null)))
and ("t1"."product" = 'BTC-USD'))
order by
"t2"."time"
Вот вывод EXPLAIN(ANALYZE, BUFFERS)
:
Gather Merge (cost=3293333.15..3673129.97 rows=3255174 width=47) (actual time=195630.667..195668.246 rows=83766 loops=1)
Workers Planned: 2
Workers Launched: 2
Buffers: shared hit=346185 read=948128, temp read=402275 written=402089
-> Sort (cost=3292333.13..3296402.10 rows=1627587 width=47) (actual time=193748.573..193751.027 rows=27922 loops=3)
Sort Key: t2."time"
Sort Method: quicksort Memory: 4853kB
Buffers: shared hit=346185 read=948128, temp read=402275 written=402089
-> Hash Join (cost=1315861.90..3074345.01 rows=1627587 width=47) (actual time=65363.240..193703.738 rows=27922 loops=3)
Hash Cond: (t1.id = t2.order_id)
Buffers: shared hit=346172 read=948127, temp read=402275 written=402089
-> Parallel Seq Scan on "order" t1 (cost=0.00..1293501.00 rows=11021971 width=34) (actual time=0.122..78296.478 rows=8629896 loops=3)
Filter: (((close_time > '2018-03-28 07:00:00'::timestamp without time zone) OR (close_time IS NULL)) AND ((product)::text = 'BTC-USD'::text))
Rows Removed by Filter: 19019229
Buffers: shared hit=13 read=775079
-> Hash (cost=1079028.57..1079028.57 rows=12248346 width=29) (actual time=65107.372..65107.372 rows=12358141 loops=3)
Buckets: 524288 Batches: 32 Memory Usage: 27473kB
Buffers: shared hit=346071 read=173036, temp written=218295
-> Bitmap Heap Scan on order_history t2 (cost=229265.25..1079028.57 rows=12248346 width=29) (actual time=2951.352..61701.142 rows=12358141 loops=3)
Recheck Cond: ("time" <= '2018-03-28 08:00:00'::timestamp without time zone)
Heap Blocks: exact=139266
Buffers: shared hit=346071 read=173036
-> Bitmap Index Scan on order_history_time_idx (cost=0.00..226203.16 rows=12248346 width=0) (actual time=2925.500..2925.500 rows=12358141 loops=3)
Index Cond: ("time" <= '2018-03-28 08:00:00'::timestamp without time zone)
Buffers: shared hit=67539 read=33770
Planning time: 0.444 ms
Execution time: 195672.969 ms
Понятия не имеюпочему этот простой запрос такой медленный, мне удалось немного ускорить его, создав индекс для order_history.time, но это все.Любое предложение приветствуется!