模拟优化参数optimizer_switch选项mrr(Multi_Range Read)对查询优化的影响

11.1背景:模拟优化参数optimizer_switch选项mrr(Multi_Range Read)对查询优化的影响

11.2建表语句

mysql> show create table order_line\G;

*************************** 1. row ***************************

Table: order_line

Create Table: CREATE TABLE `order_line` (

`ol_o_id` int(11) NOT NULL,

`ol_d_id` tinyint(4) NOT NULL,

`ol_w_id` smallint(6) NOT NULL,

`ol_number` tinyint(4) NOT NULL,

`ol_i_id` int(11) DEFAULT NULL,

`ol_supply_w_id` smallint(6) DEFAULT NULL,

`ol_delivery_d` datetime DEFAULT NULL,

`ol_quantity` tinyint(4) DEFAULT NULL,

`ol_amount` decimal(6,2) DEFAULT NULL,

`ol_dist_info` char(24) DEFAULT NULL,

PRIMARY KEY (`ol_w_id`,`ol_d_id`,`ol_o_id`,`ol_number`),

KEY `fkey_order_line_2` (`ol_supply_w_id`,`ol_i_id`),

CONSTRAINT `fkey_order_line_1` FOREIGN KEY (`ol_w_id`, `ol_d_id`, `ol_o_id`) REFERENCES `orders` (`o_w_id`, `o_d_id`, `o_id`),

CONSTRAINT `fkey_order_line_2` FOREIGN KEY (`ol_supply_w_id`, `ol_i_id`) REFERENCES `stock` (`s_w_id`, `s_i_id`)

) ENGINE=InnoDB DEFAULT CHARSET=latin1

1 row in set (0.00 sec)

11.3查询语句

select * from order_line where (ol_supply_w_id > 0 and ol_supply_w_id <6 ) and (ol_i_id =90000);

11.4示例及分析:

11.4.1源语句执行计划分析

mysql> explain extended select * from order_line where (ol_supply_w_id > 0 and ol_supply_w_id <6 ) and (ol_i_id =90000);

+----+-------------+------------+------+-------------------+------+---------+------+---------+----------+-------------+

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

+----+-------------+------------+------+-------------------+------+---------+------+---------+----------+-------------+

|  1 | SIMPLE      | order_line | ALL  | fkey_order_line_2 | NULL | NULL    | NULL | 1701558 |    50.00 | Using where |

+----+-------------+------------+------+-------------------+------+---------+------+---------+----------+-------------+

1 row in set, 1 warning (0.00 sec)

mysql> show warnings\G;

*************************** 1. row ***************************

Level: Note

Code: 1003

Message: /* select#1 */ select `tpcc`.`order_line`.`ol_o_id` AS `ol_o_id`,`tpcc`.`order_line`.`ol_d_id` AS `ol_d_id`,`tpcc`.`order_line`.`ol_w_id` AS `ol_w_id`,`tpcc`.`order_line`.`ol_number` AS `ol_number`,`tpcc`.`order_line`.`ol_i_id` AS `ol_i_id`,`tpcc`.`order_line`.`ol_supply_w_id` AS `ol_supply_w_id`,`tpcc`.`order_line`.`ol_delivery_d` AS `ol_delivery_d`,`tpcc`.`order_line`.`ol_quantity` AS `ol_quantity`,`tpcc`.`order_line`.`ol_amount` AS `ol_amount`,`tpcc`.`order_line`.`ol_dist_info` AS `ol_dist_info` from `tpcc`.`order_line` where ((`tpcc`.`order_line`.`ol_i_id` = 90000) and (`tpcc`.`order_line`.`ol_supply_w_id` > 0) and (`tpcc`.`order_line`.`ol_supply_w_id` < 6))

1 row in set (0.00 sec)

该语句执行了全表扫描。

11.4.2关闭mrr对执行计划的影响

注释掉快速预热缓冲池功能

#innodb_buffer_pool_dump_at_shutdown=1

#innodb_buffer_pool_dump_now=1

#innodb_buffer_pool_load_at_start=1

#innodb_buffer_pool_load_now=1

重启数据库示例

关闭mrr对数据查询的影响

mysql> set @@optimizer_switch=‘mrr=off,mrr_cost_based=on‘;

Query OK, 0 rows affected (0.00 sec)

mysql> explain extended select * from order_line where (ol_supply_w_id > 0 and ol_supply_w_id <6 ) and (ol_i_id =90000);

+----+-------------+------------+------+-------------------+------+---------+------+---------+----------+-------------+

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

+----+-------------+------------+------+-------------------+------+---------+------+---------+----------+-------------+

|  1 | SIMPLE      | order_line | ALL  | fkey_order_line_2 | NULL | NULL    | NULL | 1638777 |    50.00 | Using where |

+----+-------------+------------+------+-------------------+------+---------+------+---------+----------+-------------+

1 row in set, 1 warning (0.00 sec)

执行全表扫描。

mysql> select * from order_line where (ol_supply_w_id > 0 and ol_supply_w_id <6 ) and (ol_i_id =90000);

