sys 库是MySQL 5.7其中的一个系统库,里面有很多很好用的跟性能相关的视图、函数和存储过程, 增强MySQL的易用性

例如:哪些语句使用了临时表,哪个用户请求了最多的io,哪个线程占用了最多的内存,哪些索引是没有使用过的索引。

有了sys 库以后,5.7分析问题和定位问题,减少依赖percona-toolkit工具,更多的使用里面的视图。下面我们做个实验,来看sys库那几个性能视图的作用

环境介绍:

192.168.23.10 安装sysbench

192.168.23.12 安装MySQL5.7.13

两台虚拟机,都是4线程,3G内存,宿主机是4核,16G内存,普通SATA盘

sysbench压测脚本,为了做实验,把压测时间设置大一点,方便查看那几个性能视图

/opt/app/sysbench/bin/sysbench --test=/opt/app/sysbench/lua/oltp.lua \

--db-driver=mysql --mysql-host=192.168.23.12 --oltp-table-size=100000 \

--oltp-tables-count=10 --mysql-db=sysbench --mysql-user=sysbench \

--mysql-password=1qaz2wsx --max-time=30000 --max-requests=0 \

--num-threads=8 --report-interval=1 run

第一:查看用户级别的相关情况,

##看一下host发来过的SQL请求情况:total_latency 总延时, lock_latency 总的锁延时,row_sent 发送了多少行, full_ssans 全表扫描总的次数
mysql> select * from x$host_summary_by_statement_latency;
+---------------+--------+-----------------+---------------+----------------+-----------+---------------+---------------+------------+
| host | total | total_latency | max_latency | lock_latency | rows_sent | rows_examined | rows_affected | full_scans |
+---------------+--------+-----------------+---------------+----------------+-----------+---------------+---------------+------------+
| 192.168.23.10 | | | | | | | | |
| localhost | | | | | | | | |
| background | | | | | | | | |
+---------------+--------+-----------------+---------------+----------------+-----------+---------------+---------------+------------+
rows in set (0.01 sec) ##查看按客户端相关IO线程相关的情况 total_latency 总延时,avg_latency 平均延时, max_latency 最大延时
mysql> select * from x$waits_by_host_by_latency order by total_latency desc ;
+---------------+--------------------------------------+---------+-----------------+-------------+---------------+
| host | event | total | total_latency | avg_latency | max_latency |
+---------------+--------------------------------------+---------+-----------------+-------------+---------------+
| 192.168.23.10 | wait/io/file/sql/binlog | | | | |
| 192.168.23.10 | wait/io/table/sql/handler | | | | |
| background | wait/io/file/sql/binlog | | | | |
| background | wait/io/file/innodb/innodb_data_file | | | | |
| background | wait/io/file/innodb/innodb_log_file | | | | |
| 192.168.23.10 | wait/io/file/innodb/innodb_data_file | | | | |
| 192.168.23.10 | wait/io/file/innodb/innodb_log_file | | | | |
| background | wait/io/file/sql/FRM | | | | |
| localhost | wait/io/file/sql/FRM | | | | |
| 192.168.23.10 | wait/lock/table/sql/handler | | | | |
| background | wait/io/file/myisam/kfile | | | | |
| 192.168.23.10 | wait/io/file/sql/FRM | | | | |
| localhost | wait/io/file/myisam/dfile | | | | |
| localhost | wait/io/file/sql/file_parser | | | | |
| background | wait/io/file/mysys/cnf | | | | |
| background | wait/io/file/myisam/dfile | | | | |
| background | wait/io/file/mysys/charset | | | | |
| background | wait/io/file/sql/ERRMSG | | | | |
| localhost | wait/io/file/myisam/kfile | | | | |
| background | wait/io/file/sql/binlog_index | | | | |
| background | wait/io/file/sql/pid | | | | |
| localhost | wait/io/file/sql/dbopt | | | | |
| background | wait/io/file/sql/casetest | | | | |
| 192.168.23.10 | wait/io/file/sql/dbopt | | | | |
| background | wait/io/file/sql/global_ddl_log | | | | |
+---------------+--------------------------------------+---------+-----------------+-------------+---------------+
rows in set (0.01 sec) mysql>
##查看按用户相关的延时情况 total_latency 总延时,avg_latency 平均延时, max_latency 最大延时
mysql> select * from x$waits_by_user_by_latency order by total_latency desc ;
+----------+--------------------------------------+---------+-----------------+-------------+---------------+
| user | event | total | total_latency | avg_latency | max_latency |
+----------+--------------------------------------+---------+-----------------+-------------+---------------+
| sysbench | wait/io/file/sql/binlog | | | | |
| sysbench | wait/io/table/sql/handler | | | | |
| sysbench | wait/io/file/innodb/innodb_log_file | | | | |
| sysbench | wait/io/file/innodb/innodb_data_file | | | | |
| root | wait/io/file/sql/FRM | | | | |
| sysbench | wait/lock/table/sql/handler | | | | |
| sysbench | wait/io/file/sql/FRM | | | | |
| root | wait/io/file/myisam/dfile | | | | |
| root | wait/io/file/sql/file_parser | | | | |
| root | wait/io/file/myisam/kfile | | | | |
| root | wait/io/file/sql/dbopt | | | | |
| sysbench | wait/io/file/sql/dbopt | | | | |
+----------+--------------------------------------+---------+-----------------+-------------+---------------+
rows in set (0.01 sec) mysql>
#查看按用户发来的sql情况,比如:总的SQL条目,语句执行延时,平均延时,是否存在表扫描情况,文件IO,IO延时
mysql> select * from x$user_summary;
+------------+------------+-------------------+-----------------------+-------------+----------+-----------------+---------------------+-------------------+--------------+----------------+------------------------+
| user | statements | statement_latency | statement_avg_latency | table_scans | file_ios | file_io_latency | current_connections | total_connections | unique_hosts | current_memory | total_memory_allocated |
+------------+------------+-------------------+-----------------------+-------------+----------+-----------------+---------------------+-------------------+--------------+----------------+------------------------+
| sysbench | | | 3629674323.0237 | | | | | | | | |
| root | | | 19556783617.3913 | | | | | | | | |
| background | | | 0.0000 | | | | | | | | |
+------------+------------+-------------------+-----------------------+-------------+----------+-----------------+---------------------+-------------------+--------------+----------------+------------------------+
rows in set (0.48 sec) mysql>

