执行下列sql语句用时37分钟,这个语句怎么优化呢?
=======================================================================================
SELECT ri.rep_id AS rep_ite_rep_id, rd.rep_ite_id, ri.rep_ite_typ_id, rd.rep_ite_sub_typ_id, ri.sca_side, rd.blob_data, rd.num_data1, rd.num_data2, rd.num_data3
FROM report_items ri
JOIN (SELECT rep_ite_num_id AS rep_ite_id, rep_ite_num_typ_id AS rep_ite_sub_typ_id,
NULL AS blob_data, rep_ite_num_value1 AS num_data1, rep_ite_num_value2 AS num_data2, rep_ite_num_value3 AS num_data3
FROM report_item_numerics
WHERE rep_ite_num_typ_id IN (1,2,3,4,5,7,9,36,45)
UNION
SELECT rep_ite_sur_id AS rep_ite_id, rep_ite_sur_typ_id AS rep_ite_sub_typ_id,
rep_ite_sur_value AS blob_data, NULL AS num_data1, NULL AS num_data2, NULL AS num_data3
FROM report_item_surfaces
WHERE rep_ite_sur_typ_id IN (4,1,5,2,7,11,22,23,20,21)) AS rd ON (ri.rep_ite_id = rd.rep_ite_id)
WHERE ri.rep_id = 7492 AND ri.rep_ite_typ_id != 2
=======================================================================================
=======================================================================================
SELECT ri.rep_id AS rep_ite_rep_id, rd.rep_ite_id, ri.rep_ite_typ_id, rd.rep_ite_sub_typ_id, ri.sca_side, rd.blob_data, rd.num_data1, rd.num_data2, rd.num_data3
FROM report_items ri
JOIN (SELECT rep_ite_num_id AS rep_ite_id, rep_ite_num_typ_id AS rep_ite_sub_typ_id,
NULL AS blob_data, rep_ite_num_value1 AS num_data1, rep_ite_num_value2 AS num_data2, rep_ite_num_value3 AS num_data3
FROM report_item_numerics
WHERE rep_ite_num_typ_id IN (1,2,3,4,5,7,9,36,45)
UNION
SELECT rep_ite_sur_id AS rep_ite_id, rep_ite_sur_typ_id AS rep_ite_sub_typ_id,
rep_ite_sur_value AS blob_data, NULL AS num_data1, NULL AS num_data2, NULL AS num_data3
FROM report_item_surfaces
WHERE rep_ite_sur_typ_id IN (4,1,5,2,7,11,22,23,20,21)) AS rd ON (ri.rep_ite_id = rd.rep_ite_id)
WHERE ri.rep_id = 7492 AND ri.rep_ite_typ_id != 2
=======================================================================================
解决方案
40
没什么好方法,两个你可以尝试下。
1、中间UNION的两张表结果建临时表
2、查询条件内置,例如ri.rep_ite_typ_id != 2放到 rd的union查询里去..减少union的结果集..
没有其他好的办法~
1、中间UNION的两张表结果建临时表
2、查询条件内置,例如ri.rep_ite_typ_id != 2放到 rd的union查询里去..减少union的结果集..
没有其他好的办法~
40
你看看这句能查出多少行数据
SELECT ri.rep_id AS rep_ite_rep_id, rd.rep_ite_id, ri.rep_ite_typ_id, rd.rep_ite_sub_typ_id, ri.sca_side, rd.blob_data, rd.num_data1, rd.num_data2, rd.num_data3 FROM report_items ri WHERE ri.rep_id = 7492 AND ri.rep_ite_typ_id != 2;