MySQL在查询优化器中将5.6升级到5.7问题

时间:2018-06-21 09:04:05

标签: mysql

升级到5.7 cpu后,盒的利用率急剧增加。当我们检查了最常用的查询之一时,我们发现它在5.7中花费了更多时间。粘贴升级前后查询的解释结果

升级mysql 5.6.27之前

explain 
SELECT * from `shipments` 
LEFT OUTER JOIN `orders` ON `orders`.`id` = `shipments`.`order_id` 
LEFT OUTER JOIN `users` `pickup_users` ON `orders`.`pickup_user_id` = `pickup_users`.`id` 
LEFT OUTER JOIN `users` `drop_users` ON `orders`.`drop_user_id` = `drop_users`.`id` 
LEFT OUTER JOIN `merchants` ON `orders`.`source_id` = `merchants`.`id` 
LEFT OUTER JOIN `users` `source_users` ON `merchants`.`user_id` = `source_users`.`id` 
LEFT OUTER JOIN `user_addresses` `pickup_user_address` ON `orders`.`pickup_user_address_id` = `pickup_user_address`.`id` 
LEFT OUTER JOIN `user_addresses` `drop_user_address` ON `orders`.`drop_user_address_id` = `drop_user_address`.`id` 
LEFT OUTER JOIN `driver_queues` `driver_queues` ON `orders`.`id` = `driver_queues`.`order_id` 
WHERE `pickup_user_address`.`city_id` = 10  
ORDER BY `shipments`.`id` DESC 
LIMIT 20 ;

| id | select_type | table               | type   | possible_keys                                  | key                                    | key_len | ref                                       | rows    | Extra                           |
+----+-------------+---------------------+--------+------------------------------------------------+----------------------------------------+---------+-------------------------------------------+---------+---------------------------------+
|  1 | SIMPLE      | pickup_user_address | ref    | PRIMARY,index_user_addresses_on_city_id        | index_user_addresses_on_city_id        | 4       | const                                     | 1156876 | Using temporary; Using filesort |

|  1 | SIMPLE      | orders              | ref    | PRIMARY,index_orders_on_pickup_user_address_id | index_orders_on_pickup_user_address_id | 5       | carthero_prod.pickup_user_address.id      |       7 | Using index condition           |

|  1 | SIMPLE      | drop_user_address   | eq_ref | PRIMARY                                        | PRIMARY                                | 4       | carthero_prod.orders.drop_user_address_id |       1 | NULL                            |

|  1 | SIMPLE      | shipments           | ref    | index_shipments_on_order_id                    | index_shipments_on_order_id            | 5       | carthero_prod.orders.id                   
|    1 | NULL                            |

|  1 | SIMPLE      | driver_queues       | ref    | index_driver_queues_on_order_id                | index_driver_queues_on_order_id        | 5       | carthero_prod.orders.id         |       1 | NULL                            |

|  1 | SIMPLE      | pickup_users        | eq_ref | PRIMARY                                        | PRIMARY                                | 4       | carthero_prod.orders.pickup_user_id       |       1 | NULL                            |

|  1 | SIMPLE      | drop_users          | eq_ref | PRIMARY                                        | PRIMARY                                | 4       | carthero_prod.orders.drop_user_id         |       1 | NULL                            |

|  1 | SIMPLE      | merchants           | eq_ref | PRIMARY                                        | PRIMARY                                | 4       | carthero_prod.orders.source_id            |       1 | NULL                            |

|  1 | SIMPLE      | source_users        | eq_ref | PRIMARY                                        | PRIMARY                                | 4       | carthero_prod.merchants.user_id           |       1 | NULL                            |
+----+-------------+---------------------+--------+------------------------------------------------+----------------------------------------+---------+-------------------------------------------+---------+---------------------------------+

升级到5.7.21后

explain  
SELECT * from `shipments` 
LEFT OUTER JOIN `orders` ON `orders`.`id` = `shipments`.`order_id` 
LEFT OUTER JOIN `users` `pickup_users` ON `orders`.`pickup_user_id` = `pickup_users`.`id` 
LEFT OUTER JOIN `users` `drop_users` ON `orders`.`drop_user_id` = `drop_users`.`id` 
LEFT OUTER JOIN `merchants` ON `orders`.`source_id` = `merchants`.`id` 
LEFT OUTER JOIN `users` `source_users` ON `merchants`.`user_id` = `source_users`.`id` 
LEFT OUTER JOIN `user_addresses` `pickup_user_address` ON `orders`.`pickup_user_address_id` = `pickup_user_address`.`id` 
LEFT OUTER JOIN `user_addresses` `drop_user_address` ON `orders`.`drop_user_address_id` = `drop_user_address`.`id` 
LEFT OUTER JOIN `driver_queues` `driver_queues` ON `orders`.`id` = `driver_queues`.`order_id` 
WHERE `pickup_user_address`.`city_id` = 10  
ORDER BY `shipments`.`id` DESC 
LIMIT 20 ;

+----+-------------+---------------------+------------+--------+------------------------------------------------+---------------------------------+---------+---------------------------------------------+----------+----------+----------------------------------------------+
| id | select_type | table               | partitions | type   | possible_keys                                  | key                             | key_len | ref                                         | rows     | filtered | Extra                                        |
+----+-------------+---------------------+------------+--------+------------------------------------------------+---------------------------------+---------+---------------------------------------------+----------+----------+----------------------------------------------+

|  1 | SIMPLE      | orders              | NULL       | ALL    | PRIMARY,index_orders_on_pickup_user_address_id | NULL                            | NULL    | NULL                                        | 24079764 |   100.00 | Using where; Using temporary; Using filesort |

|  1 | SIMPLE      | pickup_users        | NULL       | eq_ref | PRIMARY                                        | PRIMARY                         | 4       | carthero_prod.orders.pickup_user_id         |        1 |   100.00 | NULL                                         |

|  1 | SIMPLE      | drop_users          | NULL       | eq_ref | PRIMARY                                        | PRIMARY                         | 4       | carthero_prod.orders.drop_user_id           |        1 |   100.00 | NULL                                         |

|  1 | SIMPLE      | merchants           | NULL       | eq_ref | PRIMARY                                        | PRIMARY                         | 4       | carthero_prod.orders.source_id              |        1 |   100.00 | NULL                                         |

|  1 | SIMPLE      | pickup_user_address | NULL       | eq_ref | PRIMARY,index_user_addresses_on_city_id        | PRIMARY                         | 4       | carthero_prod.orders.pickup_user_address_id |        1 |    10.83 | Using where                                  |

|  1 | SIMPLE      | source_users        | NULL       | eq_ref | PRIMARY                                        | PRIMARY                         | 4       | carthero_prod.merchants.user_id             |        1 |   100.00 | NULL                                         |

|  1 | SIMPLE      | drop_user_address   | NULL       | eq_ref | PRIMARY                                        | PRIMARY                         | 4       | carthero_prod.orders.drop_user_address_id   |        1 |   100.00 | NULL                                         |

|  1 | SIMPLE      | driver_queues       | NULL       | ref    | index_driver_queues_on_order_id                | index_driver_queues_on_order_id | 5       | carthero_prod.orders.id                     |        1 |   100.00 | NULL                                         |

|  1 | SIMPLE      | shipments           | NULL       | ref    | index_shipments_on_order_id                    | index_shipments_on_order_id     | 5       | carthero_prod.orders.id                     |        1 |   100.00 | NULL                                         |
+----+-------------+---------------------+------------+--------+------------------------------------------------+---------------------------------+---------+---------------------------------------------+----------+----------+----------------------------------------------+

0 个答案:

没有答案