+---------+---------+---------+-----------+---------+----------------+---------------------+-------------+-----------+--------------------------+

| ol_o_id | ol_d_id | ol_w_id | ol_number | ol_i_id | ol_supply_w_id | ol_delivery_d       | ol_quantity | ol_amount | ol_dist_info             |

+---------+---------+---------+-----------+---------+----------------+---------------------+-------------+-----------+--------------------------+

|    2124 |       7 |       2 |         5 |   90000 |              2 | 2014-07-24 16:12:33 |           5 |      0.00 | 3dvrSdvNlCswM5astj4EBmCd |

|    2833 |      10 |       2 |         4 |   90000 |              2 | NULL                |           5 |      0.00 | jdJuW0MobORbaxEKWW5HWqK9 |

|    3268 |       2 |       3 |        11 |   90000 |              3 | NULL                |           3 |    127.62 | HklNywWs02ab8XGFID2HpjK4 |

|    2838 |       3 |       3 |         5 |   90000 |              3 | NULL                |           5 |      0.00 | DtqfRbDNgmJLH7Vn0xvk6uTS |

|     653 |       5 |       3 |        12 |   90000 |              3 | 2014-07-24 15:52:26 |           5 |     80.73 | QIBcVanwiqRhxVJPckA7KEbv |

|     909 |       5 |       3 |         1 |   90000 |              3 | 2014-07-24 15:52:26 |           5 |     74.72 | zcl2wdh3RGEdI8EhgOHhR8wT |

|    1129 |       5 |       3 |         5 |   90000 |              3 | 2014-07-24 15:52:26 |           5 |     62.02 | JtP5oa0koNxc2BPRoppNt98J |

|    1740 |       6 |       3 |        11 |   90000 |              3 | 2014-07-24 15:52:26 |           5 |      1.16 | 5rY0GnyMEPnQsitnTO5o5Y1I |

|    1306 |       9 |       3 |         1 |   90000 |              3 | 2014-07-24 15:52:26 |           5 |     64.57 | P8pWwOmvEZKNWCWEyuxPl4Z3 |

|    1203 |      10 |       3 |         1 |   90000 |              3 | 2014-07-24 15:52:26 |           5 |     33.59 | iCDsA2a62VtLQnUBvPGSTA5E |

|    3014 |       1 |       4 |         5 |   90000 |              4 | NULL                |           8 |    551.15 | hJjs6oxAamoY5ZrhW2oboiIY |

|     840 |       5 |       4 |         9 |   90000 |              4 | 2014-07-24 15:52:26 |           5 |     55.06 | D85SGasZQaeFzFnpFOmOdtAW |

|    2444 |       7 |       4 |         7 |   90000 |              4 | NULL                |           5 |      0.00 | 5RUMFQ9PDgfMyM1kPfnJayJu |

|    2577 |       4 |       5 |         3 |   90000 |              5 | NULL                |           5 |      0.00 | 1sqa7AzfsPurzqvMHnpnGuKz |

|     620 |       6 |       5 |         6 |   90000 |              5 | 2014-07-24 15:52:26 |           5 |     72.78 | vskOqsHuimM1gd58oPxKoGA0 |

|    1247 |       8 |       5 |         9 |   90000 |              5 | 2014-07-24 15:52:26 |           5 |     89.96 | v0c9QGlqZwD9Mg9GtswM3f6q |

|    2628 |       9 |       5 |         9 |   90000 |              5 | NULL                |           5 |      0.00 | LCLh9GhbMoJo9cXNk1Rhb9aX |

+---------+---------+---------+-----------+---------+----------------+---------------------+-------------+-----------+--------------------------+

17 rows in set (6.63 sec)

所花费时间为6.63 sec。

增加限制条件,使其使用索引。

mysql> explain extended select * from order_line where (ol_supply_w_id > 0 and ol_supply_w_id <6 ) and (ol_i_id =90000) limit 1701557;

+----+-------------+------------+-------+-------------------+-------------------+---------+------+--------+----------+-----------------------+

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

+----+-------------+------------+-------+-------------------+-------------------+---------+------+--------+----------+-----------------------+

|  1 | SIMPLE      | order_line | range | fkey_order_line_2 | fkey_order_line_2 | 3       | NULL | 850779 |   100.00 | Using index condition |

+----+-------------+------------+-------+-------------------+-------------------+---------+------+--------+----------+-----------------------+

1 row in set, 1 warning (0.00 sec)

mysql> show warnings\G;

*************************** 1. row ***************************

Level: Note

Code: 1003

