postgresql pgsql查询导致性能问题

hmae6n7t  于 2022-11-23  发布在  PostgreSQL
关注(0)|答案(1)|浏览(269)

我对PostgreSQL示例的查询需要很长时间才能检索到结果。在我们的应用程序代码中,此查询每30分钟运行一次。

select fgh_dk,
       (select dmb
        from fgh_tonee
        where id=fgh_dk),
       ghk_nm,
       SUM(hjlm_sup) as mgh_klmno
from yunm
where fgh_dk is not null
group by fgh_dk, ghk_nm
order by fgh_dk,ghk_nm;

请建议我们重写此查询的最佳方式。

at0kjp5o

at0kjp5o1#

通常,如果不查看EXPLAIN (ANALYZE, BUFFERS)输出,就无法回答此类问题。但是,如果取消子查询,查询的性能一定会更好:

select yunm.fgh_dk,
       fgh_tonee.dmb,
       yunm.ghk_nm,
       SUM(yunm.hjlm_sup) as mgh_klmno
from yunm
   left join fgh_tonee
      on fgh_tonee.id = yunm.fgh_dk
where yunm.fgh_dk is not null
group by yunm.fgh_dk, yunm.ghk_nm, fgh_tonee.dmb
order by yunm.fgh_dk, yunm.ghk_nm, fgh_tonee.dmb;

相关问题