在有或没有连接的情况下,COUNT速度会有显着差异吗?

时间:2012-01-12 13:38:08

标签: mysql

非常感谢任何反馈!

我正在使用一个paginator类来接受我的查询,创建两个额外的查询来进行计数并获取id,然后修改我的原始查询以通过id(这里的id列表)选择结果。一切都很好,除了前两个查询连接是不必要的。

在count和id中使用连接获取查询是否会显着降低速度?我问,因为如果有明显的差异,我宁愿写自己的分页器,但是如果差异是不重要的话,我希望避免这样做。

以下是我提到的生成的查询:

SELECT count(DISTINCT a0_.id) AS sclr0 
FROM   test_comment a0_ 
       INNER JOIN test_user a1_ ON a0_.user_id = a1_.id 
       LEFT JOIN test_comment_vote a2_ 
           ON a0_.id = a2_.comment_id 
              AND (a2_.user_id = ?) WHERE a0_.post_id = ?

SELECT DISTINCT a0_.id AS id0 FROM test_comment a0_ INNER JOIN test_user a1_ ON a0_.user_id = a1_.id LEFT JOIN test_comment_vote a2_ ON a0_.id = a2_.comment_id AND (a2_.user_id = ?) WHERE a0_.post_id = ? LIMIT 5 OFFSET 0

SELECT a0_.id AS id0, a0_.comment AS comment1, a0_.comment_processed AS comment_processed2, a0_.uvotes_up AS uvotes_up3, a0_.uvotes_down AS uvotes_down4, a0_.bvotes_up AS bvotes_up5, a0_.bvotes_down AS bvotes_down6, a0_.tvotes_up AS tvotes_up7, a0_.tvotes_down AS tvotes_down8, a0_.tvotes AS tvotes9, a0_.created_at AS created_at10, a0_.updated_at AS updated_at11, a1_.id AS id12, a1_.name AS name13, a1_.email AS email14, a1_.pass AS pass15, a1_.salt AS salt16, a1_.created_at AS created_at17, a2_.uvote_up AS uvote_up18, a2_.uvote_down AS uvote_down19, a2_.bvote_up AS bvote_up20, a2_.bvote_down AS bvote_down21, a2_.ip AS ip22, a0_.user_id AS user_id23, a0_.post_id AS post_id24, a2_.user_id AS user_id25, a2_.comment_id AS comment_id26 FROM test_comment a0_ INNER JOIN test_user a1_ ON a0_.user_id = a1_.id LEFT JOIN test_comment_vote a2_ ON a0_.id = a2_.comment_id AND (a2_.user_id = ?) WHERE a0_.post_id = ? AND a0_.id IN (?, ?, ?, ?, ?)

0 个答案:

没有答案