优化下面sql select count(distinct sheet.ORDER_ID ) as num, sub.DEAL_PROV_NAME as PROVINCE_NAME from ee_order_fault_sheet sheet, ee_order_sub_sheet sub, EE_BZ_SUB_FAULT_SHEET jd where sheet.ORDER_ID = sub.ORDER_ID and sheet.ORDER_ID = jd.ORDER_ID and jd.FAULT_OCCUR_PERIOD='3' and sheet.SHEET_STATUS in('113','115') and sub.SHEET_STATUS !='206' and jd.END_SHEET_METHOD = '1' and sub.DEAL_PROV_NAME != '' and sheet.CONCLUDE_TIME between '2023-06-01 00:00:00' and '2023-06-07 00:00:00' group by sub.DEAL_PROV_NAME
时间: 2024-03-11 15:49:31 浏览: 73
可以考虑以下优化:
1. 使用JOIN替代多表查询:
```
SELECT COUNT(DISTINCT sheet.ORDER_ID) AS num,
sub.DEAL_PROV_NAME AS PROVINCE_NAME
FROM ee_order_fault_sheet sheet
JOIN ee_order_sub_sheet sub ON sheet.ORDER_ID = sub.ORDER_ID
JOIN EE_BZ_SUB_FAULT_SHEET jd ON sheet.ORDER_ID = jd.ORDER_ID
WHERE jd.FAULT_OCCUR_PERIOD = '3'
AND sheet.SHEET_STATUS IN ('113', '115')
AND sub.SHEET_STATUS != '206'
AND jd.END_SHEET_METHOD = '1'
AND sub.DEAL_PROV_NAME != ''
AND sheet.CONCLUDE_TIME BETWEEN '2023-06-01 00:00:00' AND '2023-06-07 00:00:00'
GROUP BY sub.DEAL_PROV_NAME
```
2. 为表中的列添加索引,以加速查询:
```
CREATE INDEX idx_order_id ON ee_order_fault_sheet (ORDER_ID);
CREATE INDEX idx_sheet_status ON ee_order_fault_sheet (SHEET_STATUS);
CREATE INDEX idx_conclude_time ON ee_order_fault_sheet (CONCLUDE_TIME);
CREATE INDEX idx_order_id_sub ON ee_order_sub_sheet (ORDER_ID);
CREATE INDEX idx_sheet_status_sub ON ee_order_sub_sheet (SHEET_STATUS);
CREATE INDEX idx_deal_prov_name ON ee_order_sub_sheet (DEAL_PROV_NAME);
CREATE INDEX idx_order_id_jd ON EE_BZ_SUB_FAULT_SHEET (ORDER_ID);
CREATE INDEX idx_fault_occur_period ON EE_BZ_SUB_FAULT_SHEET (FAULT_OCCUR_PERIOD);
CREATE INDEX idx_end_sheet_method ON EE_BZ_SUB_FAULT_SHEET (END_SHEET_METHOD);
```
阅读全文