show processlist
分析当前有哪个连接,连接处于什么状态
mysql -uroot -padmin888 -e 'show processlist'
只取出State的值:
mysql -uroot -padmin888 -e 'show processlist\G'|grep State
去重并排序:
mysql -uroot -padmin888 -e 'show processlist\G'|grep State|uniq|sort -rn
我们如何写个脚本,每秒钟查询10次?
vi tjproce.sh
#!bin/bash
while true
do
mysql -uroot -padmin888 -e 'show processlist\G'|grep State|uniq|sort -rn >> proce.txt
uslesp 100000
done
(usleep 100000 十分之一秒,这就每秒钟执行10次)
我们要特别注意的几种mysql进程状态:
converting HEAP to MyISAM #查询结果太大时,把结果放在磁盘
create tmp table #创建临时表(如group时存储中间结果)
Copying to tmp table #往临时表复制,很耗时间和资源
Sending data #发送数据
Sorting result #排序也是非常消耗资源的
locked #被其他查询锁住
loggin slow query #记录慢查询
mysql> show variables like '%size%';
tmp_table_size #临时表的大小
myisam_sort_buffer_size #排序的大小
修改:
mysql> set global tmp_table_size=20000000
profiling
查看是否打开?
mysql> show variables like '%profiling%';
打开profiling:
mysql> set profiling=ON;
当你打开之后,你所执行的每一条sql语句,它讲帮你生成每条SQL语句的分析记录。
怎么来看呢?
mysql> show profiles;
+----------+------------+-----------------------------------+
| Query_ID | Duration | Query |
+----------+------------+-----------------------------------+
|
| 7 | 0.00040450 | select * from user |
+----------+------------+-----------------------------------+
7 rows in set, 1 warning (0.00 sec)
mysql> show profile for query 7;
+----------------------+----------+
| Status | Duration |
+----------------------+----------+
| starting | 0.000084 |
| checking permissions | 0.000037 |
| Opening tables | 0.000020 |
| init | 0.000054 |
| System lock | 0.000023 |
| optimizing | 0.000004 |
| statistics | 0.000011 |
| preparing | 0.000009 |
| executing | 0.000002 |
| Sending data | 0.000076 |
| end | 0.000004 |
| query end | 0.000009 |
| closing tables | 0.000008 |
| freeing items | 0.000041 |
| cleaning up | 0.000025 |
+----------------------+----------+
15 rows in set, 1 warning (0.00 sec)