【问题标题】:Slow running query, Postgresql运行缓慢的查询,Postgresql
【发布时间】:2018-02-19 22:41:17
【问题描述】:

我有一个非常慢的查询(30 多分钟或更长时间),我认为可以通过更有效的编码来加快速度。下面是产生的代码和查询计划。因此,我正在寻找答案以加快在大型表上执行多个联接的查询。

drop table if exists totalshad;
create temporary table totalshad as
select pricedate, hour, sum(cast(price as numeric)) as totalprice from 
pjm.rtcons
where
rtcons.pricedate >= '2017-12-01'
--  and
--  rtcons.pricedate <= '2018-01-23'
group by pricedate, hour
order by pricedate, hour;
-----------------------------
drop table if exists percshad;
create temporary table percshad as
select totalshad.pricedate, totalshad.hour, facility, round(sum(cast(price 
as numeric)),2) as cons_shad, round(sum(cast(totalprice as numeric)),2) as 
total_shad, round(cast(price/totalprice as numeric),4) as per_shad from 
totalshad
join pjm.rtcons on
rtcons.pricedate = totalshad.pricedate
and
rtcons.hour = totalshad.hour
and
facility = 'ETOWANDA-NMESHOPP ETL 1057  A  115 KV'
where totalprice <> 0 and totalshad.pricedate > '2017-12-01'
group by totalshad.pricedate, totalshad.hour, facility, 
(price/totalprice)
order by per_shad desc
limit 5;

EXPLAIN select facility, percshad.pricedate, percshad.hour, per_shad, 
minmcc.rtmcc, minnode.nodename, maxmcc.rtmcc, maxnode.nodename from percshad
join pjm.prices minmcc on
minmcc.pricedate = percshad.pricedate
and
minmcc.hour = percshad.hour
and
minmcc.rtmcc = (select min(rtmcc) from pjm.prices where pricedate = 
percshad.pricedate and hour = percshad.hour)
join pjm.nodes minnode on
minnode.node_id = minmcc.node_id
join pjm.prices maxmcc on
maxmcc.pricedate = percshad.pricedate
and
maxmcc.hour = percshad.hour
and
maxmcc.rtmcc = (select max(rtmcc) from pjm.prices where pricedate = 
percshad.pricedate and hour = percshad.hour)
join pjm.nodes maxnode on
maxnode.node_id = maxmcc.node_id
order by per_shad desc
limit 5

这里是解释输出:

更新:我现在已将代码简化为以下内容。但是从 EXPLAIN 中可以看出,在最后一个 select 语句中找到 node_id 仍然需要很长时间

 drop table if exists totalshad;
create temporary table totalshad as
select pricedate, hour, sum(cast(price as numeric)) as totalprice from 
pjm.rtcons
where
rtcons.pricedate >= '2017-12-01'
--  and
--  rtcons.pricedate <= '2018-01-23'
group by pricedate, hour
order by pricedate, hour;
-----------------------------
drop table if exists percshad;
create temporary table percshad as
select totalshad.pricedate, totalshad.hour, facility, round(sum(cast(price 
as numeric)),2) as cons_shad, round(sum(cast(totalprice as numeric)),2) as 
total_shad,
round(cast(price/totalprice as numeric),4) as per_shad from totalshad
join pjm.rtcons on
rtcons.pricedate = totalshad.pricedate
and
rtcons.hour = totalshad.hour
and
facility = 'ETOWANDA-NMESHOPP ETL 1057  A  115 KV'
where totalprice <> 0 and totalshad.pricedate > '2017-12-01'
group by totalshad.pricedate, totalshad.hour, facility, (price/totalprice)
order by per_shad desc
limit 5;

drop table if exists mincong;
create temporary table mincong as
select pricedate, hour, min(rtmcc) as rtmcc
from pjm.prices JOIN percshad USING (pricedate, hour)
group by pricedate, hour;

EXPLAIN select distinct on (pricedate, hour) prices.node_id from mincong 
JOIN pjm.prices USING (pricedate, hour, rtmcc)
group by pricedate, hour, node_id

【问题讨论】:

  • 我会尝试的第一件事是将索引放在percshad 上。 (是的,您可以使用临时表来做到这一点。)
  • 使用临时表可能无法加快速度。
  • 告诉我们您的查询中索引了哪些列
  • 对于未来的问题:请不要将计划显示为图像(甚至不显示完整计划)。请使用Formatted text,no screen shots。 explain (analyze, buffers) 的输出通常更有帮助。您也可以将计划上传至explain.depesz.com
  • 使用 PgAdmin4,当我按 F7 进行解释时,我只得到:“drop”处或附近的语法错误。此外,我无法复制和粘贴解释输出。因此我需要使用屏幕截图。如果有其他方法,我会全神贯注。

标签: sql postgresql performance


【解决方案1】:

问题是连接条件中的子选择;必须为加入的每一行执行它们。

如果您无法摆脱它们,请尝试创建一个索引以尽可能好地支持子选择:

CREATE INDEX ON pjm.prices(pricedate, hour, rtmcc);

【讨论】:

  • 加入子选择而不是在加入条件下使用它会有所帮助吗?
  • 使用子选择连接通常比在连接条件下使用要好得多。
  • @LaurenzAlbe 我试着采纳你的建议。它仍然运行缓慢。请参阅我更新的问题。另外,我不能索引 pjm.prices,因为我不是所有者。
  • 嗯,可以让DBA创建索引。但是对于散列连接,您不需要它。您应该在使用临时表之前ANALYZE。可以提供EXPLAIN (ANALYZE, BUFFERS) 输出吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-11-06
  • 2021-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-01
相关资源
最近更新 更多