MySQL 5.7 SYS scheme解析

Posted 文采飞扬

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MySQL 5.7 SYS scheme解析相关的知识,希望对你有一定的参考价值。

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请求情况
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 | 200669 | 560038986996000 | 8997005086000 | 23331049000000 |   3120190 |       7153244 |         40115 |          0 |
| localhost     |    109 |   2130722779000 |  712619558000 |    90944000000 |       119 |          1269 |             0 |          5 |
| background    |      0 |               0 |             0 |              0 |         0 |             0 |             0 |          0 |
+---------------+--------+-----------------+---------------+----------------+-----------+---------------+---------------+------------+
3 rows in set (0.01 sec)

##查看host相关IO相关的情况
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              |   24841 | 163211274405180 |  6570237410 |  913839905965 |
| 192.168.23.10 | wait/io/table/sql/handler            | 6244558 |  88270585548280 |    14135530 | 3369646613620 |
| background    | wait/io/file/sql/binlog              |  128486 |  44014622163915 |   342563495 | 3842357737710 |
| background    | wait/io/file/innodb/innodb_data_file |    1846 |  14855266117140 |  8047272805 |  882840258320 |
| background    | wait/io/file/innodb/innodb_log_file  |     547 |  13470228043550 | 24625645405 | 1777250192660 |
| 192.168.23.10 | wait/io/file/innodb/innodb_data_file |     584 |  11086987247300 | 18984567010 |  380929741155 |
| 192.168.23.10 | wait/io/file/innodb/innodb_log_file  |   13877 |  10311024630260 |   743029715 |  331577308990 |
| background    | wait/io/file/sql/FRM                 |    1404 |   1405484392445 |  1001056970 |  584735335675 |
| localhost     | wait/io/file/sql/FRM                 |     526 |   1067680283670 |  2029810380 |  238569415220 |
| 192.168.23.10 | wait/lock/table/sql/handler          |  266974 |    551579769605 |     2065765 |   25834904220 |
| background    | wait/io/file/myisam/kfile            |      33 |    473172383825 | 14338556910 |  331729231625 |
| 192.168.23.10 | wait/io/file/sql/FRM                 |     130 |     84844547115 |   652650285 |   31024603660 |
| localhost     | wait/io/file/myisam/dfile            |      25 |     56702742150 |  2268109625 |   23357858920 |
| localhost     | wait/io/file/sql/file_parser         |     204 |     28396423845 |   139198035 |   23793573990 |
| background    | wait/io/file/mysys/cnf               |       5 |     19273647840 |  3854729385 |   19169800220 |
| background    | wait/io/file/myisam/dfile            |      24 |     13431475015 |   559644500 |   11908891835 |
| background    | wait/io/file/mysys/charset           |       3 |      9642786060 |  3214262020 |    9606707000 |
| background    | wait/io/file/sql/ERRMSG              |       5 |      8360195485 |  1672038975 |    3770732080 |
| localhost     | wait/io/file/myisam/kfile            |       4 |      1356593640 |   339148410 |    1331465300 |
| background    | wait/io/file/sql/binlog_index        |      15 |      1232293025 |    82152665 |    1120797225 |
| background    | wait/io/file/sql/pid                 |       3 |       296007380 |    98669025 |     207822120 |
| localhost     | wait/io/file/sql/dbopt               |      12 |       214129520 |    17844025 |      83346740 |
| background    | wait/io/file/sql/casetest            |      10 |       148326380 |    14832455 |      52451460 |
| 192.168.23.10 | wait/io/file/sql/dbopt               |       4 |        79659900 |    19914975 |      70984480 |
| background    | wait/io/file/sql/global_ddl_log      |       2 |        18142620 |     9071310 |      15014540 |
+---------------+--------------------------------------+---------+-----------------+-------------+---------------+
25 rows in set (0.01 sec)