第二:IO相关情况

##可以通过这几个视图来看一下数具体数据文件IO相关的情况
#查看相关数据文件:total_read 总的读取字节,avg_read 平均读取字节,total_written总的写的字节,avg_write 平均写的字节,write_pct 以及写比例
mysql> select * from x$io_global_by_file_by_bytes ORDER BY total desc limit ;
+-----------------------------------------+------------+------------+------------+-------------+---------------+-------------+------------+-----------+
| file | count_read | total_read | avg_read | count_write | total_written | avg_write | total | write_pct |
+-----------------------------------------+------------+------------+------------+-------------+---------------+-------------+------------+-----------+
| /data//logs/mysql-bin. | | | 8191.9259 | | | 0.0000 | | 0.00 |
| /data//data/ib_logfile0 | | | 10020.5714 | | | 3354.4644 | | 99.96 |
| /data//logs/mysql-bin. | | | 0.0000 | | | 1808.5376 | | 100.00 |
| /data//data/ibdata1 | | | 23826.0282 | | | 76484.5498 | | 87.74 |
| /data//data/sysbench/sbtest3.ibd | | | 16384.0000 | | | 16384.0000 | | 16.02 |
| /data//data/ibtmp1 | | | 0.0000 | | | 285651.4783 | | 100.00 |
| /data//data/mysql/proc.MYD | | | 2969.3775 | | | 0.0000 | | 0.00 |
| /data//logs/mysql-bin. | | | 7105.2857 | | | 0.0000 | | 0.00 |
| /opt/app/mysql/share/english/errmsg.sys | | | 25152.0000 | | | 0.0000 | | 0.00 |
| /data//data/mysql/engine_cost.ibd | | | 16384.0000 | | | 0.0000 | | 0.00 |
+-----------------------------------------+------------+------------+------------+-------------+---------------+-------------+------------+-----------+
rows in set (0.00 sec) mysql>
#查看相关数据文件延时:如total_latency 总的延时 ,read_latency 总的读延时 write_latency 总的写延时
mysql> select * from x$io_global_by_file_by_latency ORDER BY total_latency desc limit ;
+---------------------------------------------------+--------+-----------------+------------+----------------+-------------+-----------------+------------+-----------------+
| file | total | total_latency | count_read | read_latency | count_write | write_latency | count_misc | misc_latency |
+---------------------------------------------------+--------+-----------------+------------+----------------+-------------+-----------------+------------+-----------------+
| /data//logs/mysql-bin. | | | | | | | | |
| /data//data/ib_logfile0 | | | | | | | | |
| /data//logs/mysql-bin. | | | | | | | | |
| /data//data/ibdata1 | | | | | | | | |
| /data//data/sysbench/sbtest3.ibd | | | | | | | | |
| /data//data/mysql/db.frm | | | | | | | | |
| /data//logs/mysql-bin. | | | | | | | | |
| /data//data/mysql/db.MYI | | | | | | | | |
| /data//data/sys/x@0024memory_global_total.frm | | | | | | | | |
| /data//data/mysql/server_cost.frm | | | | | | | | |
+---------------------------------------------------+--------+-----------------+------------+----------------+-------------+-----------------+------------+-----------------+
rows in set (0.00 sec) mysql> ##可以通过下面这几个视图,看一下binlog redolog innodb数据文件等相关数据文件的IO情况:比如total_latency  总延时,min_latency 最小延时,avg_latency平均延时 max_latency 最大延时
mysql> select * from x$io_global_by_wait_by_bytes ORDER BY total_latency desc;
+-------------------------+--------+-----------------+-------------+-------------+----------------+------------+------------+------------+-------------+---------------+-------------+-----------------+
| event_name | total | total_latency | min_latency | avg_latency | max_latency | count_read | total_read | avg_read | count_write | total_written | avg_written | total_requested |
+-------------------------+--------+-----------------+-------------+-------------+----------------+------------+------------+------------+-------------+---------------+-------------+-----------------+
| sql/binlog | | | | | | | | 8191.8074 | | | 1805.8354 | |
| innodb/innodb_log_file | | | | | | | | 10020.5714 | | | 3351.7202 | |
| innodb/innodb_data_file | | | | | | | | 18310.6509 | | | 77959.0330 | |
| sql/FRM | | | | | | | | 602.9664 | | | 0.0000 | |
| myisam/kfile | | | | | | | | 273.6667 | | | 0.0000 | |
| myisam/dfile | | | | | | | | 2924.7740 | | | 0.0000 | |
| sql/file_parser | | | | | | | | 720.0000 | | | 0.0000 | |
| mysys/cnf | | | | | | | | 18.6667 | | | 0.0000 | |
| mysys/charset | | | | | | | | 18710.0000 | | | 0.0000 | |
| sql/ERRMSG | | | | | | | | 25152.0000 | | | 0.0000 | |
| sql/binlog_index | | | | | | | | 99.0000 | | | 0.0000 | |
| sql/pid | | | | | | | | 0.0000 | | | 5.0000 | |
| sql/dbopt | | | | | | | | 0.0000 | | | 0.0000 | |
| sql/casetest | | | | | | | | 0.0000 | | | 0.0000 | |
| sql/global_ddl_log | | | | | | | | 0.0000 | | | 0.0000 | |
| archive/data | | | | | | | | 0.0000 | | | 0.0000 | |
+-------------------------+--------+-----------------+-------------+-------------+----------------+------------+------------+------------+-------------+---------------+-------------+-----------------+
rows in set (0.01 sec) mysql>
##还可以看一下io_wait,比如总的延时,平均延时,最大延时,读延时,写的延时
mysql> select * from x$io_global_by_wait_by_latency ORDER BY total desc;
+-------------------------+--------+------------------+-------------+----------------+----------------+-----------------+-----------------+------------+------------+------------+-------------+---------------+-------------+
| event_name | total | total_latency | avg_latency | max_latency | read_latency | write_latency | misc_latency | count_read | total_read | avg_read | count_write | total_written | avg_written |
+-------------------------+--------+------------------+-------------+----------------+----------------+-----------------+-----------------+------------+------------+------------+-------------+---------------+-------------+
| sql/binlog | | | | | | | | | | 8191.8074 | | | 1807.7708 |
| innodb/innodb_log_file | | | | | | | | | | 10020.5714 | | | 3354.7646 |
| innodb/innodb_data_file | | | | | | | | | | 18298.4348 | | | 81246.4135 |
| sql/FRM | | | | | | | | | | 602.9664 | | | 0.0000 |
| myisam/dfile | | | | | | | | | | 2924.7740 | | | 0.0000 |
| sql/file_parser | | | | | | | | | | 720.0000 | | | 0.0000 |
| myisam/kfile | | | | | | | | | | 273.6667 | | | 0.0000 |
| sql/dbopt | | | | | | | | | | 0.0000 | | | 0.0000 |
| sql/binlog_index | | | | | | | | | | 99.0000 | | | 0.0000 |
| sql/casetest | | | | | | | | | | 0.0000 | | | 0.0000 |
| sql/ERRMSG | | | | | | | | | | 25152.0000 | | | 0.0000 |
| mysys/cnf | | | | | | | | | | 18.6667 | | | 0.0000 |
| sql/pid | | | | | | | | | | 0.0000 | | | 5.0000 |
| mysys/charset | | | | | | | | | | 18710.0000 | | | 0.0000 |
| sql/global_ddl_log | | | | | | | | | | 0.0000 | | | 0.0000 |
| archive/data | | | | | | | | | | 0.0000 | | | 0.0000 |
+-------------------------+--------+------------------+-------------+----------------+----------------+-----------------+-----------------+------------+------------+------------+-------------+---------------+-------------+
rows in set (0.38 sec) 从这边来看,binlog IO压力比较大。

