Unable to improve query performance in postgresql

安稳与你 提交于 2020-03-04 17:10:19

问题


I am trying to join 9 tables together. The count and index of each tables are given below along with the query. Green color in screenshot indicates keys used to join. But please note that I have used another column for visit_occurrence table called visit_occurrence_id to join but it's not indexed

DROP MATERIALIZED VIEW IF EXISTS cdm.dummy CASCADE;
CREATE MATERIALIZED VIEW cdm.dummy as
select
f.person_id,f.gender_id
from cdm.visit_occurrence a
left join 
cdm.condition_occurrence b
on a.person_id = b.person_id and a.visit_occurrence_id = 
b.visit_occurrence_id
left join 
cdm.measurement c
on a.person_id = c.person_id and a.visit_occurrence_id = 
c.visit_occurrence_id
left join 
cdm.drug_exposure d
on a.person_id = d.person_id and a.visit_occurrence_id = 
d.visit_occurrence_id
left join 
cdm.procedure_occurrence e
on a.person_id = e.person_id and a.visit_occurrence_id = 
e.visit_occurrence_id
left join 
cdm.person f
on a.person_id = f.person_id 
left join
cdm.observation g
on a.person_id = g.person_id and a.visit_occurrence_id = 
g.visit_occurrence_id
left join
cdm.observation_period h
on a.person_id = g.person_id
left join
cdm.death i
on a.person_id = i.person_id 

explain output

explain outpt with enable_nestloop = off;

Please note that visit_occurrence is the base table. I pick the columns person_id and visit_occurrence_id from visit_occurrence table to join with other tables as shown in the query. I see that visit_occurrence_id used to join (from base table) with other tables isn't a index column (in base table).

a) Is this the reason for slow performance because it's a base table? But in all other tables, the joining keys are used as index as shown in screenshot above(green color indicates - keys used to join)

b) Are the records count an issue?

Can you help me adapt my query to fix this?

Its been running for more than 5-6 hours but not output yet.

Any help is much appreciated. Will be really helpful

来源:https://stackoverflow.com/questions/58409824/unable-to-improve-query-performance-in-postgresql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!