0

我问错了问题,所以我会再试一次(对不起)。您不必详细阅读查询,而只是给您一个想法。

无论如何:有问题的查询是这个,由 Doctrine2 生成的(我删掉SELECT了一部分)

FROM
    tbl_story t0_ 
      LEFT JOIN tbl_user t1_ ON t0_.creator_id = t1_.id 
      LEFT JOIN tbl_image t2_  ON t1_.avatar_id = t2_.id 
      LEFT JOIN tbl_image t3_  ON t0_.id = t3_.story_id 
      LEFT JOIN tbl_like t4_ ON t0_.id = t4_.story_id AND (t4_.creator_id = 3) 
      LEFT JOIN tbl_comment t5_ ON t0_.id = t5_.story_id 
      LEFT JOIN tbl_like t6_  ON t5_.id = t6_.comment_id AND (t6_.creator_id = 3) 
      LEFT JOIN tbl_user t7_  ON t5_.creator_id = t7_.id 
      LEFT JOIN tbl_image t8_ ON t7_.avatar_id = t8_.id 
      LEFT JOIN tbl_location_city t9_  ON t0_.city_id = t9_.id 
      LEFT JOIN tbl_location_state t10_ ON t9_.state_id = t10_.id 
      LEFT JOIN tbl_location_country t11_ ON t10_.country_id = t11_.id 
      LEFT JOIN tbl_story t12_ ON t0_.parent_id = t12_.id 
      LEFT JOIN tbl_page t13_ ON t0_.page_id = t13_.id 
      LEFT JOIN tbl_page_type t14_ ON t13_.page_type_id = t14_.id 
      LEFT JOIN tbl_story_tag t25_  ON t0_.id = t25_.story_id 
      LEFT JOIN tbl_tag t15_ ON t15_.id = t25_.tag_id 
      LEFT JOIN tbl_story_linked_user_reference t16_ ON t0_.id = t16_.story_id 
      LEFT JOIN tbl_user t17_ ON t16_.user_id = t17_.id 
      LEFT JOIN tbl_image t18_ ON t17_.avatar_id = t18_.id 
      LEFT JOIN tbl_story t19_  ON t0_.id = t19_.parent_id 
      LEFT JOIN tbl_album t20_ ON t0_.parent_album_id = t20_.id 
      LEFT JOIN tbl_image t21_ ON t20_.id = t21_.album_id 
      LEFT JOIN tbl_image_linked_user t22_ ON t21_.id = t22_.image_id 
      LEFT JOIN tbl_user t23_ ON t22_.user_id = t23_.id 
      LEFT JOIN tbl_image t24_ ON t23_.avatar_id = t24_.id 
WHERE (
    t0_.creator_id = 3 
    OR t0_.id IN 
        (SELECT 
          t26_.id 
        FROM tbl_story t26_ 
          INNER JOIN tbl_story_circle_visibility t28_ ON t26_.id = t28_.story_id 
          INNER JOIN tbl_circle t27_ ON t27_.id = t28_.circle_id 
          INNER JOIN tbl_user_circle_reference t29_ ON t27_.id = t29_.circle_id 
            AND (t29_.user_id = 3))
    OR t0_.is_public = 1
  ) 
  AND t0_.is_draft = 0 
  AND t0_.type IN (0, 3, 2, 21) 
ORDER BY t0_.created_at DESC
LIMIT 7 

问题:ORDER BY当 tbl_story (主查询)中只有 31.000 行时,执行查询需要 1.2 秒。如果我删除ORDER BY它,它会在 6 毫秒内执行,我想所有这些连接都需要额外的时间,但仍然没有什么可担心的。

我一开始以为 ORDER BY很慢,但我错了。我试过了

SELECT * FROM `tbl_story` ORDER BY created_at DESC LIMIT 7

(同样的东西,但没有连接),它在 2 毫秒内执行。

我也阅读了其他问题,包括建议移除JOIN部件的问题。如果我在 Doctrine2 中可以这样做,那可能会起作用:

$queryBuilder->select("o.id")->removeLeftJoins()

在我获取 ID 后,在原始查询的克隆中使用它们。

如何解决分页问题?

笔记:

即使我完全删除WHERE子句,查询也需要相同的 1.2 秒,所以它不是一个问题。

谢谢。

4

1 回答 1

0

