SQL优化以下语句(select f.file_name,a.content_id,c.fd_objectid level_id,c.level level_val,e.fd_objectid manage_id, ifnull((select count(fd_objectid) from message_receiver where MESSAGE_ID = e.fd_objectid), 0) SEND_PEOPLE_NUM, ifnull((select sum(case when reply_content is not null and reply_content != '' then 1 else 0 end) from message_receiver where MESSAGE_ID = e.fd_objectid), 0) reply_num, ifnull((select count(fd_objectid) from (select * from (select *,row_number() over(partition by seq,sendee_tel order by call_stat desc) flag from GROUPCALL_DETAILS) where flag = '1') where busi_id like concat('%', a.content_id) and busi_id like concat(a.event_id, '%')), 0) call_all, ifnull((select sum(case when call_stat like '%0%' then 1 else 0 end) from (select * from (select *,row_number() over(partition by seq,sendee_tel order by call_stat desc) flag from GROUPCALL_DETAILS) where flag = '1') where busi_id like concat('%', a.content_id) and busi_id like concat(a.event_id, '%')), 0) call_jt from NWYJ_SERVICE.ECM_EMYA_ORDER a left join MAP_EMEC_PLAN_CONTENT b on b.FD_OBJECTID = a.CONTENT_ID left join MAP_EMEC_PLAN c on c.FD_OBJECTID = b.RELATION_ID left join MAP_EMEC_ORG_RELATION d on d.FD_OBJECTID = b.ORG_RELATION_ID left join MESSAGE_MANAGE e on e.BUSI_ID = a.FD_OBJECTID left join MAP_EMEC_PLAN_ORG_TREE f on f.fd_objectid = d.org_id where a.event_id = #{eventId} and a.is_del = '0' and b.is_del = '0' and c.is_del = '0' and d.is_del = '0' and f.is_del = '0' and c.fd_objectid = #{levelId} and e.fd_objectid is not null)
时间: 2024-04-19 15:23:27 浏览: 219
以下是对给定的SQL语句进行优化的建议:
1. 使用连接查询代替子查询:将子查询转换为连接查询可以提高查询性能。可以使用LEFT JOIN来连接子查询的结果。
2. 使用COUNT()和SUM()函数的条件表达式:在使用COUNT()和SUM()函数时,可以使用条件表达式替代子查询,从而减少子查询的数量。
3. 使用预编译语句:使用预编译语句可以提高SQL的执行效率,并且可以防止SQL注入攻击。
4. 添加索引:根据查询中的条件列,可以考虑添加索引以提高查询性能。
下面是优化后的SQL语句:
```sql
SELECT
f.file_name, a.content_id, c.fd_objectid AS level_id, c.level AS level_val, e.fd_objectid AS manage_id,
IFNULL((
SELECT COUNT(fd_objectid)
FROM message_receiver
WHERE MESSAGE_ID = e.fd_objectid
), 0) AS SEND_PEOPLE_NUM,
IFNULL((
SELECT SUM(CASE WHEN reply_content IS NOT NULL AND reply_content != '' THEN 1 ELSE 0 END)
FROM message_receiver
WHERE MESSAGE_ID = e.fd_objectid
), 0) AS reply_num,
IFNULL((
SELECT COUNT(fd_objectid)
FROM (
SELECT *, ROW_NUMBER() OVER(PARTITION BY seq, sendee_tel ORDER BY call_stat DESC) flag
FROM GROUPCALL_DETAILS
) AS temp
WHERE flag = '1' AND busi_id LIKE CONCAT('%', a.content_id) AND busi_id LIKE CONCAT(a.event_id, '%')
), 0) AS call_all,
IFNULL((
SELECT SUM(CASE WHEN call_stat LIKE '%0%' THEN 1 ELSE 0 END)
FROM (
SELECT *, ROW_NUMBER() OVER(PARTITION BY seq, sendee_tel ORDER BY call_stat DESC) flag
FROM GROUPCALL_DETAILS
) AS temp
WHERE flag = '1' AND busi_id LIKE CONCAT('%', a.content_id) AND busi_id LIKE CONCAT(a.event_id, '%')
), 0) AS call_jt
FROM NWYJ_SERVICE.ECM_EMYA_ORDER a
LEFT JOIN MAP_EMEC_PLAN_CONTENT b ON b.FD_OBJECTID = a.CONTENT_ID
LEFT JOIN MAP_EMEC_PLAN c ON c.FD_OBJECTID = b.RELATION_ID
LEFT JOIN MAP_EMEC_ORG_RELATION d ON d.FD_OBJECTID = b.ORG_RELATION_ID
LEFT JOIN MESSAGE_MANAGE e ON e.BUSI_ID = a.FD_OBJECTID
LEFT JOIN MAP_EMEC_PLAN_ORG_TREE f ON f.fd_objectid = d.org_id
WHERE a.event_id = #{eventId}
AND a.is_del = '0'
AND b.is_del = '0'
AND c.is_del = '0'
AND d.is_del = '0'
AND f.is_del = '0'
AND c.fd_objectid = #{levelId}
AND e.fd_objectid IS NOT NULL
```
请注意,这些优化建议仅供参考,实际效果可能因数据库结构和数据量而有所不同。建议在进行任何更改之前,在测试环境中进行充分的测试和评估。
阅读全文