gpt4 book ai didi

mysql - 优化mysql子查询语句

转载 作者:行者123 更新时间:2023-11-29 10:48:40 24 4
gpt4 key购买 nike

我正在创建一个社区,分享“令人毛骨悚然的意大利面”。我的查询现在需要 133 毫秒,但我想让查询更快。只需推荐一个关键字就足够了。

这是我的 table 。

Column Description:

'cp_id' means creepypasta's id.

'session_id' is something like writer.

'creepy' means how creepy the creepy pasta is. when 'creepy' is 0, it's not creepy story. but when that is 1, it's creepy.

 - creepypastas: id, alias, created_at, modified_at, title, content, session_id
- creepypasta_comments: id, cp_id, created_at, content, session_id
- creepy_points: id, cp_id, creepy, session_id

这是我的 SQL 查询。

SELECT id, alias, created_at, modified_at, title, content, session_id, 
(SELECT COALESCE(SUM(case creepy when 1 then 1 else 0 end), 0) FROM
creepy_points c WHERE creepypastas.id = c.cp_id) AS creepy_count,
(SELECT COALESCE(SUM(case creepy when 0 then 1 else 0 end), 0) FROM
creepy_points c WHERE creepypastas.id = c.cp_id) AS not_creepy_count,
(SELECT COUNT(c.id) FROM creepypasta_comments c WHERE creepypastas.id =
c.cp_id) AS comment_count
FROM creepypastas ORDER BY created_at DESC

最佳答案

对于您的查询,您可以在 creepy_points(cp_id, Creepy) 上添加索引:

create index idx_creepypoints_cpid_creepy on creepy_points(cpid, creepy);

关于mysql - 优化mysql子查询语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44134985/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com