將逗號分割的clob欄位轉化為varchar,並將字串拆分為多行
阿新 • • 發佈:2019-02-10
SELECT '遠大' 企業名稱, sgpro.project_name 工程名稱, pur.product_type 產品類別, pur.product_name 產品名稱, pur.purchase_num 涉及數量, pur.product_regdate 材料進場時間, sgpro.build_licence 施工許可證號, sgpro.contractor_name 總包單位名稱 FROM t_purchase_info pur, t_allproject_info sgpro WHERE pur.project_id = sgpro.id AND pur.id in (select distinct regexp_substr(ids, '[^,]+', 1, level) project_id from (select dbms_lob.substr(project_ids) ids from g_enterprise_score where class in (3, 4) and score_type = 5 AND bad_no = 1 AND enterprise_id in (select distinct id from bemms.g_enterprise_info where name like '%遠大%') and project_ids is not null) connect by level <= length(dbms_lob.substr(ids)) - length(replace(dbms_lob.substr(ids), ',', '')) + 1 )
其中,將clob轉化為varchar,利用dbms_lob.substr,例:
select dbms_lob.substr(project_ids) from g_enterprise_score where class in (3, 4) and score_type = 5 AND bad_no = 1 AND enterprise_id in (select distinct id from bemms.g_enterprise_info where name like '%遠大%') and project_ids is not null ;
將一行字串(逗號分割)轉化多行,利用regexp_substr() connect by level,例:
select distinct regexp_substr(ids, '[^,]+', 1, level) project_id from (select dbms_lob.substr(project_ids) ids from g_enterprise_score where class in (3, 4) and score_type = 5 AND bad_no = 1 AND enterprise_id in (select distinct id from bemms.g_enterprise_info where name like '%遠大%') and project_ids is not null) connect by level <= length(dbms_lob.substr(ids)) - length(replace(dbms_lob.substr(ids), ',', '')) + 1
以上SQL執行效率特別低,我們歡歡大神說過: 越是高階的函式越有可能影響效率,還是最基礎的函式好,效率槓槓的。
其中儘量少用in,用exists,或者instr,經過優化後的SQL如下:
SELECT '北京XXX股份有限公司' 企業名稱,
sgpro.project_name 工程名稱,
pur.product_type 產品類別,
pur.product_name 產品名稱,
pur.purchase_num 涉及數量,
pur.product_regdate 材料進場時間,
sgpro.build_licence 施工許可證號,
sgpro.contractor_name 總包單位名稱
FROM t_purchase_info pur, t_allproject_info sgpro
WHERE pur.project_id = sgpro.id
AND instr((select ',' || wm_concat(dbms_lob.substr(g.project_ids)) || ',' ids
from g_enterprise_score g
where exists (select id
from bemms.g_enterprise_info e
where g.enterprise_id = e.id
and name like '%北京%')
and (g.class = 3 or g.class = 4)
and g.score_type = 5
AND g.bad_no = 1
and project_ids is not null),
',' || pur.id || ',') > 0