0

这是一个查询:

SELECT DISTINCT
    spentits.*,
    username,
    (SELECT count(*) from likes where likes.spentit_id = spentits.id) as like_count,
    (SELECT count(*) from comments where comments.spentit_id = spentits.id) as comment_count,
    (SELECT count(*) from wishlist_items where wishlist_items.spentit_id = spentits.id) as wishlist_count,
    (case when likes.id is null then 0 else 1 end) as is_liked_by_me,
    (case when wishlist_items.id is null then 0 else 1 end) as is_wishlisted_by_me
FROM spentits
LEFT JOIN users ON users.id = spentits.user_id
LEFT JOIN likes ON likes.user_id = 9 AND likes.spentit_id = spentits.id
LEFT JOIN wishlist_items ON wishlist_items.user_id = 9 AND wishlist_items.spentit_id = spentits.id
WHERE spentits.user_id IN
    (SELECT follows.following_id
     FROM follows
     WHERE follows.follower_id = 9)
ORDER BY id DESC 
LIMIT 15;

这平均43ms需要执行。现在另一个查询(如下)没有 where 子句,更不用说第二个 SELECT 子查询的执行速度要慢 5 倍(240ms)!

SELECT DISTINCT
    spentits.*,
    username,
    (SELECT count(*) from likes where likes.spentit_id = spentits.id) as like_count,
    (SELECT count(*) from comments where comments.spentit_id = spentits.id) as comment_count,
    (SELECT count(*) from wishlist_items where wishlist_items.spentit_id = spentits.id) as wishlist_count,
    (case when likes.id is null then 0 else 1 end) as is_liked_by_me,
    (case when wishlist_items.id is null then 0 else 1 end) as is_wishlisted_by_me
FROM spentits
LEFT JOIN users ON users.id = spentits.user_id
LEFT JOIN likes ON likes.user_id = 9 AND likes.spentit_id = spentits.id
LEFT JOIN wishlist_items ON wishlist_items.user_id = 9 AND wishlist_items.spentit_id = spentits.id
ORDER BY id DESC 
LIMIT 15;

为什么?第二个查询不应该快得多,因为没有条件,没有第二个查询。数据库需要做的就是选择最后 15 条记录,与第一个记录相比,它需要在执行子查询后扫描每条记录并检查 id 是否包含在其中。我真的很困惑,一个应该执行得更快的查询实际上却慢了 5 倍。

4

1 回答 1

1

有与统计信息和元数据相关的执行路径,explain plan为两者做一个并查看执行路径。

此外,按 id 过滤可能会在没有匹配行的表上执行跳过,当您不按该 id 过滤时,会投影行。

于 2013-06-04T15:43:04.343 回答