阅读更多
1 Mysql
1.1 系统参数
1.1.1 查看所有系统参数
1 2 3
| SHOW VARIABLES SHOW VARIABLES\G SHOW VARIABLES LIKE '%isolation%';
|
1.1.2 查看指定系统参数的值
以系统参数tx_isolation
为例
1 2 3 4 5 6 7
| SELECT @@global.tx_isolation; SELECT @@session.tx_isolation; SELECT @@tx_isolation;
SHOW GLOBAL VARIABLES LIKE 'tx_isolation'; SHOW SESSION VARIABLES LIKE 'tx_isolation'; SHOW VARIABLES LIKE 'tx_isolation';
|
1.1.3 修改系统参数的值
以系统参数profiling_history_size
为例
1 2 3 4 5 6 7
| SET GLOBAL profiling_history_size = 10; SET SESSION profiling_history_size = 15; SET profiling_history_size = 15;
SET @@global.profiling_history_size = 10; SET @@session.profiling_history_size = 15; SET @@profiling_history_size = 15;
|
以系统参数tx_isolation
为例,这个参数比较特殊,需要用特殊的赋值语句
1 2 3
| SET GLOBAL TRANSACTION ISOLATION LEVEL READ COMMITTED; SET SESSION TRANSACTION ISOLATION LEVEL SERIALIZABLE; SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
|
1.2 查看版本号
1.3 执行状态分析
1.4 profile工具
查看profile相关的系统变量
1 2 3 4 5 6 7 8 9
| mysql> SHOW VARIABLES LIKE '%profi%'; +------------------------+-------+ | Variable_name | Value | +------------------------+-------+ | have_profiling | YES | | profiling | OFF | | profiling_history_size | 15 | +------------------------+-------+ 3 rows in set (0.01 sec)
|
开启profiling功能
1 2 3 4 5 6 7 8 9 10 11 12
| mysql> SET SESSION profiling = 1; Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> SHOW VARIABLES LIKE '%profi%'; +------------------------+-------+ | Variable_name | Value | +------------------------+-------+ | have_profiling | YES | | profiling | ON | | profiling_history_size | 15 | +------------------------+-------+ 3 rows in set (0.00 sec)
|
查询所有语句的id以及执行时间
1 2 3 4 5 6 7 8 9 10 11
| mysql> SHOW PROFILES; +----------+------------+-------------------------------------+ | Query_ID | Duration | Query | +----------+------------+-------------------------------------+ | 10 | 0.00006000 | SET SESSION @@session.profiling = 1 | | 11 | 0.00005800 | SET SESSION @@profiling = 1 | | 12 | 0.00010100 | SET SESSION profiling = 1 | | 13 | 0.00190400 | SHOW VARIABLES LIKE '%profi%' | | 14 | 0.00005800 | SHOW PROFIILES | +----------+------------+-------------------------------------+ 5 rows in set, 1 warning (0.00 sec)
|
查询某个特定的语句
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| mysql> SHOW PROFILE FOR QUERY 13; +----------------------+----------+ | Status | Duration | +----------------------+----------+ | starting | 0.000063 | | checking permissions | 0.000016 | | Opening tables | 0.000016 | | init | 0.000052 | | System lock | 0.000008 | | optimizing | 0.000004 | | optimizing | 0.000002 | | statistics | 0.000013 | | preparing | 0.000014 | | statistics | 0.000007 | | preparing | 0.000006 | | executing | 0.000009 | | Sending data | 0.000007 | | executing | 0.000002 | | Sending data | 0.001597 | | end | 0.000009 | | query end | 0.000006 | | closing tables | 0.000016 | | removing tmp table | 0.000009 | | closing tables | 0.000008 | | freeing items | 0.000028 | | cleaning up | 0.000012 | +----------------------+----------+ 22 rows in set, 1 warning (0.00 sec)
|
查询某个特定的语句的详细信息
- ALL:显示所有性能信息
SHOW PROFILE ALL FOR QUERY 13;
- CPU:CPU占用情况
SHOW PROFILE CPU FOR QUERY 13;
- BLOCK IO:显示块IO的次数
SHOW PROFILE BLOCK IO FOR QUERY 13;
- CONTEXT SWITCHES:显示自动和被动的上下文切换数量
SHOW PROFILE CONTEXT SWITCHES FOR QUERY 13;
- IPC:显示发送和接受的消息数量
SHOW PROFILE IPC FOR QUERY 13;
- MEMORY:显示内存占用情况
SHOW PROFILE MEMORY FOR QUERY 13;
- SWAPS:显示swap的次数
SHOW PROFILE SWAPS FOR QUERY 13;
- 以上参数可以组合使用
SHOW PROFILE BLOCK IO, CPU FOR QUERY 13;
1.5 EXPLAIN-分析执行计划
语法
1 2 3 4 5 6 7 8 9 10
| EXPLAIN + SQL
mysql> EXPLAIN SELECT * FROM test WHERE id = 1; +----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+-------+ | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra | +----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+-------+ | 1 | SIMPLE | test | NULL | const | PRIMARY | PRIMARY | 4 | const | 1 | 100.00 | NULL | +----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+-------+ 1 row in set, 1 warning (0.00 sec)
|
参数解释
- select_type:
- table:显示这一行的数据是关于哪张表的
- partitions:
- type:这是重要的列,显示连接使用了何种类型。从最好到最差的连接类型为const、eq_reg、ref、range、index和ALL
- possible_keys:显示可能应用在这张表中的索引。如果为空,没有可能的索引。可以为相关的域从WHERE语句中选择一个合适的语句
- key: 实际使用的索引。如果为NULL,则没有使用索引。很少的情况下,MYSQL会选择优化不足的索引。这种情况下,可以在SELECT语句中使用USE INDEX(indexname)来强制使用一个索引或者用IGNORE INDEX(indexname)来强制MYSQL忽略索引
- NULL:表示没有使用索引
- PRIMARY:表示使用主键作为索引
- 索引名字:表示使用的索引的名字
- key_len:使用的索引的长度。在不损失精确性的情况下,长度越短越好
- ref:显示索引的哪一列被使用了,如果可能的话,是一个常数
- rows:MYSQL认为必须检查的用来返回请求数据的行数
- filtered:
- Extra:关于MYSQL如何解析查询的额外信息。将在表4.3中讨论,但这里可以看到的坏的例子是Using temporary和Using filesort,意思MYSQL根本不能使用索引,结果是检索会很慢
1.6 参考
2 TiDB
Performance Analysis and Tuning
3 Trino
EXPLAIN ANALYZE
4 Other
profile_example.txt