Message: /* select#1 */ select `tpcc`.`order_line`.`ol_o_id` AS `ol_o_id`,`tpcc`.`order_line`.`ol_d_id` AS `ol_d_id`,`tpcc`.`order_line`.`ol_w_id` AS `ol_w_id`,`tpcc`.`order_line`.`ol_number` AS `ol_number`,`tpcc`.`order_line`.`ol_i_id` AS `ol_i_id`,`tpcc`.`order_line`.`ol_supply_w_id` AS `ol_supply_w_id`,`tpcc`.`order_line`.`ol_delivery_d` AS `ol_delivery_d`,`tpcc`.`order_line`.`ol_quantity` AS `ol_quantity`,`tpcc`.`order_line`.`ol_amount` AS `ol_amount`,`tpcc`.`order_line`.`ol_dist_info` AS `ol_dist_info` from `tpcc`.`order_line` where ((`tpcc`.`order_line`.`ol_i_id` = 90000) and (`tpcc`.`order_line`.`ol_supply_w_id` > 0) and (`tpcc`.`order_line`.`ol_supply_w_id` < 6)) limit 1500000

1 row in set (0.00 sec)

mysql> select * from order_line where (ol_supply_w_id > 0 and ol_supply_w_id <6 ) and (ol_i_id =90000) limit 1701557;

+---------+---------+---------+-----------+---------+----------------+---------------------+-------------+-----------+--------------------------+

| ol_o_id | ol_d_id | ol_w_id | ol_number | ol_i_id | ol_supply_w_id | ol_delivery_d       | ol_quantity | ol_amount | ol_dist_info             |

+---------+---------+---------+-----------+---------+----------------+---------------------+-------------+-----------+--------------------------+

|    2124 |       7 |       2 |         5 |   90000 |              2 | 2014-07-24 16:12:33 |           5 |      0.00 | 3dvrSdvNlCswM5astj4EBmCd |

|    2833 |      10 |       2 |         4 |   90000 |              2 | NULL                |           5 |      0.00 | jdJuW0MobORbaxEKWW5HWqK9 |

|    3268 |       2 |       3 |        11 |   90000 |              3 | NULL                |           3 |    127.62 | HklNywWs02ab8XGFID2HpjK4 |

|    2838 |       3 |       3 |         5 |   90000 |              3 | NULL                |           5 |      0.00 | DtqfRbDNgmJLH7Vn0xvk6uTS |

|     653 |       5 |       3 |        12 |   90000 |              3 | 2014-07-24 15:52:26 |           5 |     80.73 | QIBcVanwiqRhxVJPckA7KEbv |

|     909 |       5 |       3 |         1 |   90000 |              3 | 2014-07-24 15:52:26 |           5 |     74.72 | zcl2wdh3RGEdI8EhgOHhR8wT |

|    1129 |       5 |       3 |         5 |   90000 |              3 | 2014-07-24 15:52:26 |           5 |     62.02 | JtP5oa0koNxc2BPRoppNt98J |

|    1740 |       6 |       3 |        11 |   90000 |              3 | 2014-07-24 15:52:26 |           5 |      1.16 | 5rY0GnyMEPnQsitnTO5o5Y1I |

|    1306 |       9 |       3 |         1 |   90000 |              3 | 2014-07-24 15:52:26 |           5 |     64.57 | P8pWwOmvEZKNWCWEyuxPl4Z3 |

|    1203 |      10 |       3 |         1 |   90000 |              3 | 2014-07-24 15:52:26 |           5 |     33.59 | iCDsA2a62VtLQnUBvPGSTA5E |

|    3014 |       1 |       4 |         5 |   90000 |              4 | NULL                |           8 |    551.15 | hJjs6oxAamoY5ZrhW2oboiIY |

|     840 |       5 |       4 |         9 |   90000 |              4 | 2014-07-24 15:52:26 |           5 |     55.06 | D85SGasZQaeFzFnpFOmOdtAW |

|    2444 |       7 |       4 |         7 |   90000 |              4 | NULL                |           5 |      0.00 | 5RUMFQ9PDgfMyM1kPfnJayJu |

|    2577 |       4 |       5 |         3 |   90000 |              5 | NULL                |           5 |      0.00 | 1sqa7AzfsPurzqvMHnpnGuKz |

|     620 |       6 |       5 |         6 |   90000 |              5 | 2014-07-24 15:52:26 |           5 |     72.78 | vskOqsHuimM1gd58oPxKoGA0 |

|    1247 |       8 |       5 |         9 |   90000 |              5 | 2014-07-24 15:52:26 |           5 |     89.96 | v0c9QGlqZwD9Mg9GtswM3f6q |

|    2628 |       9 |       5 |         9 |   90000 |              5 | NULL                |           5 |      0.00 | LCLh9GhbMoJo9cXNk1Rhb9aX |

+---------+---------+---------+-----------+---------+----------------+---------------------+-------------+-----------+--------------------------+

17 rows in set (2.38 sec)

所花费时间为2.38sec。

11.4.2限制条件“limit n”的选择:

(1)limit n限制条件结果

mysql> select * from order_line where (ol_supply_w_id > 0 and ol_supply_w_id <6 ) and (ol_i_id =90000) limit 1;

+---------+---------+---------+-----------+---------+----------------+---------------------+-------------+-----------+--------------------------+

| ol_o_id | ol_d_id | ol_w_id | ol_number | ol_i_id | ol_supply_w_id | ol_delivery_d       | ol_quantity | ol_amount | ol_dist_info             |

