SELECT (CASE WHEN aa.Infor2 = 0 THEN '0.00' ELSE CONVERT (round(aa.Infor1/aa.Infor2, 2), VARCHAR) END) AS qztime1 FROM (SELECT (CASE WHEN sum(subfb.BUSI_UNAVAIL_DURA - subfb.IRRESPONSE_DURA) IS NULL THEN 0 ELSE sum(subfb.BUSI_UNAVAIL_DURA - subfb.IRRESPONSE_DURA) END) AS Infor1, count(DISTINCT fb.SHEET_STREAM_NO) AS Infor2 FROM EM_ORDER em, EE_ORDER_FAULT_SHEET fb, EE_BZ_SUB_FAULT_SHEET subfb WHERE fb.SHEET_STREAM_NO = subfb.SHEET_STREAM_NO AND fb.BUSI_TYPE = '11' AND subfb.END_SHEET_METHOD = '1' AND em.DEL_FLAG = '0' AND em.PROCESS_INST_ID > 0 AND em.ORDER_ID = fb.ORDER_ID AND subfb.REASON_DESC NOT LIKE '%海缆%' AND (subfb.FAULT_DUTY = '11') AND fb.CONCLUDE_TIME >= '2023-09-09' AND fb.CONCLUDE_TIME <= '2023-09-09' ) aa
时间: 2024-04-11 12:26:14 浏览: 169
根据你提供的 SQL 查询,以下是优化建议:
1. 使用显式的 JOIN 语法:替换使用逗号分隔的表名列表,使用显式的 JOIN 语法可以提高查询的可读性和维护性。例如,将子查询中的表连接改写为内连接:
```sql
FROM EM_ORDER em
JOIN EE_ORDER_FAULT_SHEET fb ON em.ORDER_ID = fb.ORDER_ID
JOIN EE_BZ_SUB_FAULT_SHEET subfb ON fb.SHEET_STREAM_NO = subfb.SHEET_STREAM_NO
```
2. 使用合适的索引:确保为涉及到连接和筛选条件的列上创建了适当的索引。在这个查询中,你可以考虑为 `EE_ORDER_FAULT_SHEET` 表的 `BUSI_TYPE` 列和 `CONCLUDE_TIME` 列,以及 `EE_BZ_SUB_FAULT_SHEET` 表的 `END_SHEET_METHOD` 列和 `FAULT_DUTY` 列创建索引。
3. 避免不必要的子查询:你可以将子查询中的聚合函数和计数函数移到外部查询中,以避免使用子查询。这样可以简化查询,并且可能提高性能。这是重写后的查询:
```sql
SELECT
CASE WHEN aa.Infor2 = 0 THEN '0.00'
ELSE CONVERT(ROUND(aa.Infor1 / aa.Infor2, 2), VARCHAR) END AS qztime1
FROM
(SELECT
CASE WHEN SUM(subfb.BUSI_UNAVAIL_DURA - subfb.IRRESPONSE_DURA) IS NULL THEN 0
ELSE SUM(subfb.BUSI_UNAVAIL_DURA - subfb.IRRESPONSE_DURA) END AS Infor1,
COUNT(DISTINCT fb.SHEET_STREAM_NO) AS Infor2
FROM
EM_ORDER em
JOIN EE_ORDER_FAULT_SHEET fb ON em.ORDER_ID = fb.ORDER_ID
JOIN EE_BZ_SUB_FAULT_SHEET subfb ON fb.SHEET_STREAM_NO = subfb.SHEET_STREAM_NO
WHERE
fb.BUSI_TYPE = '11'
AND subfb.END_SHEET_METHOD = '1'
AND em.DEL_FLAG = '0'
AND em.PROCESS_INST_ID > 0
AND subfb.REASON_DESC NOT LIKE '%海缆%'
AND subfb.FAULT_DUTY = '11'
AND fb.CONCLUDE_TIME >= '2023-09-09'
AND fb.CONCLUDE_TIME <= '2023-09-09'
) aa
```
这些是一些常见的优化建议,但具体的优化策略取决于你的数据库架构、数据量和性能需求。如果你能提供更多关于表结构、数据量和性能目标的信息,我可以为你提供更具体的优化建议。
阅读全文