1

我已经在相当小的表上运行了查询(每个表最多 4000 行)。查询持续约 4 秒,包含多个连接,但是,所有连接都使用索引。

这是查询:

SELECT count(DISTINCT p0_.id) AS sclr0 
FROM promoter p0_ 
LEFT JOIN user u1_ ON p0_.user_id = u1_.id 
LEFT JOIN profile p2_ ON p0_.id = p2_.promoter_id 
LEFT JOIN promoter_city p4_ ON p0_.id = p4_.user_id 
LEFT JOIN city c3_ ON c3_.id = p4_.city_id 
LEFT JOIN promoter_language p6_ ON p0_.id = p6_.user_id 
LEFT JOIN language l5_ ON l5_.id = p6_.language_id 
LEFT JOIN promoter_worker_type p8_ ON p0_.id = p8_.promoter_id 
LEFT JOIN worker_type w7_ ON w7_.id = p8_.workertype_id 
LEFT JOIN practise p9_ ON p0_.id = p9_.promoter_id 
LEFT JOIN contract c11_ ON p0_.id = c11_.promoter_id 

这是解释的输出:

+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+
|        id        |   select_type    |      table       |       type       |  possible_keys   |       key        |     key_len      |       ref        |       rows       |      Extra       |
+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+
|        1         |      SIMPLE      |       p0_        |      index       |                  |UNIQ_BCB929A3A76ED|        5         |                  |       4161       |   Using index    |
+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+
|        1         |      SIMPLE      |       u1_        |      eq_ref      |     PRIMARY      |     PRIMARY      |        4         |promoteri.p0_.user|        1         |   Using index    |
+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+
|        1         |      SIMPLE      |       p2_        |       ref        |type,IDX_8157AA0F4|       type       |        5         | promoteri.p0_.id |        1         |   Using index    |
+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+
|        1         |      SIMPLE      |       p4_        |       ref        |PRIMARY,IDX_183C53|     PRIMARY      |        4         | promoteri.p0_.id |        1         |   Using index    |
+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+
|        1         |      SIMPLE      |       c3_        |      eq_ref      |     PRIMARY      |     PRIMARY      |        4         |promoteri.p4_.city|        1         |   Using index    |
+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+
|        1         |      SIMPLE      |       p6_        |       ref        |PRIMARY,IDX_19EE2A|     PRIMARY      |        4         | promoteri.p0_.id |        1         |   Using index    |
+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+
|        1         |      SIMPLE      |       l5_        |      eq_ref      |     PRIMARY      |     PRIMARY      |        4         |promoteri.p6_.lang|        1         |   Using index    |
+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+
|        1         |      SIMPLE      |       p8_        |       ref        |PRIMARY,IDX_37AC17|     PRIMARY      |        4         | promoteri.p0_.id |        1         |   Using index    |
+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+
|        1         |      SIMPLE      |       w7_        |      eq_ref      |     PRIMARY      |     PRIMARY      |        4         |promoteri.p8_.work|        1         |   Using index    |
+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+
|        1         |      SIMPLE      |       p9_        |       ref        |IDX_352E261F4B84B2|IDX_352E261F4B84B2|        5         | promoteri.p0_.id |        1         |   Using index    |
+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+
|        1         |      SIMPLE      |       c11_       |       ref        |IDX_E98F28594B84B2|IDX_E98F28594B84B2|        5         | promoteri.p0_.id |        6         |   Using index    |
+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+------------------+

我知道,在这个查询中加入是不必要的,但是我使用了很多类似的查询,这些查询应用了一些 where 语句,这些连接在哪里真的很重要。

有什么解释,为什么这个查询需要这么长时间?

4

1 回答 1

2

尝试将所有这些更改left joinsinner joins. 当您使用 时left join,您会强制优化器按照您提供的顺序连接表……当您使用 时inner join,优化器可以选择更好的起始位置来连接表。

至于left join强制特定读取顺序的参考 - 来自MySQL Docs

LEFT JOIN由连接优化器强制或STRAIGHT_JOIN帮助连接优化器更快地完成其工作的表读取顺序,因为要检查的表排列更少。

... 表示left join强制读取顺序与STRAIGHT_JOIN.

于 2013-02-20T21:17:49.560 回答