Mariadb 2 解释计划:使用连接缓冲区和不使用连接缓冲区

Mariadb 2 explains plan : with Using join buffer and without

我 运行 在具有巨大性能差异的 2 个环境中进行相同的查询:0.015 秒与 25 秒。

解释计划:

+------+-------------+---------------+--------+------------------------------------+---------+---------+---------------------------------------------------------------------------------------------------------------------------+------+----------+---------------------------------+
| id   | select_type | table         | type   | possible_keys                      | key     | key_len | ref                                                                                                                       | rows | filtered | Extra                           |
+------+-------------+---------------+--------+------------------------------------+---------+---------+---------------------------------------------------------------------------------------------------------------------------+------+----------+---------------------------------+
|    1 | SIMPLE      | company1_     | const  | PRIMARY                            | PRIMARY | 152     | const                                                                                                                     |    1 |   100.00 | Using temporary; Using filesort |
|    1 | SIMPLE      | user2_        | ref    | PRIMARY                            | PRIMARY | 152     | const                                                                                                                     | 1032 |   100.00 | Using where                     |
|    1 | SIMPLE      | vacationpr5_  | eq_ref | PRIMARY                            | PRIMARY | 304     | user2_.ID_COMPANY_VACATION_PROFILE,.user2_.ID_VACATION_PROFILE                                                            |    1 |   100.00 | Using index                     |
|    1 | SIMPLE      | vacationac0_  | ref    | PRIMARY,I_VACATION_ACCUMULATION_EA | PRIMARY | 304     | const,.user2_.ID_USER                                                                                                     |    4 |   100.00 | Using where                     |
|    1 | SIMPLE      | vacationty3_  | eq_ref | PRIMARY                            | PRIMARY | 304     | const,.vacationac0_.ID_VACATION_TYPE                                                                                      |    1 |   100.00 | Using where                     |
|    1 | SIMPLE      | vacationst6_  | eq_ref | PRIMARY                            | PRIMARY | 608     | user2_.ID_COMPANY_VACATION_PROFILE,.user2_.ID_VACATION_PROFILE,const,.vacationac0_.ID_VACATION_TYPE                       |    1 |   100.00 | Using where                     |
|    1 | SIMPLE      | translatio9_  | eq_ref | PRIMARY                            | PRIMARY | 919     | vacationty3_.ID_COMPANY_TRANSLATION,.vacationty3_.ID_TRANSLATION                                                          |    1 |   100.00 | Using index                     |
|    1 | SIMPLE      | descriptio10_ | eq_ref | PRIMARY,                           | PRIMARY | 951     | vacationty3_.ID_COMPANY_TRANSLATION,.vacationty3_.ID_TRANSLATION,const                                                    |    1 |   100.00 | Using where                     |
|    1 | SIMPLE      | listvalue4_   | ALL    | NULL                               | NULL    | NULL    | NULL                                                                                                                      | 5284 |   100.00 | Using where                     |
|    1 | SIMPLE      | translatio7_  | eq_ref | PRIMARY                            | PRIMARY | 919     | listvalue4_.ID_COMPANY_TRANSLATION,.listvalue4_.ID_TRANSLATION                                                            |    1 |   100.00 | Using index                     |
|    1 | SIMPLE      | descriptio8_  | eq_ref | PRIMARY                            | PRIMARY | 951     | listvalue4_.ID_COMPANY_TRANSLATION,.listvalue4_.ID_TRANSLATION,const                                                      |    1 |   100.00 | Using where                     |
+------+-------------+---------------+--------+------------------------------------+---------+---------+---------------------------------------------------------------------------------------------------------------------------+------+----------+---------------------------------+

接下来的讲解计划:

