MySQL说明计划根据条件值更改

时间:2019-04-11 01:25:17

标签: mysql

今天,我们刚刚注意到两个看似完全相同的查询导致了截然不同的执行计划,从而导致了截然不同的性能。 另一个令人震惊的事实是,聚合超过50k行的查询运行的速度比聚合600多个结果的查询快30倍。 “快速”查询在〜400ms内运行,而“慢速”查询在〜10sc内运行。

慢速查询:

SELECT account_ownership_id, count(*)
FROM posts
JOIN accounts ON posts.account_id = accounts.id
JOIN platforms ON accounts.platform_id = platforms.id 
JOIN sponsor_annotations ON sponsor_annotations.post_id = posts.id
JOIN rightsholders_placements
ON (rightsholders_placements.rightsholder_id = sponsor_annotations.rightsholder_id
AND rightsholders_placements.placement_id = sponsor_annotations.placement_id)
JOIN clients_sponsors_placements
ON (clients_sponsors_placements.rightsholder_id = sponsor_annotations.rightsholder_id
AND clients_sponsors_placements.sponsor_id = sponsor_annotations.sponsor_id
AND clients_sponsors_placements.placement_id = sponsor_annotations.placement_id)
WHERE  clients_sponsors_placements.client_id = 1125   and accounts.platform_id = 5 
GROUP BY sponsor_annotations.account_ownership_id LIMIT 1000

快速查询:

SELECT account_ownership_id, count(*)
FROM posts
JOIN accounts ON posts.account_id = accounts.id
JOIN platforms ON accounts.platform_id = platforms.id 
JOIN sponsor_annotations ON sponsor_annotations.post_id = posts.id
JOIN rightsholders_placements
ON (rightsholders_placements.rightsholder_id = sponsor_annotations.rightsholder_id
AND rightsholders_placements.placement_id = sponsor_annotations.placement_id)
JOIN clients_sponsors_placements
ON (clients_sponsors_placements.rightsholder_id = sponsor_annotations.rightsholder_id
AND clients_sponsors_placements.sponsor_id = sponsor_annotations.sponsor_id
AND clients_sponsors_placements.placement_id = sponsor_annotations.placement_id)
WHERE  clients_sponsors_placements.client_id = 1125   and accounts.platform_id = 1
GROUP BY sponsor_annotations.account_ownership_id LIMIT 1000

如您所见,两个查询之间的唯一区别在于平台ID where子句。我希望查询计划非常相似,但事实并非如此。即使慢查询聚集在更少的行上,也要花费更多的时间。

有趣的是,将条件更改为“(1,5)中的accounts.platform_id”可以显着加快查询速度(查询的速度与执行accounts.platform_id = 1的速度一样快)< / p>

这是慢速查询的解释计划:

+---+--------+-----------------------------+--------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-----------------------+----+--------------------------------------------------------------------------------------------------------------------------------+-----+-------+----------------------------------------------+
| 1 | SIMPLE | platforms                   | const  | PRIMARY                                                                                                                                                                                     | PRIMARY               | 4  | const                                                                                                                          | 1   | 100.0 | Using index; Using temporary; Using filesort |
| 1 | SIMPLE | accounts                    | ref    | PRIMARY,fk_accounts_platforms                                                                                                                                                               | fk_accounts_platforms | 4  | const                                                                                                                          | 354 | 100.0 | Using index                                  |
| 1 | SIMPLE | posts                       | ref    | PRIMARY,fk_posts_accounts_id                                                                                                                                                                | fk_posts_accounts_id  | 4  | sports.accounts.id                                                                                                             | 2   | 100.0 | Using index                                  |
| 1 | SIMPLE | sponsor_annotations         | ref    | sponsor_annotations,fk_sponsor_annotations_sponsor_placements,fk_sponsor_annotations_rightsholder_placements,fk_sponsor_annotations_sponsors,fk_sponsor_annotations_account_ownership_types | sponsor_annotations   | 4  | sports.posts.id                                                                                                                | 29  | 100.0 |                                              |
| 1 | SIMPLE | clients_sponsors_placements | eq_ref | PRIMARY,fk_client_sponsor_placements_clients_rightsholders_sponsors,fk_client_sponsor_placements_sponsor_placements                                                                         | PRIMARY               | 16 | const,sports.sponsor_annotations.placement_id,sports.sponsor_annotations.rightsholder_id,sports.sponsor_annotations.sponsor_id | 1   | 100.0 | Using index                                  |
| 1 | SIMPLE | rightsholders_placements    | eq_ref | PRIMARY,fk_rightsholders_placements_rightsholders                                                                                                                                           | PRIMARY               | 8  | sports.sponsor_annotations.placement_id,sports.sponsor_annotations.rightsholder_id                                             | 1   | 100.0 | Using index                                  |
+---+--------+-----------------------------+--------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-----------------------+----+--------------------------------------------------------------------------------------------------------------------------------+-----+-------+----------------------------------------------+

以及用于更快查询的解释计划:

+---+--------+-----------------------------+--------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------------------------------------------+----+--------------------------------------------------------------------------------------------------------------------------------------------------+-----+-------+----------------------------------------------+
| 1 | SIMPLE | platforms                   | const  | PRIMARY                                                                                                                                                                                     | PRIMARY                                   | 4  | const                                                                                                                                            | 1   | 100.0 | Using index; Using temporary; Using filesort |
| 1 | SIMPLE | clients_sponsors_placements | ref    | PRIMARY,fk_client_sponsor_placements_clients_rightsholders_sponsors,fk_client_sponsor_placements_sponsor_placements                                                                         | PRIMARY                                   | 4  | const                                                                                                                                            | 223 | 100.0 | Using index                                  |
| 1 | SIMPLE | rightsholders_placements    | eq_ref | PRIMARY,fk_rightsholders_placements_rightsholders                                                                                                                                           | PRIMARY                                   | 8  | sports.clients_sponsors_placements.placement_id,sports.clients_sponsors_placements.rightsholder_id                                               | 1   | 100.0 | Using index                                  |
| 1 | SIMPLE | sponsor_annotations         | ref    | sponsor_annotations,fk_sponsor_annotations_sponsor_placements,fk_sponsor_annotations_rightsholder_placements,fk_sponsor_annotations_sponsors,fk_sponsor_annotations_account_ownership_types | fk_sponsor_annotations_sponsor_placements | 12 | sports.clients_sponsors_placements.rightsholder_id,sports.clients_sponsors_placements.sponsor_id,sports.clients_sponsors_placements.placement_id | 158 | 100.0 |                                              |
| 1 | SIMPLE | posts                       | eq_ref | PRIMARY,fk_posts_accounts_id                                                                                                                                                                | PRIMARY                                   | 4  | sports.sponsor_annotations.post_id                                                                                                               | 1   | 100.0 |                                              |
| 1 | SIMPLE | accounts                    | eq_ref | PRIMARY,fk_accounts_platforms                                                                                                                                                               | PRIMARY                                   | 4  | sports.posts.account_id                                                                                                                          | 1   | 100.0 | Using where                                  |
+---+--------+-----------------------------+--------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------------------------------------------+----+--------------------------------------------------------------------------------------------------------------------------------------------------+-----+-------+----------------------------------------------+

我该如何更改查询或架构以确保两种情况下的执行计划都相同?

谢谢

0 个答案:

没有答案