优化sql select cir.ID, cir.NAME, mebm.type, msg.persion_num, artic.TITLE AS ARTICLE_TITLE, ( CASE WHEN top_msg.top_id IS NOT NULL THEN 'true' ELSE 'false' END ) AS is_top from CIRLE_GROUP cir LEFT JOIN GROUP_MEMBER mebm on cir.ID = mebm.GROUP_ID LEFT JOIN(select gro.ID, gro.NAME, COUNT(*) as persion_num from CIRLE_GROUP gro LEFT JOIN GROUP_MEMBER meb on gro.ID = meb.GROUP_ID GROUP BY gro.ID, gro.NAME) msg on msg.ID = cir.ID LEFT JOIN(SELECT art_msg.TITLE, art_msg.ID FROM (SELECT art.TITLE, cl.id, ROW_NUMBER() OVER ( PARTITION BY cl.id ORDER BY art.PUBLISH_TIME DESC ) AS num FROM CIRLE_GROUP cl INNER JOIN ARTICLE art ON cl.id = art.GROUP_ID where art.STATUS = '1' and art.IS_CLOSE = '0' and art.DELETE_FLAG = '0') art_msg WHERE art_msg.num = 1) artic on artic.ID = cir.ID LEFT JOIN(select ci.id, top.id as top_id, top.UPDATED_TIME from CIRLE_GROUP ci INNER JOIN GROUP_TOP top on ci.ID = top.GROUP_ID where top.USER_ID = #{pageDto.userId} and top.DELETE_FLAG = '0') top_msg on cir.ID = top_msg.id where mebm.USER_ID = #{pageDto.userId} and cir.STATUS = '0' and cir.IS_DISMISS = '0' and cir.IS_ENABLE = '1' and cir.DELETE_FLAG = '0' order by case when top_msg.top_id is not null then top_msg.top_id else '1' end desc, top_msg.UPDATED_TIME desc, mebm.TYPE asc, mebm.JOIN_TIME desc
时间: 2024-04-14 18:26:35 浏览: 171
如何进行sql优化
为了优化这个 SQL 查询,可以考虑以下几个方面:
1. 索引优化:确保相关的表上有适当的索引,以加快查询速度。对于经常被使用的列,可以考虑创建索引来提高查询性能。
2. 子查询优化:可以尝试将一些子查询转换为连接操作,以减少查询的复杂度和开销。比如将第一个左连接子查询 msg 转换为一个连接操作。
3. 冗余条件移除:检查 WHERE 子句中的条件,确保只包含必要的条件。移除不必要的冗余条件可以减少查询的复杂度。
4. CASE 表达式优化:对于 CASE 表达式中使用的条件,可以考虑使用更简洁的写法,以提高查询效率。
5. 分页优化:如果查询结果需要分页,可以考虑使用 LIMIT 或者 ROW_NUMBER() 函数进行分页,以减少返回的数据量。
最终优化后的 SQL 查询可能如下所示(注意:这只是一个示例,具体优化方式需要根据实际情况进行调整):
```
SELECT cir.ID, cir.NAME, mebm.type, msg.persion_num, artic.TITLE AS ARTICLE_TITLE,
(CASE WHEN top_msg.top_id IS NOT NULL THEN 'true' ELSE 'false' END) AS is_top
FROM CIRLE_GROUP cir
LEFT JOIN GROUP_MEMBER mebm ON cir.ID = mebm.GROUP_ID
LEFT JOIN (
SELECT gro.ID, gro.NAME, COUNT(*) AS persion_num
FROM CIRLE_GROUP gro
LEFT JOIN GROUP_MEMBER meb ON gro.ID = meb.GROUP_ID
GROUP BY gro.ID, gro.NAME
) msg ON msg.ID = cir.ID
LEFT JOIN (
SELECT art_msg.TITLE, art_msg.ID
FROM (
SELECT art.TITLE, cl.id,
ROW_NUMBER() OVER (PARTITION BY cl.id ORDER BY art.PUBLISH_TIME DESC) AS num
FROM CIRLE_GROUP cl
INNER JOIN ARTICLE art ON cl.id = art.GROUP_ID
WHERE art.STATUS = '1' AND art.IS_CLOSE = '0' AND art.DELETE_FLAG = '0'
) art_msg
WHERE art_msg.num = 1
) artic ON artic.ID = cir.ID
LEFT JOIN (
SELECT ci.id, top.id AS top_id, top.UPDATED_TIME
FROM CIRLE_GROUP ci
INNER JOIN GROUP_TOP top ON ci.ID = top.GROUP_ID
WHERE top.USER_ID = #{pageDto.userId} AND top.DELETE_FLAG = '0'
) top_msg ON cir.ID = top_msg.id
WHERE mebm.USER_ID = #{pageDto.userId}
AND cir.STATUS = '0'
AND cir.IS_DISMISS = '0'
AND cir.IS_ENABLE = '1'
AND cir.DELETE_FLAG = '0'
ORDER BY CASE WHEN top_msg.top_id IS NOT NULL THEN top_msg.top_id ELSE '1' END DESC,
top_msg.UPDATED_TIME DESC, mebm.TYPE ASC, mebm.JOIN_TIME DESC
```
请注意,以上只是一个示例,具体的优化策略需要根据数据库的结构和数据量进行调整。同时,建议在进行优化之前先备份数据库,并在测试环境中进行验证。
阅读全文