SHOW STATUS; SHOW STATUS LIKE <pattern>;
如,
mysql> SHOW STATUS LIKE 'Com_%'; +---------------------------+-------+ | Variable_name | Value | +---------------------------+-------+ | Com_admin_commands | 0 | | Com_assign_to_keycache | 0 | | Com_alter_db | 0 | | Com_alter_db_upgrade | 0 | | Com_alter_event | 0 | | Com_alter_function | 0 | | Com_alter_procedure | 0 | | Com_alter_server | 0 | | Com_alter_table | 0 | | Com_alter_tablespace | 0 | | Com_alter_user | 0 | | Com_analyze | 0 | | Com_begin | 0 | | Com_binlog | 0 | | Com_call_procedure | 0 | | Com_change_db | 0 | | Com_change_master | 0 | ...具体各参数详解可见:
http://dev.mysql.com/doc/refman/5.7/en/server-status-variables.html
下面几个参数便于用户了解数据库的基本情况:
mysql> EXPLAIN SELECT * FROM sales \G *************************** 1. row *************************** id: 1 select_type: SIMPLE --查询类型, 有SIMPLE,PRIMARY,UNION,SUBQUERY等值 table: sales --输出结果集的表 type: ALL --表的连接类型:性能由高到低为:system, const, eq_ref, ref, ref_or_null, index_merge, unique_subquery,index_subquery, range, index, all possible_keys: NULL --查询时可能使用的索引 key: NULL --实际使用的索引 key_len: NULL --索引字段的长度 ref: NULL --相对于key,若值为func,则表示从函数中获取的结果集 rows: 12 --扫描行的数量 Extra: NULL --执行情况的说明和描述 1 row in set (0.00 sec)
具体详情可参考:
http://dev.mysql.com/doc/refman/5.7/en/explain-output.html#explain_ref
--创建多列索引 mysql> CREATE INDEX idx_sales_year on sales(year, profit); Query OK, 0 rows affected (0.25 sec) Records: 0 Duplicates: 0 Warnings: 0 --仅第一列有可能使用索引 mysql> EXPLAIN SELECT * FROM sales WHERE year = 2005\G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: sales type: ref possible_keys: idx_sales_year key: idx_sales_year key_len: 4 ref: const rows: 6 Extra: NULL 1 row in set (0.00 sec) mysql> EXPLAIN SELECT * FROM sales WHERE profit = 2012\G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: sales type: ALL possible_keys: NULL key: NULL key_len: NULL ref: NULL rows: 11 Extra: Using where 1 row in set (0.05 sec)
mysql> EXPLAIN SELECT * FROM sales WHERE year LIKE '%2'\G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: sales type: ALL possible_keys: NULL key: NULL key_len: NULL ref: NULL rows: 11 Extra: Using where 1 row in set (0.00 sec) mysql> EXPLAIN SELECT * FROM sales WHERE year LIKE '2%'\G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: sales type: ALL possible_keys: idx_sales_year key: NULL key_len: NULL ref: NULL rows: 11 Extra: Using where 1 row in set (0.00 sec)
mysql> EXPLAIN SELECT * FROM sales WHERE year is null\G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: NULL type: NULL possible_keys: NULL key: NULL key_len: NULL ref: NULL rows: NULL Extra: Impossible WHERE 1 row in set (0.00 sec)
有些情况下,虽然存在索引,MySQL却不会使用索引:
SELECT * FROM table_name WHERE key_part1 > 1 AND key_part2 < 100;
mysql> EXPLAIN SELECT * FROM sales WHERE country = 'ch%'\G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: sales type: ref possible_keys: idx_sales_country key: idx_sales_country key_len: 22 ref: const rows: 1 Extra: Using index condition 1 row in set (0.00 sec) mysql> EXPLAIN SELECT * FROM sales WHERE country = 'ch%' OR product = 'tn1'\G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: sales type: ALL possible_keys: idx_sales_country key: NULL key_len: NULL ref: NULL rows: 11 Extra: Using where 1 row in set (0.00 sec)
mysql> EXPLAIN SELECT * FROM sales WHERE country = 121\G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: sales type: ALL possible_keys: idx_sales_country key: NULL key_len: NULL ref: NULL rows: 11 Extra: Using where 1 row in set (0.00 sec) mysql> EXPLAIN SELECT * FROM sales WHERE country = '121'\G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: sales type: ref possible_keys: idx_sales_country key: idx_sales_country key_len: 22 ref: const rows: 1 Extra: Using index condition 1 row in set (0.00 sec)
mysql> SHOW STATUS LIKE 'Handler_read%'; +-----------------------+-------+ | Variable_name | Value | +-----------------------+-------+ | Handler_read_first | 2 | | Handler_read_key | 3 | 如果索引正在工作,值会增加 | Handler_read_last | 0 | | Handler_read_next | 0 | | Handler_read_prev | 0 | | Handler_read_rnd | 0 | | Handler_read_rnd_next | 76 | 值越大,查询效率越慢 +-----------------------+-------+
ANALYZE [LOCA | NO_WRITE_TO_BINLOG] TABLE table1, table2, ...
如:
mysql> ANALYZE TABLE sales; +------------+---------+----------+----------+ | Table | Op | Msg_type | Msg_text | +------------+---------+----------+----------+ | test.sales | analyze | status | OK | +------------+---------+----------+----------+ 1 row in set (0.18 sec)
CHECK TABLE tb1_name [, tb1_name] ... [option] ... option = {QUICK|FASE|EXTENDED|CHANGED}如:
mysql> CHECK TABLE sales; +------------+-------+----------+----------+ | Table | Op | Msg_type | Msg_text | +------------+-------+----------+----------+ | test.sales | check | status | OK | +------------+-------+----------+----------+ 1 row in set (0.00 sec)
OPTIMIZE [LOCAL | NO WRITE_TO_BINLOG] TABLE tb1_name, tb2_name,...
ALTER TABLE tb1_name DISABLE KEYS; --loading data ALTER TABLE tb1_name ENABLE KEYS;
1. 将导入的数据按主键的顺序排列,再导入;
2. 导入数据前关闭唯一性校验::
SET UNIQUE_CHECKS=0; -- load data SET UNIQUE_CHECKS=1;3. 导入数据前关闭自动提交:
SET AUTOCOMMIT=0; -- load data SET AUTOCOMMIT=1;
mysql> EXPLAIN SELECT * FROM sales GROUP BY product\G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: sales type: ALL possible_keys: NULL key: NULL key_len: NULL ref: NULL rows: 12 Extra: Using temporary; Using filesort 1 row in set (0.00 sec) mysql> EXPLAIN SELECT * FROM sales GROUP BY product ORDER BY NULL\G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: sales type: ALL possible_keys: NULL key: NULL key_len: NULL ref: NULL rows: 12 Extra: Using temporary 1 row in set (0.04 sec)
SELECT SQL_BUFFER_RESULTS * FROM ...
mysql> EXPLAIN SELECT * FROM users WHERE id = 3\G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: users type: const possible_keys: PRIMARY,idx_users_id key: PRIMARY key_len: 4 ref: const rows: 1 Extra: NULL 1 row in set (0.00 sec) -- USE INDEX告诉MySQL使用index mysql> EXPLAIN SELECT * FROM users USE INDEX (idx_users_id) WHERE id = 3\G *************************** 1. row *************************** id: 1 select_type: SIMPLE table: users type: ref possible_keys: idx_users_id key: idx_users_id key_len: 4 ref: const rows: 1 Extra: NULL 1 row in set (0.00 sec) 其他: -- IGNORE INDEX告诉MySQL忽略索引 -- FORCE INDEX告诉MySQL强制使用索引