mysql> 
##查看用户相关的延时
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              |   27875 | 224727885984870 |  8061986920 | 2402537542350 |
| sysbench | wait/io/table/sql/handler            | 7068872 |  96860278959465 |    13702125 | 3369646613620 |
| sysbench | wait/io/file/innodb/innodb_log_file  |   15628 |  20073781544475 |  1284475170 | 1609081505740 |
| sysbench | wait/io/file/innodb/innodb_data_file |     598 |  11246278276650 | 18806485135 |  380929741155 |
| root     | wait/io/file/sql/FRM                 |     526 |   1067680283670 |  2029810380 |  238569415220 |
| sysbench | wait/lock/table/sql/handler          |  302216 |    613611714840 |     2030080 |   25834904220 |
| sysbench | wait/io/file/sql/FRM                 |     130 |     84844547115 |   652650285 |   31024603660 |
| root     | wait/io/file/myisam/dfile            |      25 |     56702742150 |  2268109625 |   23357858920 |
| root     | wait/io/file/sql/file_parser         |     204 |     28396423845 |   139198035 |   23793573990 |
| root     | wait/io/file/myisam/kfile            |       4 |      1356593640 |   339148410 |    1331465300 |
| root     | wait/io/file/sql/dbopt               |      12 |       214129520 |    17844025 |      83346740 |
| sysbench | wait/io/file/sql/dbopt               |       4 |        79659900 |    19914975 |      70984480 |
+----------+--------------------------------------+---------+-----------------+-------------+---------------+
12 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   |     367069 |  1332340924078000 |       3629674323.0237 |           0 |    48220 | 297374827396840 |                   8 |                 8 |            1 |              0 |                      0 |
| root       |        115 |     2249030116000 |      19556783617.3913 |          11 |      771 |   1154350172825 |                   1 |                 1 |            1 |              0 |                      0 |
| background |          0 |                 0 |                0.0000 |           0 |   132723 |  89773752093085 |                  25 |                27 |            0 |              0 |                      0 |
+------------+------------+-------------------+-----------------------+-------------+----------+-----------------+---------------------+-------------------+--------------+----------------+------------------------+
3 rows in set (0.48 sec)

mysql> 

 

 

第二:IO相关情况

 

##可以通过这几个视图来看一下数具体数据文件IO相关的情况
#查看相关数据文件总的读取字节,平均读取字节,总的写的字节,平均写的自己,以及写比例
 mysql> select * from x$io_global_by_file_by_bytes ORDER BY total desc limit 10;
+-----------------------------------------+------------+------------+------------+-------------+---------------+-------------+------------+-----------+
| file                                    | count_read | total_read | avg_read   | count_write | total_written | avg_write   | total      | write_pct |
+-----------------------------------------+------------+------------+------------+-------------+---------------+-------------+------------+-----------+
| /data/3306/logs/mysql-bin.000006        |     128456 | 1052302029 |  8191.9259 |           0 |             0 |      0.0000 | 1052302029 |      0.00 |
| /data/3306/data/ib_logfile0             |          7 |      70144 | 10020.5714 |       52836 |     177236480 |   3354.4644 |  177306624 |     99.96 |
| /data/3306/logs/mysql-bin.000007        |          0 |          0 |     0.0000 |       52464 |      94883118 |   1808.5376 |   94883118 |    100.00 |
| /data/3306/data/ibdata1                 |        284 |    6766592 | 23826.0282 |         633 |      48414720 |  76484.5498 |   55181312 |     87.74 |
| /data/3306/data/sysbench/sbtest3.ibd    |        734 |   12025856 | 16384.0000 |         140 |       2293760 |  16384.0000 |   14319616 |     16.02 |
| /data/3306/data/ibtmp1                  |          0 |          0 |     0.0000 |          46 |      13139968 | 285651.4783 |   13139968 |    100.00 |
| /data/3306/data/mysql/proc.MYD          |        204 |     605753 |  2969.3775 |           0 |             0 |      0.0000 |     605753 |      0.00 |
| /data/3306/logs/mysql-bin.000001        |         14 |      99474 |  7105.2857 |           0 |             0 |      0.0000 |      99474 |      0.00 |
| /opt/app/mysql/share/english/errmsg.sys |          3 |      75456 | 25152.0000 |           0 |             0 |      0.0000 |      75456 |      0.00 |
| /data/3306/data/mysql/engine_cost.ibd   |          4 |      65536 | 16384.0000 |           0 |             0 |      0.0000 |      65536 |      0.00 |
+-----------------------------------------+------------+------------+------------+-------------+---------------+-------------+------------+-----------+
10 rows in set (0.00 sec)

