MySQL优化器特性(七)成本估算常数
成本估算常数表示执行一些MySQL基础操作时的成本,如读取一个页面,创建一个临时表,比较一条记录,解析一行记录等操作。
mysql.engine_cost和mysql.server_cost表分别记录存储引擎和Server层的常量
engine_cost
mysql> select * from mysql.engine_cost; +-------------+-------------+------------------------+------------+---------------------+---------+---------------+ | engine_name | device_type | cost_name | cost_value | last_update | comment | default_value | +-------------+-------------+------------------------+------------+---------------------+---------+---------------+ | default | 0 | io_block_read_cost | NULL | 2020-03-12 18:52:18 | NULL | 1 | | default | 0 | memory_block_read_cost | NULL | 2020-03-12 18:52:18 | NULL | 0.25 |
server_cost
mysql> select * from mysql.server_cost; +------------------------------+------------+---------------------+---------+---------------+ | cost_name | cost_value | last_update | comment | default_value | +------------------------------+------------+---------------------+---------+---------------+ | disk_temptable_create_cost | NULL | 2020-03-12 18:52:18 | NULL | 20 | | disk_temptable_row_cost | NULL | 2020-03-12 18:52:18 | NULL | 0.5 | | key_compare_cost | NULL | 2020-03-12 18:52:18 | NULL | 0.05 | | memory_temptable_create_cost | NULL | 2020-03-12 18:52:18 | NULL | 1 | | memory_temptable_row_cost | NULL | 2020-03-12 18:52:18 | NULL | 0.1 | | row_evaluate_cost | NULL | 2020-03-12 18:52:18 | NULL | 0.1 | +------------------------------+------------+---------------------+---------+---------------+
这里的数据可以直接修改,修改后通过flush optimizer_costs加载生效。
mysql> update mysql.server_cost set cost_value=1 where cost_name = 'row_evaluate_cost'; Query OK, 1 row affected (0.00 sec) Rows matched: 1 Changed: 1 Warnings: 0 mysql> FLUSH OPTIMIZER_COSTS; Query OK, 0 rows affected (0.01 sec)
退出当前session,重新登录后查看SQL的成本:
mysql> explain format=tree select * from test_ror; +----------------------------------------------------+ | EXPLAIN | +----------------------------------------------------+ | -> Table scan on test_ror (cost=105.25 rows=105) | +----------------------------------------------------+ 1 row in set (0.00 sec)
一般情况下不建议修改这里的数据。