MySQL常用统计语句

查询指定SQL的trace信息

  1. mysql -uUSER -pPWD -hHOST -e "\
  2. use ins_tc_prd; \
  3. set session optimizer_trace='enabled=on'; \
  4. set optimizer_trace_max_mem_size = 1638400; \
  5. explain SQL; \
  6. select * from information_schema.optimizer_trace\G;" > trace_log.log

查询某张表大小

    table_schema = 'testdb_monitor'
ORDER BY (data_length + index_length) DESC;

查看表空间的使用情况

select table_name, 
      (data_length+ index_length) /1024/1024 as total_mb, 
      table_rows 
from information_schema.tables 
where table_schema= 'Database' ;//不写就是全部

MySQL查询阻塞语句

select r.trx_id waiting_trx_id, r.trx_mysql_thread_Id waiting_thread, 
       r.trx_query waiting_query, b.trx_id blocking_trx_id, 
       b.trx_mysql_thread_id blocking_thread, b.trx_query blocking_query 
  from information_schema.innodb_lock_waits w inner join information_schema.innodb_trx b on b.trx_id = w.blocking_trx_id 
  inner join information_schema.innodb_trx r on r.trx_id = w.requesting_trx_id;

查询表的数据量大小

select table_name,(data_length + index_length)/1024/1024 as table_mb,table_rows 
from information_schema.tables 
where table_schema='zabbix';

统计数据库中访问量前10的IP

SELECT 
    SUBSTRING_INDEX(host, ':', 1) AS ip, COUNT(*)
FROM
    information_schema.processlist
GROUP BY ip
ORDER BY COUNT(*) DESC
LIMIT 10;

MySQL性能分析脚本

MySQL数据库自带的mysqladmin工具是非常强大的,我在周振兴老师(@淘宝 花名:苏普)的博客上看到了关于mysqladmin工具的分享,对于现阶段的我还是拿来主义,谢谢周振兴老师。

Mysqladmin是Mysql一个重要的客户端,最常见的是使用它来关闭数据库,除此,该命令还可以了解Mysql运行状态、进程信息、进程杀死等。本文介绍一下如何使用Mysqladmin Extended-Status(因为没有”歧义”,所以可以使用Ex T代替)了解Mysql的运行状态。
使用mysqladmin extended-status命令可以获得所有MySQL性能指标,即show global status的输出,不过,因为多数这些指标都是累计值,如果想了解当前的状态, 则需要进行一次差值计算,这就是mysqladmin extended-status的 一个额外功能,非常实用。默认的,使用extended-status,看到也是累计值,但是,加上参数-r(—relative),就可以看到各个指标的差值,配合参数-i(—sleep)就可以指定刷新的频率。
命令如下

mysqladmin -uroot -r -i 1 -pxxx extended-status

配合grep使用,我们就有:

mysqladmin -uroot -r -i 1 -pxxx extended-status \
|grep "Questions\|Queries\|Innodb_rows\|Com_select \|Com_insert \|Com_update \|Com_delete "

加入awk,同时输出时间信息:

mysqladmin -uroot -p -h127.0.0.1 -P3306 -r -i 1 ext |\
awk -F"|" '{\
if($2 ~ /Variable_name/){\
print " <------------- " strftime("%H:%M:%S") " ------------->";\
}\
if($2 ~ /Questions|Queries|Innodb_rows|Com_select |Com_insert |Com_update |Com_delete |Innodb_buffer_pool_read_requests/)\
print $2 $3;\
}'

配合复杂一点的awk
反正也不简单了,那就更复杂一点,这样让输出结果更友好点,因为awk不支持动态变量,所以代码看起来比较复杂

mysqladmin -P3306 -uroot -p -h127.0.0.1 -r -i 1 ext |\
awk -F"|" \
"BEGIN{ count=0; }"\
'{ if($2 ~ /Variable_name/ && ((++count)%20 == 1)){\
print "----------|---------|--- MySQL Command Status --|----- Innodb row operation ----|-- Buffer Pool Read --";\
print "---Time---|---QPS---|select insert update delete| read inserted updated deleted| logicalphysical";\
}\
else if ($2 ~ /Queries/){queries=$3;}\
else if ($2 ~ /Com_select /){com_select=$3;}\
else if ($2 ~ /Com_insert /){com_insert=$3;}\
else if ($2 ~ /Com_update /){com_update=$3;}\
else if ($2 ~ /Com_delete /){com_delete=$3;}\
else if ($2 ~ /Innodb_rows_read/){innodb_rows_read=$3;}\
else if ($2 ~ /Innodb_rows_deleted/){innodb_rows_deleted=$3;}\
else if ($2 ~ /Innodb_rows_inserted/){innodb_rows_inserted=$3;}\
else if ($2 ~ /Innodb_rows_updated/){innodb_rows_updated=$3;}\
else if ($2 ~ /Innodb_buffer_pool_read_requests/){innodb_lor=$3;}\
else if ($2 ~ /Innodb_buffer_pool_reads/){innodb_phr=$3;}\
else if ($2 ~ /Uptime / && count >= 2){\
printf(" %s |%9d",strftime("%H:%M:%S"),queries);\
printf("|%6d %6d %6d %6d",com_select,com_insert,com_update,com_delete);\
printf("|%6d %8d %7d %7d",innodb_rows_read,innodb_rows_inserted,innodb_rows_updated,innodb_rows_deleted);\
printf("|%10d %11d\n",innodb_lor,innodb_phr);\
}}'