mysql> 

mysql> select * from x$io_global_by_file_by_latency ORDER BY total_latency desc limit 10;
+---------------------------------------------------+--------+-----------------+------------+----------------+-------------+-----------------+------------+-----------------+
| file                                              | total  | total_latency   | count_read | read_latency   | count_write | write_latency   | count_misc | misc_latency    |
+---------------------------------------------------+--------+-----------------+------------+----------------+-------------+-----------------+------------+-----------------+
| /data/3306/logs/mysql-bin.000007                  | 104272 | 857635813928830 |          0 |              0 |       59585 | 128645758363315 |      44687 | 728990055565515 |
| /data/3306/data/ib_logfile0                       |  61003 | 130330488448400 |          7 |    23089028260 |       59975 |  70847663102440 |       1021 |  59459736317700 |
| /data/3306/logs/mysql-bin.000006                  | 128462 |  43435687994120 |     128456 | 43435586393740 |           0 |               0 |          6 |       101600380 |
| /data/3306/data/ibdata1                           |   1567 |  21265602188520 |        284 |   763552576960 |         633 |   7409130660570 |        650 |  13092918950990 |
| /data/3306/data/sysbench/sbtest3.ibd              |   1048 |  14948103372255 |        740 | 12899909284700 |         140 |     20390455360 |        168 |   2027803632195 |
| /data/3306/data/mysql/db.frm                      |     13 |    584825990215 |          7 |   584777107255 |           0 |               0 |          6 |        48882960 |
| /data/3306/logs/mysql-bin.000001                  |     17 |    573208795500 |         14 |   573147683260 |           0 |               0 |          3 |        61112240 |
| /data/3306/data/mysql/db.MYI                      |      4 |    331753618205 |          2 |   331732946525 |           0 |               0 |          2 |        20671680 |
| /data/3306/data/sys/[email protected]_global_total.frm |      6 |    239301100460 |          2 |   238573608360 |           0 |               0 |          4 |       727492100 |
| /data/3306/data/mysql/server_cost.frm             |     13 |    181650638825 |          7 |   181592420425 |           0 |               0 |          6 |        58218400 |
+---------------------------------------------------+--------+-----------------+------------+----------------+-------------+-----------------+------------+-----------------+
10 rows in set (0.00 sec)

mysql> 

