如何在postgreSql中优化查询请求
此请求需要99分钟才能执行。我需要给出一些结果。当我执行此请求时,我尝试使用索引和实体化视图,但这无法优化它:如何在postgreSql中优化查询请求,sql,postgresql,join,Sql,Postgresql,Join,此请求需要99分钟才能执行。我需要给出一些结果。当我执行此请求时,我尝试使用索引和实体化视图,但这无法优化它: select min(pointage_materiel.id_pointage) AS id, eds_societe.id_eds AS societe, eds_societe.code_eds AS code_societe, edsm.id_edsm AS id_parc, edsm.code_edsm AS n_p
select min(pointage_materiel.id_pointage) AS id,
eds_societe.id_eds AS societe, eds_societe.code_eds AS code_societe,
edsm.id_edsm AS id_parc,
edsm.code_edsm AS n_parc, edsm.lib_edsm AS
libelle_n_parc,pointage_materiel.date_periode AS
periode,
eds_chantier.id_eds AS id_chantier, eds_chantier.code_eds AS
code_chantier, eds_chantier.lib_eds
AS libelle_chantier,
eds_agence.code_eds AS code_agence,ctr_mecanique.code_ctr_mecan AS cm,
pointage_materiel.statut_pointage
from sigma02.edsm edsm
LEFT JOIN ( SELECT pointage.id_pointage,pointage.id_parc,
pointage.id_chantier,
pointage.statut_pointage,
to_char(pointage.date, 'MM-YYYY') AS date_periode FROM
sigma02.pointage) pointage_materiel
ON edsm.id_edsm = pointage_materiel.id_parc LEFT JOIN
sigma02.chantier ON
pointage_materiel.id_chantier = chantier.id_eds
LEFT JOIN sigma02.ctr_mecanique ON chantier.id_ctr_mecan =
ctr_mecanique.id_ctr_mecan
LEFT JOIN sigma02.eds eds_chantier ON pointage_materiel.id_chantier =
eds_chantier.id_eds
LEFT JOIN sigma02.eds eds_secteur ON eds_chantier.parent_id_eds =
eds_secteur.id_eds
LEFT JOIN sigma02.eds eds_agence ON eds_secteur.parent_id_eds =
eds_agence.id_eds
LEFT JOIN sigma02.eds eds_societe ON eds_agence.parent_id_eds =
eds_societe.id_eds
LEFT JOIN sigma02.eds on ctr_mecanique.id_societe=eds.id_eds
LEFT JOIN sigma02.soc_cop on eds.id_eds = soc_cop.eds
where edsm.statut = true AND pointage_materiel.statut_pointage != 'PR'
GROUP BY
pointage_materiel.statut_pointage,
pointage_materiel.date_periode,
eds_societe.id_eds,eds_societe.lib_eds,
eds_societe.code_eds, edsm.id_edsm,
eds_agence.code_eds,
edsm.code_edsm,
edsm.lib_edsm,
eds_chantier.id_eds,
eds_chantier.code_eds,
eds_chantier.lib_eds,
ctr_mecanique.code_ctr_mecan
Those are the index used to try to improve the request, I tried to use 1 index for each table.
````Index
```` EDSM INDEX
CREATE INDEX edsm_optim_id ON sigma02.edsm USING btree (id_edsm);
CREATE INDEX edsm_optim_stat ON sigma02.edsm USING btree (statut);
````EDSM INDEX
CREATE INDEX eds_optim_stat ON sigma02.eds USING btree(statut);
CREATE INDEX eds_optmi_id ON sigma02.eds USING btree (id_eds);
CREATE INDEX ids_optim_idtyp ON sigma02.eds USING btree(id_type_eds
COLLATE pg_catalog."default");
````CHANTIER INDEX
CREATE INDEX chant_optim_idctr ON sigma02.chantier USING btree
(id_ctr_mecan);
CREATE INDEX chant_optim_ideds ON sigma02.chantier USING btree (id_eds)
````CTR INDEX
CREATE INDEX ctrm_optim_id ON sigma02.ctr_mecanique USING btree
(id_ctr_mecan);
CREATE INDEX ctrm_optim_idsoc ON sigma02.ctr_mecanique USING btree
(id_societe);
````POINTAGE INDEX
CREATE INDEX point_optim_id ON sigma02.pointage USING btree
(id_pointage);
CREATE INDEX point_optim_idchant ON sigma02.pointage USING btree
(id_chantier);
CREATE INDEX point_optim_idparc ON sigma02.pointage USING btree
(id_parc);
CREATE INDEX point_optim_stat ON sigma02.pointage USING btree
(statut_pointage
COLLATE pg_catalog."default");
解释分析的结果还没有显示出来,需要很长时间
我如何优化它?有什么想法吗?救命啊!!!
谢谢你请发布404所说的
EXPLAIN ANALYZE
结果:请(通过点击下面的链接)将你的问题添加到EXPLAIN(ANALYZE,buffers,format text)
(不仅仅是一个“简单”的解释)生成的文本中,并确保防止计划缩进。粘贴文本,然后将`
放在计划前一行和计划后一行。还请包含相关表索引的完整create index
语句。当WHERE子句过滤掉任何“left only”行时,对“pointage\u materials”执行左连接没有意义。@jjanes如果我删除左连接,我会得到一些结果吗?