我怀疑 type 和 is_draft 上的索引可能不足以使 MySQL 注意到这些索引。

如果您在 creator_id 上有一个索引,在 is_public 上有一个索引,在 id 上有另一个(我假设您这样做,因为它可能是主 id),那么像这样的 3 个联合查询可能会有所帮助。

SELECT *
FROM
    tbl_story t0_ 
      LEFT JOIN tbl_user t1_ ON t0_.creator_id = t1_.id 
      LEFT JOIN tbl_image t2_  ON t1_.avatar_id = t2_.id 
      LEFT JOIN tbl_image t3_  ON t0_.id = t3_.story_id 
      LEFT JOIN tbl_like t4_ ON t0_.id = t4_.story_id AND (t4_.creator_id = 3) 
      LEFT JOIN tbl_comment t5_ ON t0_.id = t5_.story_id 
      LEFT JOIN tbl_like t6_  ON t5_.id = t6_.comment_id AND (t6_.creator_id = 3) 
      LEFT JOIN tbl_user t7_  ON t5_.creator_id = t7_.id 
      LEFT JOIN tbl_image t8_ ON t7_.avatar_id = t8_.id 
      LEFT JOIN tbl_location_city t9_  ON t0_.city_id = t9_.id 
      LEFT JOIN tbl_location_state t10_ ON t9_.state_id = t10_.id 
      LEFT JOIN tbl_location_country t11_ ON t10_.country_id = t11_.id 
      LEFT JOIN tbl_story t12_ ON t0_.parent_id = t12_.id 
      LEFT JOIN tbl_page t13_ ON t0_.page_id = t13_.id 
      LEFT JOIN tbl_page_type t14_ ON t13_.page_type_id = t14_.id 
      LEFT JOIN tbl_story_tag t25_  ON t0_.id = t25_.story_id 
      LEFT JOIN tbl_tag t15_ ON t15_.id = t25_.tag_id 
      LEFT JOIN tbl_story_linked_user_reference t16_ ON t0_.id = t16_.story_id 
      LEFT JOIN tbl_user t17_ ON t16_.user_id = t17_.id 
      LEFT JOIN tbl_image t18_ ON t17_.avatar_id = t18_.id 
      LEFT JOIN tbl_story t19_  ON t0_.id = t19_.parent_id 
      LEFT JOIN tbl_album t20_ ON t0_.parent_album_id = t20_.id 
      LEFT JOIN tbl_image t21_ ON t20_.id = t21_.album_id 
      LEFT JOIN tbl_image_linked_user t22_ ON t21_.id = t22_.image_id 
      LEFT JOIN tbl_user t23_ ON t22_.user_id = t23_.id 
      LEFT JOIN tbl_image t24_ ON t23_.avatar_id = t24_.id 
WHERE t0_.creator_id = 3 
  AND t0_.is_draft = 0 
  AND t0_.type IN (0, 3, 2, 21) 
UNION
SELECT *
FROM
    tbl_story t0_ 
      LEFT JOIN tbl_user t1_ ON t0_.creator_id = t1_.id 
      LEFT JOIN tbl_image t2_  ON t1_.avatar_id = t2_.id 
      LEFT JOIN tbl_image t3_  ON t0_.id = t3_.story_id 
      LEFT JOIN tbl_like t4_ ON t0_.id = t4_.story_id AND (t4_.creator_id = 3) 
      LEFT JOIN tbl_comment t5_ ON t0_.id = t5_.story_id 
      LEFT JOIN tbl_like t6_  ON t5_.id = t6_.comment_id AND (t6_.creator_id = 3) 
      LEFT JOIN tbl_user t7_  ON t5_.creator_id = t7_.id 
      LEFT JOIN tbl_image t8_ ON t7_.avatar_id = t8_.id 
      LEFT JOIN tbl_location_city t9_  ON t0_.city_id = t9_.id 
      LEFT JOIN tbl_location_state t10_ ON t9_.state_id = t10_.id 
      LEFT JOIN tbl_location_country t11_ ON t10_.country_id = t11_.id 
      LEFT JOIN tbl_story t12_ ON t0_.parent_id = t12_.id 
      LEFT JOIN tbl_page t13_ ON t0_.page_id = t13_.id 
      LEFT JOIN tbl_page_type t14_ ON t13_.page_type_id = t14_.id 
      LEFT JOIN tbl_story_tag t25_  ON t0_.id = t25_.story_id 
      LEFT JOIN tbl_tag t15_ ON t15_.id = t25_.tag_id 
      LEFT JOIN tbl_story_linked_user_reference t16_ ON t0_.id = t16_.story_id 
      LEFT JOIN tbl_user t17_ ON t16_.user_id = t17_.id 
      LEFT JOIN tbl_image t18_ ON t17_.avatar_id = t18_.id 
      LEFT JOIN tbl_story t19_  ON t0_.id = t19_.parent_id 
      LEFT JOIN tbl_album t20_ ON t0_.parent_album_id = t20_.id 
      LEFT JOIN tbl_image t21_ ON t20_.id = t21_.album_id 
      LEFT JOIN tbl_image_linked_user t22_ ON t21_.id = t22_.image_id 
      LEFT JOIN tbl_user t23_ ON t22_.user_id = t23_.id 
      LEFT JOIN tbl_image t24_ ON t23_.avatar_id = t24_.id 
