79,341
社区成员




我有一个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 ;
环境是