第三:innodb buffer 和锁相关情况

##看一下sysbench库对innod_buffer使用的情况,比如有多少个页,有多少页是脏页,有多少行给cache
mysql> select * from innodb_buffer_stats_by_schema;
+---------------+------------+------------+-------+--------------+-----------+-------------+
| object_schema | allocated | data | pages | pages_hashed | pages_old | rows_cached |
+---------------+------------+------------+-------+--------------+-----------+-------------+
| sysbench | 163.80 MiB | 143.80 MiB | | | | |
| InnoDB System | 9.67 MiB | 8.80 MiB | | | | |
| mysql | 240.00 KiB | 7.75 KiB | | | | |
+---------------+------------+------------+-------+--------------+-----------+-------------+
rows in set (2.20 sec)
##我们可以看表相关的数据
mysql> select * from innodb_buffer_stats_by_table;
+---------------+---------------------------+-----------+-----------+-------+--------------+-----------+-------------+
| object_schema | object_name | allocated | data | pages | pages_hashed | pages_old | rows_cached |
+---------------+---------------------------+-----------+-----------+-------+--------------+-----------+-------------+
| sysbench | sbtest10 | 23.50 MiB | 20.91 MiB | | | | |
| sysbench | sbtest1 | 23.47 MiB | 20.90 MiB | | | | |
| InnoDB System | SYS_TABLES | 22.14 MiB | 20.37 MiB | | | | |
| sysbench | sbtest9 | 18.27 MiB | 16.10 MiB | | | | |
| sysbench | sbtest8 | 14.31 MiB | 12.43 MiB | | | | |
| sysbench | sbtest6 | 14.22 MiB | 12.40 MiB | | | | |
| sysbench | sbtest3 | 14.17 MiB | 12.36 MiB | | | | |
| sysbench | sbtest7 | 14.16 MiB | 12.34 MiB | | | | |
| sysbench | sbtest5 | 14.09 MiB | 12.29 MiB | | | | |
| sysbench | sbtest4 | 14.06 MiB | 12.28 MiB | | | | |
| sysbench | sbtest2 | 14.00 MiB | 12.20 MiB | | | | |
| InnoDB System | SYS_FOREIGN | 32.00 KiB | bytes | | | | |
| InnoDB System | SYS_COLUMNS | 16.00 KiB | 10.91 KiB | | | | |
| InnoDB System | SYS_DATAFILES | 16.00 KiB | 1.52 KiB | | | | |
| InnoDB System | SYS_FIELDS | 16.00 KiB | 2.68 KiB | | | | |
| InnoDB System | SYS_INDEXES | 16.00 KiB | 3.49 KiB | | | | |
| InnoDB System | SYS_TABLESPACES | 16.00 KiB | 1.49 KiB | | | | |
| mysql | engine_cost | 16.00 KiB | bytes | | | | |
| mysql | gtid_executed | 16.00 KiB | bytes | | | | |
| mysql | innodb_index_stats | 16.00 KiB | 6.63 KiB | | | | |
| mysql | innodb_table_stats | 16.00 KiB | bytes | | | | |
| mysql | plugin | 16.00 KiB | bytes | | | | |
| mysql | servers | 16.00 KiB | bytes | | | | |
| mysql | server_cost | 16.00 KiB | bytes | | | | |
| mysql | slave_master_info | 16.00 KiB | bytes | | | | |
| mysql | slave_relay_log_info | 16.00 KiB | bytes | | | | |
| mysql | slave_worker_info | 16.00 KiB | bytes | | | | |
| mysql | time_zone | 16.00 KiB | bytes | | | | |
| mysql | time_zone_leap_second | 16.00 KiB | bytes | | | | |
| mysql | time_zone_name | 16.00 KiB | bytes | | | | |
| mysql | time_zone_transition | 16.00 KiB | bytes | | | | |
| mysql | time_zone_transition_type | 16.00 KiB | bytes | | | | |
+---------------+---------------------------+-----------+-----------+-------+--------------+-----------+-------------+
rows in set (1.06 sec) mysql> ##查询锁相关的信息
mysql> select * from innodb_lock_waits;
Empty set (0.00 sec) mysql>
mysql> select * from innodb_lock_waits;
Empty set (0.00 sec)
居然结果集为空,那么用以前查锁的SQL,也是没有锁等待相关的信息
mysql> SELECT
-> r.trx_id waiting_trx_id,
-> r.trx_mysql_thread_id waiting_thread,
-> LEFT (r.trx_query, ) waiting_query,
-> concat(
-> concat(lw.lock_type, ' '),
-> lw.lock_mode
-> ) waiting_for_lock,
-> b.trx_id blocking_trx_id,
-> b.trx_mysql_thread_id blocking_thread,
-> LEFT (b.trx_query, ) blocking_query,
-> concat(
-> concat(lb.lock_type, ' '),
-> lb.lock_mode
-> ) blocking_lock
-> 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
-> INNER JOIN information_schema.innodb_locks lw ON lw.lock_trx_id = r.trx_id
-> INNER JOIN information_schema.innodb_locks lb ON lb.lock_trx_id = b.trx_id;
Empty set (0.03 sec) mysql> SELECT * from information_schema.innodb_lock_waits w
Empty set (0.01 sec) 其实这个视图就是语句上面的那个表创建的
下面 是这个视图的
SELECT
 `r`.`trx_wait_started` AS `wait_started`,
 timediff(
  now(),
  `r`.`trx_wait_started`
 ) AS `wait_age`,
 timestampdiff(
  SECOND,
  `r`.`trx_wait_started`,
  now()
 ) AS `wait_age_secs`,
 `rl`.`lock_table` AS `locked_table`,
 `rl`.`lock_index` AS `locked_index`,
 `rl`.`lock_type` AS `locked_type`,
 `r`.`trx_id` AS `waiting_trx_id`,
 `r`.`trx_started` AS `waiting_trx_started`,
 timediff(now(), `r`.`trx_started`) AS `waiting_trx_age`,
 `r`.`trx_rows_locked` AS `waiting_trx_rows_locked`,
 `r`.`trx_rows_modified` AS `waiting_trx_rows_modified`,
 `r`.`trx_mysql_thread_id` AS `waiting_pid`,
 `r`.`trx_query` AS `waiting_query`,
 `rl`.`lock_id` AS `waiting_lock_id`,
 `rl`.`lock_mode` AS `waiting_lock_mode`,
 `b`.`trx_id` AS `blocking_trx_id`,
 `b`.`trx_mysql_thread_id` AS `blocking_pid`,
 `b`.`trx_query` AS `blocking_query`,
 `bl`.`lock_id` AS `blocking_lock_id`,
 `bl`.`lock_mode` AS `blocking_lock_mode`,
 `b`.`trx_started` AS `blocking_trx_started`,
 timediff(now(), `b`.`trx_started`) AS `blocking_trx_age`,
 `b`.`trx_rows_locked` AS `blocking_trx_rows_locked`,
 `b`.`trx_rows_modified` AS `blocking_trx_rows_modified`,
 concat(
  'KILL QUERY ',
  `b`.`trx_mysql_thread_id`
 ) AS `sql_kill_blocking_query`,
 concat(
  'KILL ',
  `b`.`trx_mysql_thread_id`
 ) AS `sql_kill_blocking_connection`