+---------+---------+---------+-----------+---------+----------------+---------------------+-------------+-----------+--------------------------+

|    2124 |       7 |       2 |         5 |   90000 |              2 | 2014-07-24 16:12:33 |           5 |      0.00 | 3dvrSdvNlCswM5astj4EBmCd |

+---------+---------+---------+-----------+---------+----------------+---------------------+-------------+-----------+--------------------------+

1 row in set (0.18 sec)

limit是对查询结果后的第一条记录的限制。

mysql> select * from order_line where (ol_supply_w_id > 0 and ol_supply_w_id <6 ) and (ol_i_id =90000) limit 2;

+---------+---------+---------+-----------+---------+----------------+---------------------+-------------+-----------+--------------------------+

| ol_o_id | ol_d_id | ol_w_id | ol_number | ol_i_id | ol_supply_w_id | ol_delivery_d       | ol_quantity | ol_amount | ol_dist_info             |

+---------+---------+---------+-----------+---------+----------------+---------------------+-------------+-----------+--------------------------+

|    2124 |       7 |       2 |         5 |   90000 |              2 | 2014-07-24 16:12:33 |           5 |      0.00 | 3dvrSdvNlCswM5astj4EBmCd |

|    2833 |      10 |       2 |         4 |   90000 |              2 | NULL                |           5 |      0.00 | jdJuW0MobORbaxEKWW5HWqK9 |

+---------+---------+---------+-----------+---------+----------------+---------------------+-------------+-----------+--------------------------+

2 rows in set (0.19 sec)

limit是对查询结果后的前二条记录的限制。

mysql> select * from order_line where (ol_supply_w_id > 0 and ol_supply_w_id <6 ) and (ol_i_id =90000) limit 17;

+---------+---------+---------+-----------+---------+----------------+---------------------+-------------+-----------+--------------------------+

| ol_o_id | ol_d_id | ol_w_id | ol_number | ol_i_id | ol_supply_w_id | ol_delivery_d       | ol_quantity | ol_amount | ol_dist_info             |

+---------+---------+---------+-----------+---------+----------------+---------------------+-------------+-----------+--------------------------+

|    2124 |       7 |       2 |         5 |   90000 |              2 | 2014-07-24 16:12:33 |           5 |      0.00 | 3dvrSdvNlCswM5astj4EBmCd |

|    2833 |      10 |       2 |         4 |   90000 |              2 | NULL                |           5 |      0.00 | jdJuW0MobORbaxEKWW5HWqK9 |

|    3268 |       2 |       3 |        11 |   90000 |              3 | NULL                |           3 |    127.62 | HklNywWs02ab8XGFID2HpjK4 |

|    2838 |       3 |       3 |         5 |   90000 |              3 | NULL                |           5 |      0.00 | DtqfRbDNgmJLH7Vn0xvk6uTS |

|     653 |       5 |       3 |        12 |   90000 |              3 | 2014-07-24 15:52:26 |           5 |     80.73 | QIBcVanwiqRhxVJPckA7KEbv |

|     909 |       5 |       3 |         1 |   90000 |              3 | 2014-07-24 15:52:26 |           5 |     74.72 | zcl2wdh3RGEdI8EhgOHhR8wT |

|    1129 |       5 |       3 |         5 |   90000 |              3 | 2014-07-24 15:52:26 |           5 |     62.02 | JtP5oa0koNxc2BPRoppNt98J |

|    1740 |       6 |       3 |        11 |   90000 |              3 | 2014-07-24 15:52:26 |           5 |      1.16 | 5rY0GnyMEPnQsitnTO5o5Y1I |

|    1306 |       9 |       3 |         1 |   90000 |              3 | 2014-07-24 15:52:26 |           5 |     64.57 | P8pWwOmvEZKNWCWEyuxPl4Z3 |

|    1203 |      10 |       3 |         1 |   90000 |              3 | 2014-07-24 15:52:26 |           5 |     33.59 | iCDsA2a62VtLQnUBvPGSTA5E |

|    3014 |       1 |       4 |         5 |   90000 |              4 | NULL                |           8 |    551.15 | hJjs6oxAamoY5ZrhW2oboiIY |

|     840 |       5 |       4 |         9 |   90000 |              4 | 2014-07-24 15:52:26 |           5 |     55.06 | D85SGasZQaeFzFnpFOmOdtAW |

|    2444 |       7 |       4 |         7 |   90000 |              4 | NULL                |           5 |      0.00 | 5RUMFQ9PDgfMyM1kPfnJayJu |

|    2577 |       4 |       5 |         3 |   90000 |              5 | NULL                |           5 |      0.00 | 1sqa7AzfsPurzqvMHnpnGuKz |

|     620 |       6 |       5 |         6 |   90000 |              5 | 2014-07-24 15:52:26 |           5 |     72.78 | vskOqsHuimM1gd58oPxKoGA0 |