##可以通过下面这几个视图,看一下binlog redolog innodb数据文件等相关数据文件的IO情况:比如总的延时,最小延时,最大延时
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              | 244630 | 952619534005725 |      198860 |  3894123795 | 14005923994180 |     128470 | 1052401503 |  8191.8074 |       66365 |     119844266 |   1805.8354 |      1172245769 |
| innodb/innodb_log_file  |  67875 | 133766003954530 |      871080 |  1970769700 |  6905414126165 |          7 |      70144 | 10020.5714 |       66784 |     223841280 |   3351.7202 |       223911424 |
| innodb/innodb_data_file |   2843 |  36773287483315 |           0 | 12934676985 |   926390076160 |       1097 |   20086784 | 18310.6509 |         819 |      63848448 |  77959.0330 |        83935232 |
| sql/FRM                 |   2251 |   2561727311090 |           0 |  1138039485 |   584735335675 |       1070 |     645174 |   602.9664 |           0 |             0 |      0.0000 |          645174 |
| myisam/kfile            |     37 |    474528977465 |      984540 | 12825107260 |   331729231625 |         18 |       4926 |   273.6667 |           0 |             0 |      0.0000 |            4926 |
| myisam/dfile            |    332 |     77683544835 |      183000 |   233986545 |    23357858920 |        208 |     608353 |  2924.7740 |           0 |             0 |      0.0000 |          608353 |
| sql/file_parser         |    104 |     25346760565 |           0 |   243718790 |    23793573990 |          1 |        720 |   720.0000 |           0 |             0 |      0.0000 |             720 |
| mysys/cnf               |      5 |     19273647840 |      561200 |  3854729385 |    19169800220 |          3 |         56 |    18.6667 |           0 |             0 |      0.0000 |              56 |
| mysys/charset           |      3 |      9642786060 |    10367560 |  3214262020 |     9606707000 |          1 |      18710 | 18710.0000 |           0 |             0 |      0.0000 |           18710 |
| sql/ERRMSG              |      5 |      8360195485 |    18419560 |  1672038975 |     3770732080 |          3 |      75456 | 25152.0000 |           0 |             0 |      0.0000 |           75456 |
| sql/binlog_index        |     15 |      1232293025 |           0 |    82152665 |     1120797225 |          2 |        198 |    99.0000 |           0 |             0 |      0.0000 |             198 |
| sql/pid                 |      3 |       296007380 |     7516420 |    98669025 |      207822120 |          0 |          0 |     0.0000 |           1 |             5 |      5.0000 |               5 |
| sql/dbopt               |     16 |       293789420 |      207400 |    18361610 |       83346740 |          0 |          0 |     0.0000 |           0 |             0 |      0.0000 |               0 |
| sql/casetest            |     10 |       148326380 |           0 |    14832455 |       52451460 |          0 |          0 |     0.0000 |           0 |             0 |      0.0000 |               0 |
| sql/global_ddl_log      |      2 |        18142620 |           0 |     9071310 |       15014540 |          0 |          0 |     0.0000 |           0 |             0 |      0.0000 |               0 |
| archive/data            |      1 |         8641260 |           0 |     8641260 |        8641260 |          0 |          0 |     0.0000 |           0 |             0 |      0.0000 |               0 |
+-------------------------+--------+-----------------+-------------+-------------+----------------+------------+------------+------------+-------------+---------------+-------------+-----------------+
16 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              | 254738 | 1041025347208805 |  4086651080 | 14005923994180 | 44008734077000 | 150831466222860 | 846185146908945 |     128470 | 1052401503 |  8191.8074 |       72233 |     130580710 |   1807.7708 |
| innodb/innodb_log_file  |  73858 |  146987100334955 |  1990130795 |  6905414126165 |    23089028260 |  79908484855465 |  67055526451230 |          7 |      70144 | 10020.5714 |       72674 |     243804160 |   3354.7646 |
| innodb/innodb_data_file |   2866 |   37182251112810 | 12973569670 |   926390076160 | 14299955007730 |   7562926369195 |  15319369735885 |       1104 |   20201472 | 18298.4348 |         827 |      67190784 |  81246.4135 |
| sql/FRM                 |   2251 |    2561727311090 |  1138039485 |   584735335675 |  2539017648540 |               0 |     22709662550 |       1070 |     645174 |   602.9664 |           0 |             0 |      0.0000 |
| myisam/dfile            |    332 |      77683544835 |   233986545 |    23357858920 |    77180177715 |               0 |       503367120 |        208 |     608353 |  2924.7740 |           0 |             0 |      0.0000 |
| sql/file_parser         |    104 |      25346760565 |   243718790 |    23793573990 |    23793573990 |               0 |      1553186575 |          1 |        720 |   720.0000 |           0 |             0 |      0.0000 |
| myisam/kfile            |     37 |     474528977465 | 12825107260 |   331729231625 |   474326969865 |               0 |       202007600 |         18 |       4926 |   273.6667 |           0 |             0 |      0.0000 |
| sql/dbopt               |     16 |        293789420 |    18361610 |       83346740 |              0 |               0 |       293789420 |          0 |          0 |     0.0000 |           0 |             0 |      0.0000 |
| sql/binlog_index        |     15 |       1232293025 |    82152665 |     1120797225 |        5070320 |               0 |      1227222705 |          2 |        198 |    99.0000 |           0 |             0 |      0.0000 |
| sql/casetest            |     10 |        148326380 |    14832455 |       52451460 |              0 |               0 |       148326380 |          0 |          0 |     0.0000 |           0 |             0 |      0.0000 |
| sql/ERRMSG              |      5 |       8360195485 |  1672038975 |     3770732080 |     4789648020 |               0 |      3570547465 |          3 |      75456 | 25152.0000 |           0 |             0 |      0.0000 |
| mysys/cnf               |      5 |      19273647840 |  3854729385 |    19169800220 |    19173307720 |               0 |       100340120 |          3 |         56 |    18.6667 |           0 |             0 |      0.0000 |
| sql/pid                 |      3 |        296007380 |    98669025 |      207822120 |              0 |        80668840 |       215338540 |          0 |          0 |     0.0000 |           1 |             5 |      5.0000 |
| mysys/charset           |      3 |       9642786060 |  3214262020 |     9606707000 |     9606707000 |               0 |        36079060 |          1 |      18710 | 18710.0000 |           0 |             0 |      0.0000 |
| sql/global_ddl_log      |      2 |         18142620 |     9071310 |       15014540 |              0 |               0 |        18142620 |          0 |          0 |     0.0000 |           0 |             0 |      0.0000 |
| archive/data            |      1 |          8641260 |     8641260 |        8641260 |              0 |               0 |         8641260 |          0 |          0 |     0.0000 |           0 |             0 |      0.0000 |
+-------------------------+--------+------------------+-------------+----------------+----------------+-----------------+-----------------+------------+------------+------------+-------------+---------------+-------------+
16 rows in set (0.38 sec)