FROM
 `information_schema`.`innodb_lock_waits` `w`
JOIN `information_schema`.`innodb_trx` `b` ON `b`.`trx_id` = `w`.`blocking_trx_id`
JOIN `information_schema`.`innodb_trx` `r` ON `r`.`trx_id` = `w`.`requesting_trx_id`
JOIN `information_schema`.`innodb_locks` `bl` ON `bl`.`lock_id` = `w`.`blocking_lock_id`
JOIN `information_schema`.`innodb_locks` `rl` ON `rl`.`lock_id` = `w`.`requested_lock_id`
ORDER BY
 `r`.`trx_wait_started`

第四:索引相关的情况

##我们还可以来看一下索引相关的信息,查看基于索引 rows_selected总的查询了多少行,select_latency 总查询延时,rows_inserted 总的插入了多少行,insert_latency 插入延时,rows_updated 总的更新了多少行,
rows_deleted 总的删除了多少行,delete_latency 删除延时
mysql> select * from schema_index_statistics;
+--------------+------------+------------+---------------+----------------+---------------+----------------+--------------+----------------+--------------+----------------+
| table_schema | table_name | index_name | rows_selected | select_latency | rows_inserted | insert_latency | rows_updated | update_latency | rows_deleted | delete_latency |
+--------------+------------+------------+---------------+----------------+---------------+----------------+--------------+----------------+--------------+----------------+
| sysbench | sbtest6 | PRIMARY | | 29.86 s | | ps | | 2.36 s | | ps |
| sysbench | sbtest8 | PRIMARY | | 29.83 s | | ps | | 2.24 s | | ps |
| sysbench | sbtest7 | PRIMARY | | 28.25 s | | ps | | 2.22 s | | ps |
| sysbench | sbtest3 | PRIMARY | | 25.26 s | | ps | | 3.49 s | | ps |
| sysbench | sbtest5 | PRIMARY | | 25.72 s | | ps | | 2.64 s | | ps |
| sysbench | sbtest4 | PRIMARY | | 25.28 s | | ps | | 2.44 s | | ps |
| sysbench | sbtest2 | PRIMARY | | 25.14 s | | ps | | 2.27 s | | ps |
| sysbench | sbtest1 | PRIMARY | | 24.71 s | | ps | | 2.67 s | | ps |
| sysbench | sbtest9 | PRIMARY | | 23.38 s | | ps | | 2.29 s | | ps |
| sysbench | sbtest10 | PRIMARY | | 18.25 s | | ps | | 2.92 s | | ps |
| sysbench | sbtest10 | k_10 | | ps | | ps | | ps | | ps |
| sysbench | sbtest9 | k_9 | | ps | | ps | | ps | | ps |
| sysbench | sbtest2 | k_2 | | ps | | ps | | ps | | ps |
| sysbench | sbtest7 | k_7 | | ps | | ps | | ps | | ps |
| sysbench | sbtest4 | k_4 | | ps | | ps | | ps | | ps |
| sysbench | sbtest6 | k_6 | | ps | | ps | | ps | | ps |
| sysbench | sbtest8 | k_8 | | ps | | ps | | ps | | ps |
| sysbench | sbtest1 | k_1 | | ps | | ps | | ps | | ps |
| sysbench | sbtest3 | k_3 | | ps | | ps | | ps | | ps |
| sysbench | sbtest5 | k_5 | | ps | | ps | | ps | | ps |
| sys | sys_config | PRIMARY | | ps | | ps | | ps | | ps |
+--------------+------------+------------+---------------+----------------+---------------+----------------+--------------+----------------+--------------+----------------+
rows in set (0.01 sec) mysql> 从上面看,二级索引没有用到,那么请放心,还有一个视图,可以看哪些库下,哪些没有使用到的索引:object_schema 库名 object_name 表名 index_name 索引名称
mysql> select * from schema_unused_indexes;
+---------------+-------------+------------+
| object_schema | object_name | index_name |
+---------------+-------------+------------+
| sysbench | sbtest1 | k_1 |
| sysbench | sbtest10 | k_10 |
| sysbench | sbtest2 | k_2 |
| sysbench | sbtest3 | k_3 |
| sysbench | sbtest4 | k_4 |
| sysbench | sbtest5 | k_5 |
| sysbench | sbtest6 | k_6 |
| sysbench | sbtest7 | k_7 |
| sysbench | sbtest8 | k_8 |
| sysbench | sbtest9 | k_9 |
+---------------+-------------+------------+
rows in set (0.00 sec)
果然是,看到么,这些都是没有使用的索引。
那么我们来做基于二级索引的查询 mysql> explain select * from sysbench.sbtest1 where k> and k < limit ;
+----+-------------+---------+------------+-------+---------------+------+---------+------+-------+----------+----------------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+---------+------------+-------+---------------+------+---------+------+-------+----------+----------------------------------+
| | SIMPLE | sbtest1 | NULL | range | k_1 | k_1 | | NULL | | 100.00 | Using index condition; Using MRR |
+----+-------------+---------+------------+-------+---------------+------+---------+------+-------+----------+----------------------------------+
row in set, warning (0.00 sec) mysql> explain select * from sysbench.sbtest2 where k> and k < limit ;
+----+-------------+---------+------------+-------+---------------+------+---------+------+-------+----------+----------------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+---------+------------+-------+---------------+------+---------+------+-------+----------+----------------------------------+
| | SIMPLE | sbtest2 | NULL | range | k_2 | k_2 | | NULL | | 100.00 | Using index condition; Using MRR |
+----+-------------+---------+------------+-------+---------------+------+---------+------+-------+----------+----------------------------------+
row in set, warning (0.00 sec) mysql> select * from sysbench.sbtest1 where k> and k < limit ;
+----+-------+-------------------------------------------------------------------------------------------------------------------------+-------------------------------------------------------------+
| id | k | c | pad |
+----+-------+-------------------------------------------------------------------------------------------------------------------------+-------------------------------------------------------------+
| | | --------- | ---- |
| | | --------- | ---- |
| | | --------- | ---- |
| | | --------- | ---- |
| | | --------- | ---- |
| | | --------- | ---- |
| | | --------- | ---- |
| | | --------- | ---- |
| | | --------- | ---- |
| | | --------- | ---- |
+----+-------+-------------------------------------------------------------------------------------------------------------------------+-------------------------------------------------------------+
rows in set (0.08 sec) mysql> select * from schema_unused_indexes;
+---------------+-------------+------------+
| object_schema | object_name | index_name |
+---------------+-------------+------------+
| sysbench | sbtest10 | k_10 |
| sysbench | sbtest2 | k_2 |
| sysbench | sbtest3 | k_3 |
| sysbench | sbtest4 | k_4 |
| sysbench | sbtest5 | k_5 |
| sysbench | sbtest6 | k_6 |
| sysbench | sbtest7 | k_7 |
| sysbench | sbtest8 | k_8 |
| sysbench | sbtest9 | k_9 |
+---------------+-------------+------------+
rows in set (0.00 sec) 果然少了k_1这条记录,那么我们再来跑一条SQL
mysql> select * from sysbench.sbtest2 where k> and k < limit ;
+----+-------+-------------------------------------------------------------------------------------------------------------------------+-------------------------------------------------------------+
| id | k | c | pad |
+----+-------+-------------------------------------------------------------------------------------------------------------------------+-------------------------------------------------------------+
| | | --------- | ---- |
| | | --------- | ---- |
| | | --------- | ---- |
| | | --------- | ---- |
| | | --------- | ---- |
| | | --------- | ---- |
| | | --------- | ---- |
| | | --------- | ---- |
| | | --------- | ---- |
| | | --------- | ---- |
+----+-------+-------------------------------------------------------------------------------------------------------------------------+-------------------------------------------------------------+
rows in set (3.56 sec)
mysql> select * from schema_unused_indexes;
+---------------+-------------+------------+
| object_schema | object_name | index_name |
+---------------+-------------+------------+
| sysbench | sbtest10 | k_10 |
| sysbench | sbtest3 | k_3 |
| sysbench | sbtest4 | k_4 |
| sysbench | sbtest5 | k_5 |
| sysbench | sbtest6 | k_6 |
| sysbench | sbtest7 | k_7 |
| sysbench | sbtest8 | k_8 |
| sysbench | sbtest9 | k_9 |
+---------------+-------------+------------+
rows in set (0.00 sec) 果然又少了k_2,这个时候我们再来回看一下schema_index_statistics视图,k_1,k_2 就有数据了,基于索引总共扫描了多少行,查询总延时,
从这个也可以看到sysbench 的oltp.lua脚本压测都是基于主键,跟实际场景有很大差异,不太建议用这个压测工具压测MySQL,用TPCC-MySQL来做压测比较贴合实际情况
mysql> select * from schema_index_statistics;
+--------------+------------+------------+---------------+----------------+---------------+----------------+--------------+----------------+--------------+----------------+
| table_schema | table_name | index_name | rows_selected | select_latency | rows_inserted | insert_latency | rows_updated | update_latency | rows_deleted | delete_latency |
+--------------+------------+------------+---------------+----------------+---------------+----------------+--------------+----------------+--------------+----------------+
| sysbench | sbtest8 | PRIMARY | | 41.50 s | | ps | | 3.70 s | | ps |
| sysbench | sbtest6 | PRIMARY | | 41.05 s | | ps | | 3.87 s | | ps |
| sysbench | sbtest7 | PRIMARY | | 39.66 s | | ps | | 3.73 s | | ps |
| sysbench | sbtest5 | PRIMARY | | 37.13 s | | ps | | 4.12 s | | ps |
| sysbench | sbtest3 | PRIMARY | | 36.30 s | | ps | | 4.96 s | | ps |
| sysbench | sbtest4 | PRIMARY | | 36.42 s | | ps | | 3.89 s | | ps |
| sysbench | sbtest2 | PRIMARY | | 36.15 s | | ps | | 3.74 s | | ps |
| sysbench | sbtest1 | PRIMARY | | 35.75 s | | ps | | 4.17 s | | ps |
| sysbench | sbtest9 | PRIMARY | | 34.59 s | | ps | | 3.81 s | | ps |
| sysbench | sbtest10 | PRIMARY | | 29.17 s | | ps | | 4.40 s | | ps |
| sysbench | sbtest2 | k_2 | | 3.53 s | | ps | | ps | | ps |
| sysbench | sbtest1 | k_1 | | 39.70 ms | | ps | | ps | | ps |
| sysbench | sbtest10 | k_10 | | ps | | ps | | ps | | ps |
| sysbench | sbtest9 | k_9 | | ps | | ps | | ps | | ps |
| sysbench | sbtest7 | k_7 | | ps | | ps | | ps | | ps |
| sysbench | sbtest4 | k_4 | | ps | | ps | | ps | | ps |
| sysbench | sbtest6 | k_6 | | ps | | ps | | ps | | ps |
| sysbench | sbtest8 | k_8 | | ps | | ps | | ps | | ps |
| sysbench | sbtest3 | k_3 | | ps | | ps | | ps | | ps |
| sysbench | sbtest5 | k_5 | | ps | | ps | | ps | | ps |
| sys | sys_config | PRIMARY | | ps | | ps | | ps | | ps |
+--------------+------------+------------+---------------+----------------+---------------+----------------+--------------+----------------+--------------+----------------+
rows in set (0.01 sec) mysql>
这跟pt-index-usage只能分析慢查询日志来判断索引使用情况相对好一些