WHERE t0_.id IN 
        (SELECT 
          t26_.id 
        FROM tbl_story t26_ 
          INNER JOIN tbl_story_circle_visibility t28_ ON t26_.id = t28_.story_id 
          INNER JOIN tbl_circle t27_ ON t27_.id = t28_.circle_id 
          INNER JOIN tbl_user_circle_reference t29_ ON t27_.id = t29_.circle_id 
            AND (t29_.user_id = 3))
  AND t0_.is_draft = 0 
  AND t0_.type IN (0, 3, 2, 21) 
UNION
SELECT *
FROM
    tbl_story t0_ 
      LEFT JOIN tbl_user t1_ ON t0_.creator_id = t1_.id 
      LEFT JOIN tbl_image t2_  ON t1_.avatar_id = t2_.id 
      LEFT JOIN tbl_image t3_  ON t0_.id = t3_.story_id 
      LEFT JOIN tbl_like t4_ ON t0_.id = t4_.story_id AND (t4_.creator_id = 3) 
      LEFT JOIN tbl_comment t5_ ON t0_.id = t5_.story_id 
      LEFT JOIN tbl_like t6_  ON t5_.id = t6_.comment_id AND (t6_.creator_id = 3) 
      LEFT JOIN tbl_user t7_  ON t5_.creator_id = t7_.id 
      LEFT JOIN tbl_image t8_ ON t7_.avatar_id = t8_.id 
      LEFT JOIN tbl_location_city t9_  ON t0_.city_id = t9_.id 
      LEFT JOIN tbl_location_state t10_ ON t9_.state_id = t10_.id 
      LEFT JOIN tbl_location_country t11_ ON t10_.country_id = t11_.id 
      LEFT JOIN tbl_story t12_ ON t0_.parent_id = t12_.id 
      LEFT JOIN tbl_page t13_ ON t0_.page_id = t13_.id 
      LEFT JOIN tbl_page_type t14_ ON t13_.page_type_id = t14_.id 
      LEFT JOIN tbl_story_tag t25_  ON t0_.id = t25_.story_id 
      LEFT JOIN tbl_tag t15_ ON t15_.id = t25_.tag_id 
      LEFT JOIN tbl_story_linked_user_reference t16_ ON t0_.id = t16_.story_id 
      LEFT JOIN tbl_user t17_ ON t16_.user_id = t17_.id 
      LEFT JOIN tbl_image t18_ ON t17_.avatar_id = t18_.id 
      LEFT JOIN tbl_story t19_  ON t0_.id = t19_.parent_id 
      LEFT JOIN tbl_album t20_ ON t0_.parent_album_id = t20_.id 
      LEFT JOIN tbl_image t21_ ON t20_.id = t21_.album_id 
      LEFT JOIN tbl_image_linked_user t22_ ON t21_.id = t22_.image_id 
      LEFT JOIN tbl_user t23_ ON t22_.user_id = t23_.id 
      LEFT JOIN tbl_image t24_ ON t23_.avatar_id = t24_.id 
WHERE t0_.is_public = 1
  AND t0_.is_draft = 0 
  AND t0_.type IN (0, 3, 2, 21) 
ORDER BY t0_.created_at DESC
LIMIT 7 
于 2013-07-04T13:39:00.020 回答