系统开发的初期阶段注重功能实现,数据比较少,随着数据量的增加,性能问题会慢慢突显
查询系统状态信息
show [sesison |global ] status;
mysqladmin extended-status
show status like 'Com_%';
mysql> show status like 'Com_%';
+-----------------------------+-------+
| Variable_name | Value |
+-----------------------------+-------+
| Com_admin_commands | 0 |
| Com_delete | 0 | 执行删除的次数,执行一次累加一次
| Com_insert | 0 | 执行insert的次数,执行一次累加一次,批量执行 也只累加一次
| Com_savepoint | 0 |
| Com_select | 1 | 执行select的次数,执行一次增加一次
| Com_unlock_tables | 0 |
| Com_update | 0 | 执行update的次数,执行一次累加一次
。。。。。。。。。。
+-----------------------------+-------+
149 rows in set (2.43 sec)
innnodb的表的操作记录
mysql> show status like 'Innodb_row_%';
+-------------------------------+-------+
| Variable_name | Value |
+-------------------------------+-------+
| Innodb_row_lock_current_waits | 0 |
| Innodb_row_lock_time | 0 |
| Innodb_row_lock_time_avg | 0 |
| Innodb_row_lock_time_max | 0 |
| Innodb_row_lock_waits | 0 |
| Innodb_rows_deleted | 0 | 执行删除操作的行数
| Innodb_rows_inserted | 0 |执行insert的行数
| Innodb_rows_read | 8 | select查询返回的行数
| Innodb_rows_updated | 0 |执行updata操作的行数
+-------------------------------+-------+
9 rows in set (0.00 sec)
连接mysql服务器的次数
mysql> show status like 'Connections%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Connections | 5 |
+---------------+-------+
1 row in set (0.00 sec)
linux命令 uptime 服务器的工作时间
[root@localhost ~]# uptime
22:54:13 up 3:54, 3 users, load average: 0.08, 0.02, 0.01
mysql数据库慢查询的次数
mysql> show status like 'Slow_queries%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| Slow_queries | 1 |
+---------------+-------+
1 row in set (0.01 sec)
打开慢查询监控,定位慢查询的sql
41 #开启慢查询
42 slow_query_log = 1
43 long_query_time=2
44 slow_query_log_file = /var/lib/mysql/slow.log
通过show processlist;定位sql正在执行的情况
通过执行计划expalin,对慢sql进行执行计划分析
mysql> explain select * from complain where complain_code='TSD20190515006';
+----+-------------+----------+------------+------+---------------------+---------------------+---------+-------+-------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+----------+------------+------+---------------------+---------------------+---------+-------+-------+----------+-------+
| 1 | SIMPLE | complain | NULL | ref | complain_code_index | complain_code_index | 195 | const | 54570 | 100.00 | NULL |
+----+-------------+----------+------------+------+---------------------+---------------------+---------+-------+-------+----------+-------+
1 row in set, 1 warning (0.00 sec)
select_type类型说明
SIMPLE:简单表查询
PRIMARY:主查询,即外出查询
UNION:union中的第二个或者后面的查询语句
SUBQUERY:子查询中的第一个select
table:输出结果的表
type:mysql在表中查询所需行的方式, 访问类型
ALL:全表扫描,遍历全表
index:索引全扫描, 遍历整个索引
ref:使用非唯一扫描或者唯一索引的前缀扫描
eq_ref:类似ref,区别在于索引是使用唯一索引,对于每个索引键值,表中只有一条记录匹配
多表中使用primary key或者 unique index 作为关联条件
const/system 单表中最多有一个匹配行,查询速度快
NULL:不访问表或者索引,直接能够得到结果
通过show profile分析sql
验证profile是否打开
mysql> select @@have_profiling;
+------------------+
| @@have_profiling |
+------------------+
| YES |
+------------------+
1 row in set, 1 warning (2.23 sec)
profiling默认关闭,并通过set命令打开
mysql> select @@profiling;
+-------------+
| @@profiling |
+-------------+
| 0 |
+-------------+
1 row in set, 1 warning (0.00 sec)
mysql> set profiling=1;
Query OK, 0 rows affected, 1 warning (0.00 sec)
mysql> select count(1) from complain;
+----------+
| count(1) |
+----------+
| 1638400 |
+----------+
1 row in set (1.32 sec)
查询sql语句执行情况
mysql> show profiles;
+----------+------------+---------------------------------+
| Query_ID | Duration | Query |
+----------+------------+---------------------------------+
| 1 | 2.22956925 | show tables |
| 2 | 0.00213900 | show databases |
| 3 | 0.00025525 | SELECT DATABASE() |
| 4 | 0.00040425 | show tables |
| 5 | 0.00005675 | selcet count(1) from complain |
| 6 | 1.31990750 | select count(1) from complain |
+----------+------------+---------------------------------+
6 rows in set, 1 warning (0.00 sec)
查看具体sql的执行情况
mysql> show profile for query 6;
+----------------------+----------+
| Status | Duration |
+----------------------+----------+
| starting | 0.000265 |
| checking permissions | 0.000008 |
| Opening tables | 0.000538 |
| init | 0.000089 |
| System lock | 0.000043 |
| optimizing | 0.000021 |
| statistics | 0.000018 |
| preparing | 0.000043 |
| executing | 0.000004 |
| Sending data | 1.318605 | mysql线程开始访问数据,并返回给客户端的时间
| end | 0.000014 |
| query end | 0.000014 |
| closing tables | 0.000030 |
| freeing items | 0.000201 |
| cleaning up | 0.000017 |
+----------------------+----------+
15 rows in set, 1 warning (0.00 sec)
sending data 表示mysql线程开始访问数据行并把结果返回给客户端的时间,而不仅仅是把结果返回给客户端的时间;
sending data的状态下,mysql线程往往需要做大量的磁盘读取操作经常都是最耗时的操作
profiling 相关的信息存在了information_schema.profiling表中
select * from information_schema.profiling
where query_id=1
mysql> show profile cpu for query 1;
+----------------------+----------+----------+------------+
| Status | Duration | CPU_user | CPU_system |
+----------------------+----------+----------+------------+
| starting | 0.000072 | 0.000000 | 0.000000 |
| checking permissions | 0.000006 | 0.000000 | 0.000000 |
| Opening tables | 0.000013 | 0.000000 | 0.000000 |
| init | 0.000026 | 0.000000 | 0.000000 |
| System lock | 0.000007 | 0.000000 | 0.000000 |
| optimizing | 0.000003 | 0.000000 | 0.000000 |
| statistics | 0.000011 | 0.000000 | 0.000000 |
| preparing | 0.000008 | 0.000000 | 0.000000 |
| executing | 0.000002 | 0.000000 | 0.000000 |
| Sending data | 0.204742 | 0.203969 | 0.000000 |
| end | 0.000012 | 0.000000 | 0.000000 |
| query end | 0.000009 | 0.000000 | 0.000000 |
| closing tables | 0.000007 | 0.000000 | 0.000000 |
| freeing items | 0.000086 | 0.001000 | 0.000000 |
| cleaning up | 0.000016 | 0.000000 | 0.000000 |
+----------------------+----------+----------+------------+
15 rows in set, 1 warning (0.01 sec)
mysql> show profile all for query 1;
+----------------------+----------+----------+------------+-------------------+---------------------+--------------+---------------+---------------+-------------------+-------------------+-------------------+-------+-----------------------+----------------------+-------------+
| Status | Duration | CPU_user | CPU_system | Context_voluntary | Context_involuntary | Block_ops_in | Block_ops_out | Messages_sent | Messages_received | Page_faults_major | Page_faults_minor | Swaps | Source_function | Source_file | Source_line |
+----------------------+----------+----------+------------+-------------------+---------------------+--------------+---------------+---------------+-------------------+-------------------+-------------------+-------+-----------------------+----------------------+-------------+
| starting | 0.000072 | 0.000000 | 0.000000 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | NULL | NULL | NULL |
| checking permissions | 0.000006 | 0.000000 | 0.000000 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | check_access | sql_authorization.cc | 802 |
| Opening tables | 0.000013 | 0.000000 | 0.000000 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | open_tables | sql_base.cc | 5714 |
| init | 0.000026 | 0.000000 | 0.000000 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | handle_query | sql_select.cc | 121 |
| System lock | 0.000007 | 0.000000 | 0.000000 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | mysql_lock_tables | lock.cc | 323 |
| optimizing | 0.000003 | 0.000000 | 0.000000 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | optimize | sql_optimizer.cc | 151 |
| statistics | 0.000011 | 0.000000 | 0.000000 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | optimize | sql_optimizer.cc | 367 |
| preparing | 0.000008 | 0.000000 | 0.000000 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | optimize | sql_optimizer.cc | 475 |
| executing | 0.000002 | 0.000000 | 0.000000 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | exec | sql_executor.cc | 119 |
| Sending data | 0.204742 | 0.203969 | 0.000000 | 0 | 2 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | exec | sql_executor.cc | 195 |
| end | 0.000012 | 0.000000 | 0.000000 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | handle_query | sql_select.cc | 199 |
| query end | 0.000009 | 0.000000 | 0.000000 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | mysql_execute_command | sql_parse.cc | 4946 |
| closing tables | 0.000007 | 0.000000 | 0.000000 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | mysql_execute_command | sql_parse.cc | 4998 |
| freeing items | 0.000086 | 0.001000 | 0.000000 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | mysql_parse | sql_parse.cc | 5610 |
| cleaning up | 0.000016 | 0.000000 | 0.000000 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | dispatch_command | sql_parse.cc | 1924 |
+----------------------+----------+----------+------------+-------------------+---------------------+--------------+---------------+---------------+-------------------+-------------------+-------------------+-------+-----------------------+----------------------+-------------+
15 rows in set, 1 warning (0.00 sec)
mysql> show profile page faults for query 1;
+----------------------+----------+-------------------+-------------------+
| Status | Duration | Page_faults_major | Page_faults_minor |
+----------------------+----------+-------------------+-------------------+
| starting | 0.000072 | 0 | 0 |
| checking permissions | 0.000006 | 0 | 0 |
| Opening tables | 0.000013 | 0 | 0 |
| init | 0.000026 | 0 | 0 |
| System lock | 0.000007 | 0 | 0 |
| optimizing | 0.000003 | 0 | 0 |
| statistics | 0.000011 | 0 | 0 |
| preparing | 0.000008 | 0 | 0 |
| executing | 0.000002 | 0 | 0 |
| Sending data | 0.204742 | 0 | 0 |
| end | 0.000012 | 0 | 0 |
| query end | 0.000009 | 0 | 0 |
| closing tables | 0.000007 | 0 | 0 |
| freeing items | 0.000086 | 0 | 0 |
| cleaning up | 0.000016 | 0 | 0 |
+----------------------+----------+-------------------+-------------------+
15 rows in set, 1 warning (0.00 sec)
mysql> show profile block io for query 1;
+----------------------+----------+--------------+---------------+
| Status | Duration | Block_ops_in | Block_ops_out |
+----------------------+----------+--------------+---------------+
| starting | 0.000072 | 0 | 0 |
| checking permissions | 0.000006 | 0 | 0 |
| Opening tables | 0.000013 | 0 | 0 |
| init | 0.000026 | 0 | 0 |
| System lock | 0.000007 | 0 | 0 |
| optimizing | 0.000003 | 0 | 0 |
| statistics | 0.000011 | 0 | 0 |
| preparing | 0.000008 | 0 | 0 |
| executing | 0.000002 | 0 | 0 |
| Sending data | 0.204742 | 0 | 0 |
| end | 0.000012 | 0 | 0 |
| query end | 0.000009 | 0 | 0 |
| closing tables | 0.000007 | 0 | 0 |
| freeing items | 0.000086 | 0 | 0 |
| cleaning up | 0.000016 | 0 | 0 |
+----------------------+----------+--------------+---------------+
15 rows in set, 1 warning (0.00 sec)