第五:SQL相关的情况

##查询有生成临时表的SQL:如exec_count 总的执行的次数 ,tmp_tables 总的生成临时表
mysql> select * from x$statement_analysis where db ='sysbench' and tmp_tables > 0 and query not like 'show%' ORDER BY tmp_disk_tables DESC LIMIT 3 \G
*************************** 1. row ***************************
query: SELECT DISTINCTROW `c` FROM `sbtest10` WHERE `id` BETWEEN ? AND ? + ? ORDER BY `c`
db: sysbench
full_scan:
exec_count: 243308
err_count: 0
warn_count: 0
total_latency: 421664798643000
max_latency: 13932957371000
avg_latency: 1733049000
lock_latency: 20795990000000
rows_sent: 24330900
rows_sent_avg: 100
rows_examined: 72992700
rows_examined_avg: 300
rows_affected: 0
rows_affected_avg: 0
tmp_tables: 243311
tmp_disk_tables: 0
rows_sorted: 24330500
sort_merge_passes: 0
digest: 7bec897913ccc856e2ea999413cc9ad1
first_seen: 2016-06-19 11:25:23
last_seen: 2016-06-19 13:57:05
*************************** 2. row ***************************
query: SELECT DISTINCTROW `c` FROM `sbtest9` WHERE `id` BETWEEN ? AND ? + ? ORDER BY `c`
db: sysbench
full_scan:
exec_count: 242949
err_count: 0
warn_count: 0
total_latency: 392145676272000
max_latency: 5201577387000
avg_latency: 1614106000
lock_latency: 20798157000000
rows_sent: 24295400
rows_sent_avg: 100
rows_examined: 72886500
rows_examined_avg: 300
rows_affected: 0
rows_affected_avg: 0
tmp_tables: 242954
tmp_disk_tables: 0
rows_sorted: 24295300
sort_merge_passes: 0
digest: 5326958ce3e23f6fea751ada9dfa05a4
first_seen: 2016-06-19 11:25:23
last_seen: 2016-06-19 13:57:05
*************************** 3. row ***************************
query: SELECT DISTINCTROW `c` FROM `sbtest7` WHERE `id` BETWEEN ? AND ? + ? ORDER BY `c`
db: sysbench
full_scan:
exec_count: 242815
err_count: 0
warn_count: 0
total_latency: 388029765111000
max_latency: 2019677047000
avg_latency: 1598046000
lock_latency: 20501920000000
rows_sent: 24282000
rows_sent_avg: 100
rows_examined: 72846600
rows_examined_avg: 300
rows_affected: 0
rows_affected_avg: 0
tmp_tables: 242821
tmp_disk_tables: 0
rows_sorted: 24281600
sort_merge_passes: 0
digest: 9483723c685eb28e9cfc506b30578698
first_seen: 2016-06-19 11:25:24
last_seen: 2016-06-19 13:57:05
3 rows in set (0.00 sec)