|    1247 |       8 |       5 |         9 |   90000 |              5 | 2014-07-24 15:52:26 |           5 |     89.96 | v0c9QGlqZwD9Mg9GtswM3f6q |

|    2628 |       9 |       5 |         9 |   90000 |              5 | NULL                |           5 |      0.00 | LCLh9GhbMoJo9cXNk1Rhb9aX |

+---------+---------+---------+-----------+---------+----------------+---------------------+-------------+-----------+--------------------------+

17 rows in set (0.45 sec)

mysql> select * from order_line where (ol_supply_w_id > 0 and ol_supply_w_id <6 ) and (ol_i_id =90000) limit 18;

+---------+---------+---------+-----------+---------+----------------+---------------------+-------------+-----------+--------------------------+

| ol_o_id | ol_d_id | ol_w_id | ol_number | ol_i_id | ol_supply_w_id | ol_delivery_d       | ol_quantity | ol_amount | ol_dist_info             |

+---------+---------+---------+-----------+---------+----------------+---------------------+-------------+-----------+--------------------------+

|    2124 |       7 |       2 |         5 |   90000 |              2 | 2014-07-24 16:12:33 |           5 |      0.00 | 3dvrSdvNlCswM5astj4EBmCd |

|    2833 |      10 |       2 |         4 |   90000 |              2 | NULL                |           5 |      0.00 | jdJuW0MobORbaxEKWW5HWqK9 |

|    3268 |       2 |       3 |        11 |   90000 |              3 | NULL                |           3 |    127.62 | HklNywWs02ab8XGFID2HpjK4 |

|    2838 |       3 |       3 |         5 |   90000 |              3 | NULL                |           5 |      0.00 | DtqfRbDNgmJLH7Vn0xvk6uTS |

|     653 |       5 |       3 |        12 |   90000 |              3 | 2014-07-24 15:52:26 |           5 |     80.73 | QIBcVanwiqRhxVJPckA7KEbv |

|     909 |       5 |       3 |         1 |   90000 |              3 | 2014-07-24 15:52:26 |           5 |     74.72 | zcl2wdh3RGEdI8EhgOHhR8wT |

|    1129 |       5 |       3 |         5 |   90000 |              3 | 2014-07-24 15:52:26 |           5 |     62.02 | JtP5oa0koNxc2BPRoppNt98J |

|    1740 |       6 |       3 |        11 |   90000 |              3 | 2014-07-24 15:52:26 |           5 |      1.16 | 5rY0GnyMEPnQsitnTO5o5Y1I |

|    1306 |       9 |       3 |         1 |   90000 |              3 | 2014-07-24 15:52:26 |           5 |     64.57 | P8pWwOmvEZKNWCWEyuxPl4Z3 |

|    1203 |      10 |       3 |         1 |   90000 |              3 | 2014-07-24 15:52:26 |           5 |     33.59 | iCDsA2a62VtLQnUBvPGSTA5E |

|    3014 |       1 |       4 |         5 |   90000 |              4 | NULL                |           8 |    551.15 | hJjs6oxAamoY5ZrhW2oboiIY |

|     840 |       5 |       4 |         9 |   90000 |              4 | 2014-07-24 15:52:26 |           5 |     55.06 | D85SGasZQaeFzFnpFOmOdtAW |

|    2444 |       7 |       4 |         7 |   90000 |              4 | NULL                |           5 |      0.00 | 5RUMFQ9PDgfMyM1kPfnJayJu |

|    2577 |       4 |       5 |         3 |   90000 |              5 | NULL                |           5 |      0.00 | 1sqa7AzfsPurzqvMHnpnGuKz |

|     620 |       6 |       5 |         6 |   90000 |              5 | 2014-07-24 15:52:26 |           5 |     72.78 | vskOqsHuimM1gd58oPxKoGA0 |

|    1247 |       8 |       5 |         9 |   90000 |              5 | 2014-07-24 15:52:26 |           5 |     89.96 | v0c9QGlqZwD9Mg9GtswM3f6q |

|    2628 |       9 |       5 |         9 |   90000 |              5 | NULL                |           5 |      0.00 | LCLh9GhbMoJo9cXNk1Rhb9aX |

+---------+---------+---------+-----------+---------+----------------+---------------------+-------------+-----------+--------------------------+

17 rows in set (0.44 sec)

limit是对查询结果后的所有记录的限制。

(2)limit n限制条件规律探索

由此,在不知道有多少查询记录的情况下,可以使用:

mysql> explain extended select * from order_line where (ol_supply_w_id > 0 and ol_supply_w_id <6 ) and (ol_i_id =90000);

+----+-------------+------------+------+-------------------+------+---------+------+---------+----------+-------------+

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

+----+-------------+------------+------+-------------------+------+---------+------+---------+----------+-------------+

|  1 | SIMPLE      | order_line | ALL  | fkey_order_line_2 | NULL | NULL    | NULL | 1701558 |    50.00 | Using where |

+----+-------------+------------+------+-------------------+------+---------+------+---------+----------+-------------+

1 row in set, 1 warning (0.00 sec)

