Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
优化mysql子查询语句_Mysql_Sql_Database - Fatal编程技术网

优化mysql子查询语句

优化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

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

这是我的桌子

列说明:

“cp_id”是指Creverypasta的id

“session_id”类似于writer

“令人毛骨悚然”是指令人毛骨悚然的意大利面有多令人毛骨悚然。当“令人毛骨悚然”为0时,它不是令人毛骨悚然的故事。但当这是1时,就令人毛骨悚然了

这是我的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

对于您的查询,您可以在爬行点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);