mysql>
##还可以用这个视图,查看生成临时表的SQL执行情况,如:exec_count 执行了多少次,memory_tmp_tables 生成多少临时表,tmp_tables_to_disk_pct 硬盘临时表比例
mysql> select * from x$statements_with_temp_tables where db='sysbench' and query not like 'show%' limit 3\G;
*************************** 1. row ***************************
query: SELECT DISTINCTROW `c` FROM `sbtest4` WHERE `id` BETWEEN ? AND ? + ? ORDER BY `c`
db: sysbench
exec_count: 249558
total_latency: 405559669482000
memory_tmp_tables: 249563
disk_tmp_tables: 0
avg_tmp_tables_per_query: 1
tmp_tables_to_disk_pct: 0
first_seen: 2016-06-19 11:25:24
last_seen: 2016-06-19 14:00:36
digest: 884e86516aebcd28e0da39d6a7df164a
*************************** 2. row ***************************
query: SELECT DISTINCTROW `c` FROM `sbtest5` WHERE `id` BETWEEN ? AND ? + ? ORDER BY `c`
db: sysbench
exec_count: 249542
total_latency: 391186176853000
memory_tmp_tables: 249550
disk_tmp_tables: 0
avg_tmp_tables_per_query: 1
tmp_tables_to_disk_pct: 0
first_seen: 2016-06-19 11:25:24
last_seen: 2016-06-19 14:00:36
digest: bff86a3aa37ae0242218d5657499e5a2
*************************** 3. row ***************************
query: SELECT DISTINCTROW `c` FROM `sbtest10` WHERE `id` BETWEEN ? AND ? + ? ORDER BY `c`
db: sysbench
exec_count: 249459
total_latency: 428970366771000
memory_tmp_tables: 249462
disk_tmp_tables: 0
avg_tmp_tables_per_query: 1
tmp_tables_to_disk_pct: 0
first_seen: 2016-06-19 11:25:23
last_seen: 2016-06-19 14:00:36
digest: 7bec897913ccc856e2ea999413cc9ad1
3 rows in set (0.01 sec)

