我在做一个社区共享的“令人毛骨悚然的意大利面”我的查询现在需要133 My,但是我想使查询更快。仅仅推荐一个关键字就足够了。
这是我的桌子。
列描述: 'cp_id'的意思是爬虫的id。 'session_id'有点像作家。 ' creepy '指的是令人毛骨悚然的意大利面。当“令人毛骨悚然”为0时,这并不是恐怖的故事。但是当它是1的时候,它是可怕的。
- 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发布于 2017-05-23 12:36:04
对于查询,可以在creepy_points(cp_id, creepy)上添加索引
create index idx_creepypoints_cpid_creepy on creepy_points(cpid, creepy);https://stackoverflow.com/questions/44134985
复制相似问题