可以看到影响的行数为1701558,那么使用1701558看执行计划的结果:

mysql> explain extended select * from order_line where (ol_supply_w_id > 0 and ol_supply_w_id <6 ) and (ol_i_id =90000) limit 1701558;

+----+-------------+------------+------+-------------------+------+---------+------+---------+----------+-------------+

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

+----+-------------+------------+------+-------------------+------+---------+------+---------+----------+-------------+

|  1 | SIMPLE      | order_line | ALL  | fkey_order_line_2 | NULL | NULL    | NULL | 1701558 |    50.00 | Using where |

+----+-------------+------------+------+-------------------+------+---------+------+---------+----------+-------------+

1 row in set, 1 warning (0.00 sec)

使用n-1看执行计划的结果:

mysql> explain extended select * from order_line where (ol_supply_w_id > 0 and ol_supply_w_id <6 ) and (ol_i_id =90000) limit 1701557;

+----+-------------+------------+-------+-------------------+-------------------+---------+------+--------+----------+-----------------------+

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

+----+-------------+------------+-------+-------------------+-------------------+---------+------+--------+----------+-----------------------+

|  1 | SIMPLE      | order_line | range | fkey_order_line_2 | fkey_order_line_2 | 3       | NULL | 850779 |   100.00 | Using index condition |

+----+-------------+------------+-------+-------------------+-------------------+---------+------+--------+----------+-----------------------+

1 row in set, 1 warning (0.00 sec)

11.4.3打开mrr对数据查询的影响

重启数据库

打开mrr功能

mysql> set @@optimizer_switch=‘mrr=on,mrr_cost_based=off‘;

Query OK, 0 rows affected (0.01 sec)

mysql> explain extended select * from order_line where (ol_supply_w_id > 0 and ol_supply_w_id <6 ) and (ol_i_id =90000);

+----+-------------+------------+------+-------------------+------+---------+------+---------+----------+-------------+

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

+----+-------------+------------+------+-------------------+------+---------+------+---------+----------+-------------+

|  1 | SIMPLE      | order_line | ALL  | fkey_order_line_2 | NULL | NULL    | NULL | 1604617 |    50.00 | Using where |

+----+-------------+------------+------+-------------------+------+---------+------+---------+----------+-------------+

1 row in set, 1 warning (0.01 sec)

mysql> show warnings\G;

*************************** 1. row ***************************

Level: Note

Code: 1003

Message: /* select#1 */ select `tpcc`.`order_line`.`ol_o_id` AS `ol_o_id`,`tpcc`.`order_line`.`ol_d_id` AS `ol_d_id`,`tpcc`.`order_line`.`ol_w_id` AS `ol_w_id`,`tpcc`.`order_line`.`ol_number` AS `ol_number`,`tpcc`.`order_line`.`ol_i_id` AS `ol_i_id`,`tpcc`.`order_line`.`ol_supply_w_id` AS `ol_supply_w_id`,`tpcc`.`order_line`.`ol_delivery_d` AS `ol_delivery_d`,`tpcc`.`order_line`.`ol_quantity` AS `ol_quantity`,`tpcc`.`order_line`.`ol_amount` AS `ol_amount`,`tpcc`.`order_line`.`ol_dist_info` AS `ol_dist_info` from `tpcc`.`order_line` where ((`tpcc`.`order_line`.`ol_i_id` = 90000) and (`tpcc`.`order_line`.`ol_supply_w_id` > 0) and (`tpcc`.`order_line`.`ol_supply_w_id` < 6))

1 row in set (0.01 sec)

使用的是全表扫描。

mysql> explain extended select * from order_line where (ol_supply_w_id > 0 and ol_supply_w_id <6 ) and (ol_i_id =90000) limit 1604617;

+----+-------------+------------+------+-------------------+------+---------+------+---------+----------+-------------+

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

+----+-------------+------------+------+-------------------+------+---------+------+---------+----------+-------------+

|  1 | SIMPLE      | order_line | ALL  | fkey_order_line_2 | NULL | NULL    | NULL | 1604617 |    50.00 | Using where |

+----+-------------+------------+------+-------------------+------+---------+------+---------+----------+-------------+

1 row in set, 1 warning (0.01 sec)

mysql> explain extended select * from order_line where (ol_supply_w_id > 0 and ol_supply_w_id <6 ) and (ol_i_id =90000) limit 1604616;

+----+-------------+------------+-------+-------------------+-------------------+---------+------+--------+----------+----------------------------------+

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

+----+-------------+------------+-------+-------------------+-------------------+---------+------+--------+----------+----------------------------------+

|  1 | SIMPLE      | order_line | range | fkey_order_line_2 | fkey_order_line_2 | 3       | NULL | 802308 |   100.00 | Using index condition; Using MRR |

+----+-------------+------------+-------+-------------------+-------------------+---------+------+--------+----------+----------------------------------+

1 row in set, 1 warning (0.00 sec)

mysql> show warnings\G;

*************************** 1. row ***************************

Level: Note

Code: 1003