##查看有全表扫描的SQL ,exec_count 全表扫描SQL 总的执行了多少次,tmp_tables 全表扫描产生的临时表,tmp_disk_tables 全表扫描在磁盘中生成的临时表
mysql> SELECT * FROM statement_analysis WHERE db='sysbench' AND full_scan = '*' AND query not like 'show%' AND exec_count > 1\G
*************************** 1. row ***************************
query: SELECT * FROM `sbtest5`
db: sysbench
full_scan: *
exec_count: 2
err_count: 0
warn_count: 0
total_latency: 600.90 ms
max_latency: 456.60 ms
avg_latency: 300.45 ms
lock_latency: 473.00 us
rows_sent: 200000
rows_sent_avg: 100000
rows_examined: 200000
rows_examined_avg: 100000
rows_affected: 0
rows_affected_avg: 0
tmp_tables: 0
tmp_disk_tables: 0
rows_sorted: 0
sort_merge_passes: 0
digest: 146002080199f7b82624570fc9f622b1
first_seen: 2016-06-19 14:20:17
last_seen: 2016-06-19 14:21:01
*************************** 2. row ***************************
query: SELECT `QUERY_ID` , SUM ( `DUR ... ROFILING` GROUP BY `QUERY_ID`
db: sysbench
full_scan: *
exec_count: 5
err_count: 0
warn_count: 5
total_latency: 53.22 ms
max_latency: 48.88 ms
avg_latency: 10.64 ms
lock_latency: 1.19 ms
rows_sent: 62
rows_sent_avg: 12
rows_examined: 1280
rows_examined_avg: 256
rows_affected: 0
rows_affected_avg: 0
tmp_tables: 10
tmp_disk_tables: 0
rows_sorted: 62
sort_merge_passes: 0
digest: c7d88fa65a7f375625dd708d7f208b15
first_seen: 2016-06-19 11:34:44
last_seen: 2016-06-19 14:20:17

 小结:

sys丰富的性能视图可以很方便诊断数据库,可以替代不少pt工具,因为SYS库下的视图是居于performance_schema ,有些时候可能会把这个shema优化,要用这个sys库的视图,就不能优化这个选项,所以配置文件不能设置performance_schema=off,同时可以看到,sysbench oltp.lua压测都是居于主键压测,要比较真实反应线上库性能,要用备份数据,在压测环境去压测,同时有关数据要脱敏。

MySQL 5.7 SYS scheme解析的更多相关文章

  1. 对MySQL DELETE语法的详细解析

    以下的文章主要描述的是MySQL DELETE语法的详细解析,首先我们是从单表语法与多表语法的示例开始的,假如你对MySQL DELETE语法的相关内容十分感兴趣的话,你就可以浏览以下的文章对其有个更 ...

  2. MySQL慢日志查询全解析:从参数、配置到分析工具【转】

    转自: MySQL慢日志查询全解析:从参数.配置到分析工具 - MySQL - DBAplus社群——围绕数据库.大数据.PaaS云,运维圈最专注围绕“数据”的学习交流和专业社群http://dbap ...

  3. 安装mysql报错:Can't find messagefile '/usr/share/mysql/english/errmsg.sys'和/usr/bin/mysqladmin: error while loading shared libraries: libmysqlclient.so.16: cannot open shared object file: No such file or

    使用yum安装mysql服务端: [root@centos ~]# yum -y install mysql-server Loaded plugins: fastestmirror, securit ...

  4. 100道MySQL数据库经典面试题解析(收藏版)

    前言 100道MySQL数据库经典面试题解析,已经上传github啦 https://github.com/whx123/JavaHome/tree/master/Java面试题集结号 公众号:捡田螺 ...

  5. 【MySql】[ERROR] Can't read from messagefile '/usr/share/mysql/english/errmsg.sys'

    [root@zhang bin]# ./mysql_install_db --datadir=/usr/local/mysql/mydata/data/ 2018-08-18 03:09:14 [WA ...

  6. 【MySQL】MySQL 5.7 sys Schema

    sys库说明:http://dev.mysql.com/doc/refman/5.7/en/sys-schema-usage.html sys库使用说明:http://dev.mysql.com/do ...

  7. MySQL 5.7 SYS系统SCHEMA

    版权声明:本文为博主原创文章,未经博主允许不得转载. 在说明系统数据库之前,先来看下MySQL在数据字典方面的演变历史:MySQL4.1 提供了information_schema 数据字典.从此可以 ...

  8. MYSQL常见出错mysql_errno()代码解析

    如题,今天遇到怎么一个问题, 在理论上代码是不会有问题的,但是还是报了如上的错误,把sql打印出來放到DB中却可以正常执行.真是郁闷,在百度里面 渡 了很久没有相关的解释,到时找到几个没有人回复的 & ...

  9. mysql之 binlog维护详细解析(开启、binlog相关参数作用、mysqlbinlog解读、binlog删除)

    binary log 作用:主要实现三个重要的功能:用于复制,用于恢复,用于审计.binary log 相关参数:log_bin设置此参数表示启用binlog功能,并指定路径名称log_bin_ind ...

随机推荐

  1. ios技术面试题

    1.Difference between shallow copy and deep copy? 浅复制 只拷贝地址 不拷贝地址指向的对象 深复制 拷贝地址 并且指向拷贝的新对象 2.What is ...

  2. (菜鸟要飞系列)五,基于Asp.Net MVC5的后台管理系统(添加数据表的分页功能)

    献上代码 ) { List<UserModel> arrayUserModel = new List<UserModel>(); string strText = Reques ...

  3. redis常见性能问题和解决方案?

    Master写内存快照,save命令调度rdbSave函数,会阻塞主线程的工作,当快照比较大时对性能影响是非常大的,会间断性暂停服务,所以Master最好不要写内存快照. Master AOF持久化, ...

  4. SSMS 2008R2没有智能感知方法解决

    有时SSMS会莫明奇妙的没有了智能感知(前一天还是有的, 第2天就没有了) 在网上查到有如下原因: 1. 服务器上有Offline的DB 解决方案: 将Offline的DB删掉或者设成online即可 ...

  5. bnuoj 4208 Bubble sort

    http://www.bnuoj.com/bnuoj/problem_show.php?pid=4208 [题意]:如题,求冒泡排序遍历趟数 [题解]:这题开始2B了,先模拟TLE,然后想了一下,能不 ...

  6. dbutils报错:com.microsoft.sqlserver.jdbc.SQLServerException: 无法识别元数据的表

    今天用dbutils操作数据库,莫名地报错:com.microsoft.sqlserver.jdbc.SQLServerException: 无法识别元数据的表 检查了sql语句没有问题.经过仔细排查 ...

  7. c++ memset 函数 及 坑

    #include <string.h> #include <stdio.h> typedef struct ss{ int num; ][]; }tent; tent a; i ...

  8. P2661 信息传递 强连通分量

    题目链接: http://www.luogu.org/problem/show?pid=2661 题解: 这题求最小的单向环. 可因为每个节点初度为1,所以所有的强联通分量都只能是单向环. 所以就是有 ...

  9. java.lang.UnsatisfiedLinkError: D:\Tomcat-7.0.59\apache-tomcat-7.0.59\bin\tcnative-1.dll: Can't load IA 32-bit .dll on a AMD 64-bit platform

    今日上午用Tomcat运行一个小项目,报出以下异常信息: java.lang.UnsatisfiedLinkError: D:\Tomcat-7.0.59\apache-tomcat-7.0.59\b ...

  10. 【转载】关于typedef的用法总结

    不管实在C还是C++代码中,typedef这个词都不少见,当然出现频率较高的还是在C代码中.typedef与#define有些相似,但更多的是不同,特别是在一些复杂的用法上,就完全不同了,看了网上一些 ...