优化mysql子查询语句
我正在做一个社区分享“令人毛骨悚然的意大利面”。 我的查询现在需要133ms,但我想让查询更快。 仅仅推荐一个关键词就足够了 这是我的桌子 列说明: “cp_id”是指Creverypasta的id “session_id”类似于writer “令人毛骨悚然”是指令人毛骨悚然的意大利面有多令人毛骨悚然。当“令人毛骨悚然”为0时,它不是令人毛骨悚然的故事。但当这是1时,就令人毛骨悚然了 这是我的SQL查询优化mysql子查询语句,mysql,sql,database,Mysql,Sql,Database,我正在做一个社区分享“令人毛骨悚然的意大利面”。 我的查询现在需要133ms,但我想让查询更快。 仅仅推荐一个关键词就足够了 这是我的桌子 列说明: “cp_id”是指Creverypasta的id “session_id”类似于writer “令人毛骨悚然”是指令人毛骨悚然的意大利面有多令人毛骨悚然。当“令人毛骨悚然”为0时,它不是令人毛骨悚然的故事。但当这是1时,就令人毛骨悚然了 这是我的SQL查询 SELECT id, alias, created_at, modified_at, tit
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
对于您的查询,您可以在爬行点SCP\U id上添加索引,爬行:
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
create index idx_creepypoints_cpid_creepy on creepy_points(cpid, creepy);