从这边来看,binlog IO压力比较大。

 

第三:锁相关情况

 

##看一下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 | 10483 |        10483 |     10483 |      153684 |
| InnoDB System | 9.67 MiB   | 8.80 MiB   |   619 |          619 |       619 |        9908 |
| mysql         | 240.00 KiB | 7.75 KiB   |    15 |           15 |        15 |          97 |
+---------------+------------+------------+-------+--------------+-----------+-------------+
3 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 |  1504 |         1504 |      1504 |      100819 |
| sysbench      | sbtest1                   | 23.47 MiB | 20.90 MiB |  1502 |         1502 |      1502 |      100750 |
| InnoDB System | SYS_TABLES                | 22.14 MiB | 20.37 MiB |  1417 |         1417 |      1417 |       69711 |
| sysbench      | sbtest9                   | 18.27 MiB | 16.10 MiB |  1169 |         1169 |      1169 |       88908 |
| sysbench      | sbtest8                   | 14.31 MiB | 12.43 MiB |   916 |          916 |       916 |       81720 |
| sysbench      | sbtest6                   | 14.22 MiB | 12.40 MiB |   910 |          910 |       910 |       79381 |
| sysbench      | sbtest3                   | 14.17 MiB | 12.36 MiB |   907 |          907 |       907 |       79017 |
| sysbench      | sbtest7                   | 14.16 MiB | 12.34 MiB |   906 |          906 |       906 |       79234 |
| sysbench      | sbtest5                   | 14.09 MiB | 12.29 MiB |   902 |          902 |       902 |       78814 |
| sysbench      | sbtest4                   | 14.06 MiB | 12.28 MiB |   900 |          900 |       900 |       78797 |
| sysbench      | sbtest2                   | 14.00 MiB | 12.20 MiB |   896 |          896 |       896 |       78876 |
| InnoDB System | SYS_FOREIGN               | 32.00 KiB | 0 bytes   |     2 |            2 |         2以上是关于MySQL 5.7 SYS scheme解析的主要内容,如果未能解决你的问题,请参考以下文章

通过 SYS 查看 MySQL 5.7 的锁

MySQL 5.7 SYS系统SCHEMA

MySQL- 5.7 sys schema

MySQL 5.7 新增默认账号 mysql.session和mysql.sys

MySQL 5.7 sys.schema_redundant_indexes 优化案例

性能优化利器:剖析MySQL 5.7新特征 sys schema