Message: /* select#1 */ select `tpcc`.`order_line`.`ol_o_id` AS `ol_o_id`,`tpcc`.`order_line`.`ol_d_id` AS `ol_d_id`,`tpcc`.`order_line`.`ol_w_id` AS `ol_w_id`,`tpcc`.`order_line`.`ol_number` AS `ol_number`,`tpcc`.`order_line`.`ol_i_id` AS `ol_i_id`,`tpcc`.`order_line`.`ol_supply_w_id` AS `ol_supply_w_id`,`tpcc`.`order_line`.`ol_delivery_d` AS `ol_delivery_d`,`tpcc`.`order_line`.`ol_quantity` AS `ol_quantity`,`tpcc`.`order_line`.`ol_amount` AS `ol_amount`,`tpcc`.`order_line`.`ol_dist_info` AS `ol_dist_info` from `tpcc`.`order_line` where ((`tpcc`.`order_line`.`ol_i_id` = 90000) and (`tpcc`.`order_line`.`ol_supply_w_id` > 0) and (`tpcc`.`order_line`.`ol_supply_w_id` < 6)) limit 1604616

1 row in set (0.00 sec)

可以看到使用了索引和mrr优化参数

mysql> select * from order_line where (ol_supply_w_id > 0 and ol_supply_w_id <6 ) and (ol_i_id =90000) limit 1604616;

+---------+---------+---------+-----------+---------+----------------+---------------------+-------------+-----------+--------------------------+

| ol_o_id | ol_d_id | ol_w_id | ol_number | ol_i_id | ol_supply_w_id | ol_delivery_d       | ol_quantity | ol_amount | ol_dist_info             |

+---------+---------+---------+-----------+---------+----------------+---------------------+-------------+-----------+--------------------------+

|    2124 |       7 |       2 |         5 |   90000 |              2 | 2014-07-24 16:12:33 |           5 |      0.00 | 3dvrSdvNlCswM5astj4EBmCd |

|    2833 |      10 |       2 |         4 |   90000 |              2 | NULL                |           5 |      0.00 | jdJuW0MobORbaxEKWW5HWqK9 |

|    3268 |       2 |       3 |        11 |   90000 |              3 | NULL                |           3 |    127.62 | HklNywWs02ab8XGFID2HpjK4 |

|    2838 |       3 |       3 |         5 |   90000 |              3 | NULL                |           5 |      0.00 | DtqfRbDNgmJLH7Vn0xvk6uTS |

|     653 |       5 |       3 |        12 |   90000 |              3 | 2014-07-24 15:52:26 |           5 |     80.73 | QIBcVanwiqRhxVJPckA7KEbv |

|     909 |       5 |       3 |         1 |   90000 |              3 | 2014-07-24 15:52:26 |           5 |     74.72 | zcl2wdh3RGEdI8EhgOHhR8wT |

|    1129 |       5 |       3 |         5 |   90000 |              3 | 2014-07-24 15:52:26 |           5 |     62.02 | JtP5oa0koNxc2BPRoppNt98J |

|    1740 |       6 |       3 |        11 |   90000 |              3 | 2014-07-24 15:52:26 |           5 |      1.16 | 5rY0GnyMEPnQsitnTO5o5Y1I |

|    1306 |       9 |       3 |         1 |   90000 |              3 | 2014-07-24 15:52:26 |           5 |     64.57 | P8pWwOmvEZKNWCWEyuxPl4Z3 |

|    1203 |      10 |       3 |         1 |   90000 |              3 | 2014-07-24 15:52:26 |           5 |     33.59 | iCDsA2a62VtLQnUBvPGSTA5E |

|    3014 |       1 |       4 |         5 |   90000 |              4 | NULL                |           8 |    551.15 | hJjs6oxAamoY5ZrhW2oboiIY |

|     840 |       5 |       4 |         9 |   90000 |              4 | 2014-07-24 15:52:26 |           5 |     55.06 | D85SGasZQaeFzFnpFOmOdtAW |

|    2444 |       7 |       4 |         7 |   90000 |              4 | NULL                |           5 |      0.00 | 5RUMFQ9PDgfMyM1kPfnJayJu |

|    2577 |       4 |       5 |         3 |   90000 |              5 | NULL                |           5 |      0.00 | 1sqa7AzfsPurzqvMHnpnGuKz |

|     620 |       6 |       5 |         6 |   90000 |              5 | 2014-07-24 15:52:26 |           5 |     72.78 | vskOqsHuimM1gd58oPxKoGA0 |

|    1247 |       8 |       5 |         9 |   90000 |              5 | 2014-07-24 15:52:26 |           5 |     89.96 | v0c9QGlqZwD9Mg9GtswM3f6q |

|    2628 |       9 |       5 |         9 |   90000 |              5 | NULL                |           5 |      0.00 | LCLh9GhbMoJo9cXNk1Rhb9aX |

+---------+---------+---------+-----------+---------+----------------+---------------------+-------------+-----------+--------------------------+

17 rows in set (0.44 sec)

