为提高效率,提问时请提供以下信息,问题描述清晰可优先响应。
【DM版本】:dms7
【操作系统】:
【CPU】:6
【问题描述】多个表用left join 关联导致查询速度慢如何优化,其中个别表数据量超千万级,该表已创建索引和子查询,目前查询速度在5分钟左右
数据库查询语句:
SELECT distinct
aa.sort_order as valuename,
a.valid_date,
a.invalid_date,
a.meter_id,
a.meter_logic_id,
a.meter_id as meter_no,
a.dev_no,
a.dev_no as name,
a.dev_no AS ABBREVIATION,
---c.dev_stat as sta1,
---c.dev_stat as sta2,
d.inst_usage_cls,
'0021' AS DATASOURCE_ID,
TO_CHAR(SYSDATE,'YYYY-MM-DD HH:mm:ss') AS STAMP2,
TO_CHAR(SYSDATE,'YYYY-MM-DD') AS UPDATE_TIME,
e.owner,
e.stamp as stamp_t,
f.feeder_id,
f.id as ID2,
k.dcloud_id as ID3,
nn.dcloud_id as ID4,
mm.dcloud_id as ID5
from (select mgt_org_code,cust_id,valid_date,invalid_date,meter_id,dev_no,inst_id,meter_logic_id from DSTBPV.meter_run where mgt_org_code like '13404%' and ifnull(hw_op_type,'W') != 'D') as a
left join (select primary_key_column,mgt_org_code,sort_order from ty_mxdr.temp_table_meter where mgt_org_code like '13404%' ) as aa on aa.primary_key_column = a.meter_logic_id
left join (select cust_id,mgt_org_code,elec_cons_cust_id,cust_cls from DSTBPV.ELEC_CONS_CUST where mgt_org_code like '13404%' and ifnull(hw_op_type,'W') != 'D') as n on n.cust_id = a.cust_id
left join ty_mxdr.sg_org_dcustomer_c nn on nn.cms_id = n.elec_cons_cust_id
left join (select ctnr_asset_id,dev_id,inst_id,hw_op_type
from ( select ctnr_asset_id,dev_id,inst_id,hw_op_type,row_number () over (partition by dev_id order by ctnr_asset_id desc) as rn from DSTBPV.DEV_IN_METER_CTNR where ifnull(hw_op_type,'W') != 'D' and dev_instal_date is not null) subquery where rn =1 ) b on b.dev_id = a.meter_id
left join ty_mxdr.sg_con_dlmeasuringbox_c k on k.cms_id = b.ctnr_asset_id
left join DSTBPV.GPC m on m.cust_id = a.cust_id
left join ty_mxdr.sg_con_dplant_c mm on mm.cms_id = m.gpc_id
---left join DSTBPV.DEV c on b.dev_id = c.dev_id
left join (select inst_id,dist_sta_id,inst_usage_cls from DSTBPV.INST_ELEC_CONS where ifnull(hw_op_type,'W') != 'D') d on d.inst_id = a.inst_id
left join (select dist_sta_id,adj_volt_dev_asset_id,adj_volt_dev_id from
(select dist_sta_id,adj_volt_dev_asset_id,adj_volt_dev_id,row_number () over (partition by dist_sta_id order by adj_volt_dev_id desc) as rm from DSTBPV.ADJ_VOLT_DEV where publ_clg_flag = 01) subquery where rm =1 ) as g on g.dist_sta_id = d.dist_sta_id
left join DSTBPV.ADJ_VOLT_DEV_ASSET h on h.adj_volt_dev_asset_id = g.adj_volt_dev_asset_id
left join (select replace(pms_psr_id ,'','-') as pms_psr_id,dcloud_id,owner,substr(stamp,1,22) as stamp from
(select replace(pms_psr_id ,'','-') as pms_psr_id,dcloud_id,owner,substr(stamp,1,22) as stamp,row_number () over (partition by pms_psr_id order by dcloud_id desc) as rnn from TY_MXDR.sg_dev_dpwrtransfm_c) subquery where rnn =1 ) as e on e.pms_psr_id = h.pms_equip_id
left join ty_mxdr.sg_dev_dpwrtransfm_b f on e.dcloud_id = f.id
where e.pms_psr_id is not null
先看看这个主表有多少记录
select mgt_org_code,
cust_id,
valid_date,
invalid_date,
meter_id,
dev_no,
inst_id,
meter_logic_id
from DSTBPV.meter_run
where mgt_org_code like '13404%'
and ifnull(hw_op_type,'W') != 'D'
还有mgt_org_code
列是是否有索引(作为第一个列)?
一般来说复杂长语句都是从最小单位SQL开始优化,再结合整体SQL执行计划查看。
相关表结构的SQL附件可以打包上传下,方便具体查看。