+------+-------------+---------------+--------+------------------------------------+---------+---------+---------------------------------------------------------------------------------------------------------------------------------------+------+----------+-------------------------------------------------+
| id   | select_type | table         | type   | possible_keys                      | key     | key_len | ref                                                                                                                                   | rows | filtered | Extra                                           |
+------+-------------+---------------+--------+------------------------------------+---------+---------+---------------------------------------------------------------------------------------------------------------------------------------+------+----------+-------------------------------------------------+
|    1 | SIMPLE      | company1_     | const  | PRIMARY                            | PRIMARY | 152     | const                                                                                                                                 |    1 |   100.00 | Using temporary; Using filesort                 |
|    1 | SIMPLE      | user2_        | ref    | PRIMARY,                           | PRIMARY | 152     | const                                                                                                                                 | 1050 |   100.00 | Using where                                     |
|    1 | SIMPLE      | vacationpr5_  | eq_ref | PRIMARY                            | PRIMARY | 304     | validation2.user2_.ID_COMPANY_VACATION_PROFILE,validation2.user2_.ID_VACATION_PROFILE                                                 |    1 |   100.00 | Using index                                     |
|    1 | SIMPLE      | vacationac0_  | ref    | PRIMARY,I_VACATION_ACCUMULATION_EA | PRIMARY | 304     | const,validation2.user2_.ID_USER                                                                                                      |    5 |   100.00 | Using where                                     |
|    1 | SIMPLE      | vacationty3_  | eq_ref | PRIMARY                            | PRIMARY | 304     | const,validation2.vacationac0_.ID_VACATION_TYPE                                                                                       |    1 |   100.00 | Using where                                     |
|    1 | SIMPLE      | vacationst6_  | eq_ref | PRIMARY                            | PRIMARY | 608     | validation2.user2_.ID_COMPANY_VACATION_PROFILE,validation2.user2_.ID_VACATION_PROFILE,const,validation2.vacationac0_.ID_VACATION_TYPE |    1 |   100.00 | Using where                                     |
|    1 | SIMPLE      | translatio9_  | eq_ref | PRIMARY                            | PRIMARY | 919     | validation2.vacationty3_.ID_COMPANY_TRANSLATION,validation2.vacationty3_.ID_TRANSLATION                                               |    1 |   100.00 | Using index                                     |
|    1 | SIMPLE      | descriptio10_ | eq_ref | PRIMARY,                           | PRIMARY | 951     | validation2.vacationty3_.ID_COMPANY_TRANSLATION,validation2.vacationty3_.ID_TRANSLATION,const                                         |    1 |   100.00 | Using where                                     |
|    1 | SIMPLE      | listvalue4_   | ALL    | NULL                               | NULL    | NULL    | NULL                                                                                                                                  | 5282 |   100.00 | Using where; Using join buffer (flat, BNL join) |
|    1 | SIMPLE      | translatio7_  | eq_ref | PRIMARY                            | PRIMARY | 919     | validation2.listvalue4_.ID_COMPANY_TRANSLATION,validation2.listvalue4_.ID_TRANSLATION                                                 |    1 |   100.00 | Using index                                     |
|    1 | SIMPLE      | descriptio8_  | eq_ref | PRIMARY,                           | PRIMARY | 951     | validation2.listvalue4_.ID_COMPANY_TRANSLATION,validation2.listvalue4_.ID_TRANSLATION,const                                           |    1 |   100.00 | Using where                                     |
+------+-------------+---------------+--------+------------------------------------+---------+---------+---------------------------------------------------------------------------------------------------------------------------------------+------+----------+-------------------------------------------------+

我如何强制使用 连接缓冲区(平面,BNL 连接) 第一个环境是生产环境,有更多的内存和 CPU。

在第一个环境中:

join_buffer_size............ 16777216
join_buffer_space_limit..... 2097152

第二个环境:

join_buffer_size............ 262144
join_buffer_space_limit..... 2097152

join_buffer_size和join_buffer_space_limit之间有没有link/ratio? 我们在 join_buffer_size 上配置 16Mo,因为它是一个 mysqlTuner 提示。

我将 join_buffer_space_limit 设置为 128Mo,它解决了性能问题。 所以 mysqlTuner 没有给出这个配置键的提示。

SET GLOBAL join_buffer_space_limit = 1024 * 1024 * 128; 

提高性能需要时间(小时)。

https://mariadb.com/kb/en/library/multi-range-read-optimization/