所花费时间为0.94sec,比没有启用mrr缩短了近2sec,原先的是2.38sec。

11.5总结

(1)在索引的选择率影响索引的选择时,增加limit限制,使其使用索引,从而提高查询效率;

(2)limit n中的n的选择还有待进一步验证和探讨;

(3)MRR的使用的优化目的是减少磁盘的随机访问,其前提是使用索引,找到所需的行,并按照主键进行排序,并将随机访问转换为较为顺序的数据访问,其优化使用于range、ref和eq_ref类型的查询,也就是说非这些类型的查询,优化也就失去了作用:

mysql> explain extended select * from order_line where (ol_supply_w_id > 0 and ol_supply_w_id <6 ) and (ol_i_id =90000);

+----+-------------+------------+------+-------------------+------+---------+------+---------+----------+-------------+

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

+----+-------------+------------+------+-------------------+------+---------+------+---------+----------+-------------+

|  1 | SIMPLE      | order_line | ALL  | fkey_order_line_2 | NULL | NULL    | NULL | 1604617 |    50.00 | Using where |

+----+-------------+------------+------+-------------------+------+---------+------+---------+----------+-------------+

1 row in set, 1 warning (0.00 sec)

可以看到,仍然是全表扫描。

时间: 2024-10-10 08:47:29

模拟优化参数optimizer_switch选项mrr(Multi_Range Read)对查询优化的影响的相关文章

MySql常用命令--优化参数以及日常管理

显示插入查询的优化参数: show variables like "concurrent_insert"; +-------------------+-------+ | Variable_name | Value | +-------------------+-------+ | concurrent_insert | AUTO | +-------------------+-------+ set GLOBAL concurrent_insert=2; 插入数据时MySQL会对插入

DevExpress.XtraNavBar.NavBarControl 模拟单击导航的选项事件

/// <summary> /// DevExpress.XtraNavBar.NavBarControl 模拟单击导航的选项事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void simpleButton1_Click(object sender, EventAr

ie9 中出现不明的异常(参数是必选项 (Argument not optional)、尚未实现)等

<script type="text/javascript"> function addFilter(aaa) { alert(aaa); } </script> <input type="button" onclick="addFilter();" value='button'/> 以上代码在ie9中运行时,会报错误:参数是必选项(Argument not optional) <script type=

php-fpm优化参数介绍

1.php-fpm优化参数介绍他们分别是:pm.pm.max_children.pm.start_servers.pm.min_spare_servers.pm.max_spare_servers. pm:表示使用那种方式,有两个值可以选择,就是static(静态)或者dynamic(动态).在更老一些的版本中,dynamic被称作apache-like.这个要注意看配置文件的说明. 下面4个参数的意思分别为: pm.max_children:静态方式下开启的php-fpm进程数量pm.star

mariadb配置文件优化参数

mariadb数据库优化需要根据自己业务需求以及根据硬件配置来进行参数优化,下面是一些关于mariadb数据库参数优化的配置文件. 如下为128G内存32线程处理器的mariadb配置参数优化: [client] #password= your_password port= 3306          socket= /tmp/mysql.sock !includedir /opt/local/mysql/wsrep # The MySQL server [mysqld] port= 3306

fastcgi 性能优化参数

优化性能参数设置,在ngnix.conf中的http 层加上fastcgi参数如下:http {fastcgi_cache_path /usr/local/nginx/fastcgi_cache levels=1:2 keys_zone=TEST:10minactive=5m;fastcgi_connect_timeout=300;fastcgi_send_timeout=300;fastcgi_buffer_size=64k;fastcgi_buffers 4 64k;fastcgi_busy

GCC笔记(警告.优化以及调试选项)

GCC提供了大量的警告选项,对代码中可能存在的问题提出警告,通常可以使用-Wall来开启以下警告: -Waddress -Warray-bounds (only with -O2) -Wc++0x-compat -Wchar-subscripts -Wimplicit-int -Wimplicit-function-declaration -Wcomment -Wformat -Wmain (only for C/ObjC and unless -ffreestanding) -Wmissing

Mariadb配置文件优化参数(仅供参考)

[client]#password= your_passwordport= 3306         socket= /tmp/mysql.sock!includedir /opt/local/mysql/wsrep# The MySQL server[mysqld]port= 3306socket= /tmp/mysql.sockbasedir = /opt/local/mysqldatadir=/opt/local/mysql/data                   #数据库存放目录r

大并发内核优化参数

如何分析系统的性能问题: 从以下几个维度进行考虑 当并发压力加大时,往往是吞吐量上不去,肯定是某一个方面出现了瓶颈,(比如CPU,内存(大量的swap),磁盘IO,网络IO,操作系统,应用) 如果CPU比较高(user,sys,wa),user高,可以把系统的线程dump出来,观察系统正在做的逻辑,优化逻辑:sys高,则说明kernel耗费了大量的CPU时间,比如当前的线程的切换比较厉害等等:wa高一般是IO操作比较频繁,CPU频繁调度 查看CPU,还需要观察系统load,一般不要超过系统的物理