这是我的第二部分question https://stackoverflow.com/questions/49938650/table-indexing-on-postgresql-for-performance。
所以我有下表,
CREATE TABLE public.main_transaction
(
id integer NOT NULL DEFAULT nextval('main_transaction_id_seq'::regclass),
profile_id integer NOT NULL,
request_no character varying(18),
user_id bigint,
.....
CONSTRAINT main_transaction_pkey PRIMARY KEY (id),
CONSTRAINT fk_main_transaction_user_id FOREIGN KEY (user_id)
REFERENCES public.jhi_user (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
REFERENCES public.main_profile (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION DEFERRABLE INITIALLY DEFERRED,
CONSTRAINT main_transaction_profile_id_20_fk_main_profile_id FOREIGN KEY (profile_id)
REFERENCES public.main_profile (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION DEFERRABLE INITIALLY DEFERRED,
)
在表中,我的表中有外键,我正在使用CROSS JOIN
通过以下方式链接表格id它导致性能缓慢(返回结果12 secs对于超过 100 万行数据)。
更准确地说,我有一个profile_id
列于主要交易(上)表为profile表和在profile我有的表customer_id
for customer桌子。所以,我的查询如下,
SELECT * FROM main_transaction t
CROSS JOIN main_profile p
CROSS JOIN main_customer c
WHERE t.profile_id = p.id
AND p.user_id = c.id
AND ( upper(t.request_no) LIKE upper(concat('%','0-90-6 12 ','%'))
OR upper(c.phone) LIKE upper(concat('%','0-90-6 12','%'))
)
如果您需要的结构profile
and customer
表,我可以评论。
Explain:
"Merge Join (cost=27220.51..266464.85 rows=218 width=1692) (actual time=9399.370..9399.370 rows=0 loops=1)"
" Merge Cond: (t.profile_id = p.id)"
" Join Filter: ((upper((t.request_no)::text) ~~ upper(concat('%', ' 0-90-6 12 ', '%'))) OR (upper((c.phone)::text) ~~ upper(concat('%', ' 0-90-6 12, '%'))))"
" Rows Removed by Join Filter: 1089489"
" Buffers: shared hit=453158 read=413372, temp read=1560 written=1560"
" -> Index Scan using main_transaction_profile_id_idx on main_transaction t (cost=0.43..198177.36 rows=1089489 width=1455) (actual time=0.004..3913.501 rows=1089489 loops=1)"
" Buffers: shared hit=393656 read=410718"
" -> Materialize (cost=27218.84..27645.70 rows=85372 width=237) (actual time=165.565..239.572 rows=1133650 loops=1)"
" Buffers: shared hit=59502 read=2654, temp read=1560 written=1560"
" -> Sort (cost=27218.84..27432.27 rows=85372 width=237) (actual time=165.560..188.746 rows=85368 loops=1)"
" Sort Key: p.id"
" Sort Method: external sort Disk: 12480kB"
" Buffers: shared hit=59502 read=2654, temp read=1560 written=1560"
" -> Merge Join (cost=0.73..10594.24 rows=85372 width=237) (actual time=0.007..96.133 rows=85372 loops=1)"
" Merge Cond: (p.user_id = c.id)"
" Buffers: shared hit=59502 read=2654"
" -> Index Scan using main_profile_e8701ad4 on main_profile p (cost=0.29..3350.82 rows=85372 width=40) (actual time=0.003..24.892 rows=85372 loops=1)"
" Buffers: shared hit=29016 read=963"
" -> Index Scan using main_customer_pkey on main_customer c (cost=0.29..5963.16 rows=85506 width=197) (actual time=0.003..34.235 rows=85506 loops=1)"
" Buffers: shared hit=30486 read=1691"
"Planning time: 0.850 ms"
"Execution time: 9407.244 ms"