请教explain的问题

csdncz01 2025-05-03 16:32:20

我有一个SQL效率很低,所以想用explain看一下原因和优化,但是执行很长时间不出结果,想问问explain不是从系统表里做统计分析吗?为什么运行不出结果,需要怎么处理和优化,脚本是:

explain select k2.calcdate,k2.city_branch_code,k2.town_branch_code,k2.town_branch_desc,主管人数, 基本法收入,主管基本法平均收入,总人数,税前总收入,税前总人数平均收入,FYC,总人数平均FYC,剔除新人考降人数,剔除新人考降税前收入,剔除新人考降FYC
from 
(
select DATE_TRUNC('month', CURRENT_DATE) - INTERVAL '2 months' as calcdate,city_branch_code,town_branch_code,town_branch_desc,
nvl((select count(*) from gxcost.cost_analysis_psnwage_coll_sz a,gxcost.cost_analysis_pid_range b,gxcost.cost_analysis_did_info c 
      where a.pid = b.pid and a.t05actrank SIMILAR to  '[EMD]%' and a.t00statdate= DATE_TRUNC('month', CURRENT_DATE) - INTERVAL '2 months' 
      and b.did = c.did and c.town_branch_code = k1.town_branch_code),0) 主管人数
,nvl((select sum(zyl+pyl+gll+grl+zgfa+dxl+fwl+m604+m605+m606+m607+m702+m703) 
  from gxcost.cost_analysis_psnwage_coll_sz a,gxcost.cost_analysis_pid_range b,gxcost.cost_analysis_did_info c 
  where a.pid = b.pid and a.t05actrank SIMILAR to  '[EMD]%' and a.t00statdate= DATE_TRUNC('month', CURRENT_DATE) - INTERVAL '2 months' 
  and b.did = c.did and c.town_branch_code = k1.town_branch_code),0) 基本法收入
,case when nvl((select count(*) from gxcost.cost_analysis_psnwage_coll_sz a,gxcost.cost_analysis_pid_range b,gxcost.cost_analysis_did_info c 
      where a.pid = b.pid and a.t05actrank SIMILAR to  '[EMD]%' and a.t00statdate= DATE_TRUNC('month', CURRENT_DATE) - INTERVAL '2 months' 
      and b.did = c.did and c.town_branch_code = k1.town_branch_code),0) != 0 then 
round(nvl((select sum(zyl+pyl+gll+grl+zgfa+dxl+fwl+m604+m605+m606+m607+m702+m703) 
  from gxcost.cost_analysis_psnwage_coll_sz a,gxcost.cost_analysis_pid_range b,gxcost.cost_analysis_did_info c 
  where a.pid = b.pid and a.t05actrank SIMILAR to  '[EMD]%' and a.t00statdate= DATE_TRUNC('month', CURRENT_DATE) - INTERVAL '2 months' 
  and b.did = c.did and c.town_branch_code = k1.town_branch_code),0) /
nvl((select count(*) from gxcost.cost_analysis_psnwage_coll_sz a,gxcost.cost_analysis_pid_range b,gxcost.cost_analysis_did_info c 
      where a.pid = b.pid and a.t05actrank SIMILAR to  '[EMD]%' and a.t00statdate= DATE_TRUNC('month', CURRENT_DATE) - INTERVAL '2 months' 
      and b.did = c.did and c.town_branch_code = k1.town_branch_code),0),2) else 0 end 主管基本法平均收入
from (
select distinct city_branch_code,town_branch_code,concat(city_branch_desc,town_branch_desc) as town_branch_desc from gxcost.cost_analysis_did_info where town_branch_code is not null
)  k1
) k2,
(
select DATE_TRUNC('month', CURRENT_DATE) - INTERVAL '2 months' as calcdate,city_branch_code,town_branch_code,town_branch_desc,
nvl((select count(*) from gxcost.cost_analysis_psnwage_coll_sz a,gxcost.cost_analysis_pid_range b,gxcost.cost_analysis_did_info c 
      where a.pid = b.pid and a.t00statdate= DATE_TRUNC('month', CURRENT_DATE) - INTERVAL '2 months' 
      and b.did = c.did and c.town_branch_code = k1.town_branch_code) ,0) 总人数
,nvl((select sum(m980) from gxcost.cost_analysis_psnwage_coll_sz a,gxcost.cost_analysis_pid_range b,gxcost.cost_analysis_did_info c 
      where a.pid = b.pid and a.t00statdate= DATE_TRUNC('month', CURRENT_DATE) - INTERVAL '2 months' 
      and b.did = c.did and c.town_branch_code = k1.town_branch_code) ,0) 税前总收入
,case when nvl((select count(*) from gxcost.cost_analysis_psnwage_coll_sz a,gxcost.cost_analysis_pid_range b,gxcost.cost_analysis_did_info c 
      where a.pid = b.pid and a.t00statdate= DATE_TRUNC('month', CURRENT_DATE) - INTERVAL '2 months' 
      and b.did = c.did and c.town_branch_code = k1.town_branch_code) ,0) !=0 then 
round(nvl((select sum(m980) from gxcost.cost_analysis_psnwage_coll_sz a,gxcost.cost_analysis_pid_range b,gxcost.cost_analysis_did_info c 
      where a.pid = b.pid and a.t00statdate= DATE_TRUNC('month', CURRENT_DATE) - INTERVAL '2 months' 
      and b.did = c.did and c.town_branch_code = k1.town_branch_code) ,0) / 
nvl((select count(*) from gxcost.cost_analysis_psnwage_coll_sz a,gxcost.cost_analysis_pid_range b,gxcost.cost_analysis_did_info c 
      where a.pid = b.pid and a.t00statdate= DATE_TRUNC('month', CURRENT_DATE) - INTERVAL '2 months' 
      and b.did = c.did and c.town_branch_code = k1.town_branch_code) ,0),2)  else 0 end 税前总人数平均收入 
from (
select distinct city_branch_code,town_branch_code,concat(city_branch_desc,town_branch_desc) as town_branch_desc from gxcost.cost_analysis_did_info where town_branch_code is not null
)  k1
) k3,
(
select DATE_TRUNC('month', CURRENT_DATE) - INTERVAL '2 months'  as calcdate,city_branch_code,town_branch_code,town_branch_desc,
nvl((select sum(t05firstcommision) from gxcost.cost_analysis_psnwage_coll_sz a,gxcost.cost_analysis_pid_range b,gxcost.cost_analysis_did_info c 
      where a.pid = b.pid and a.t00statdate= DATE_TRUNC('month', CURRENT_DATE) - INTERVAL '2 months' 
      and b.did = c.did and c.town_branch_code = k1.town_branch_code) ,0) FYC
,case when nvl((select count(*) from gxcost.cost_analysis_psnwage_coll_sz a,gxcost.cost_analysis_pid_range b,gxcost.cost_analysis_did_info c 
      where a.pid = b.pid and a.t00statdate= DATE_TRUNC('month', CURRENT_DATE) - INTERVAL '2 months' 
      and b.did = c.did and c.town_branch_code = k1.town_branch_code) ,0) != 0 then 
round(nvl((select sum(t05firstcommision) from gxcost.cost_analysis_psnwage_coll_sz a,gxcost.cost_analysis_pid_range b,gxcost.cost_analysis_did_info c 
      where a.pid = b.pid and a.t00statdate= DATE_TRUNC('month', CURRENT_DATE) - INTERVAL '2 months' 
      and b.did = c.did and c.town_branch_code = k1.town_branch_code) ,0) / 
nvl((select count(*) from gxcost.cost_analysis_psnwage_coll_sz a,gxcost.cost_analysis_pid_range b,gxcost.cost_analysis_did_info c 
      where a.pid = b.pid and a.t00statdate= DATE_TRUNC('month', CURRENT_DATE) - INTERVAL '2 months' 
      and b.did = c.did and c.town_branch_code = k1.town_branch_code) ,0),2)   else 0 end 总人数平均FYC 
from (
select distinct city_branch_code,town_branch_code,concat(city_branch_desc,town_branch_desc) as town_branch_desc from gxcost.cost_analysis_did_info where town_branch_code is not null
)  k1
) k4,
(
select DATE_TRUNC('month', CURRENT_DATE) - INTERVAL '2 months'  as calcdate,city_branch_code,town_branch_code,town_branch_desc,
nvl((select count(*) from gxcost.cost_analysis_psnwage_coll_sz a,gxcost.cost_analysis_pid_range b,gxcost.cost_analysis_did_info c 
      where a.pid = b.pid and a.t05actrank not in ('A1','A9') and a.t00statdate= DATE_TRUNC('month', CURRENT_DATE) - INTERVAL '2 months' 
      and b.did = c.did and c.town_branch_code = k1.town_branch_code) ,0) 剔除新人考降人数
,nvl((select sum(m980) from gxcost.cost_analysis_psnwage_coll_sz a,gxcost.cost_analysis_pid_range b,gxcost.cost_analysis_did_info c 
      where a.pid = b.pid and a.t05actrank not in ('A1','A9') and a.t00statdate= DATE_TRUNC('month', CURRENT_DATE) - INTERVAL '2 months' 
      and b.did = c.did and c.town_branch_code = k1.town_branch_code) ,0) 剔除新人考降税前收入
,nvl((select sum(t05firstcommision) from gxcost.cost_analysis_psnwage_coll_sz a,gxcost.cost_analysis_pid_range b,gxcost.cost_analysis_did_info c 
      where a.pid = b.pid and a.t05actrank not in ('A1','A9') and a.t00statdate= DATE_TRUNC('month', CURRENT_DATE) - INTERVAL '2 months' 
      and b.did = c.did and c.town_branch_code = k1.town_branch_code) ,0) 剔除新人考降FYC
from (
select distinct city_branch_code,town_branch_code,concat(city_branch_desc,town_branch_desc) as town_branch_desc from gxcost.cost_analysis_did_info where town_branch_code is not null
)  k1
) k5
where k2.calcdate = k3.calcdate and k2.calcdate = k4.calcdate and k2.calcdate = k5.calcdate and
   k2.town_branch_code = k3.town_branch_code and k2.town_branch_code = k4.town_branch_code and k2.town_branch_code = k5.town_branch_code ;
   
环境是

 

...全文
113 回复 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

79,341

社区成员

发帖
与我相关
我的任务
社区描述
汇集数据库的爱好者和关注者,大家共同学习、探索、分享数据库前沿知识和技术,像松鼠一样剥开科学的坚果;交流Gauss及其他数据库的使用心得和经验,互助解决问题,共建数据库技术交流圈。
数据库数据仓库 企业社区 北京·海淀区
社区管理员
  • Gauss松鼠会
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

欢迎大家同时关注Gauss松鼠会专家酷哥。

https://www.zhihu.com/people/ku-ge-78-98

试试用AI创作助手写篇文章吧