MYSQL Advanced - Query Optimization - Actual STATUS

Sending back to the city – " 100 days proficient in MYSQL from entry to employment"

There is a book giving event at the end of the article, you can participate!

1. Exercise topics

topic link difficulty
Advanced SQL - Query Optimization - SHOW STATUS ★★★☆☆

Two, SQL ideas

Advanced SQL - Query Optimization - SHOW STATUS

insert image description here

Initialization data

这里写入初始化表结构,初始化数据的sql

solution

What is SHOW STATUS?

SHOW STATUS can get some status of mysql service, these status are the performance parameters of mysql service!
grammar:

SHOW [SESSION | GLOBAL] STATUS

SESSION means to obtain the performance parameters at the current session level, and GLOBAL means to obtain the performance parameters at the global level, and SESSION and GLOBAL can be omitted. If omitted, the default is SESSION.
b
insert image description here
There are many parameters, so when searching for specified parameters, the following syntax can be used:

SHOW [SESSION | GLOBAL] STATUS LIKE 'status_name';

Where status_name is the parameter name of the status.

Practical experience: common mysql status query

1. QPS (number of requests processed per second)

QPS is the number of sql executed by mysql per second, but not limited to select, instert, update and delete statements.

QPS = Questions(or Queries) / seconds

show global status like 'Question%';

insert image description here

 show global status like 'uptime_since_flush_status';

insert image description here

Calculation idea:

First get the data of Question1 from show global status like 'Question%'; After a period of time, calculate show global status like 'Question%'; to get the data of Question2.

QPS = (Question2-Question1)/(uptime_since_flush_status2-uptime_since_flush_status1)

The difference between Questions and Queries

insert image description here
Because Queries counts more statistics, in theory, Queries count is always greater than or equal to Questions count.

Queries will count more stored procedure statements and prepared statements than Questions.

2. TPS (the number of transactions processed per second)

TPS refers to the number of transactions processed per second, excluding select statements.

TPS = (Com_commit + Com_rollback) / seconds

show global status like 'Com_commit';

show global status like 'Com_rollback';

Field description:

Com_commit: number of transaction commits
Com_rollback: number of transaction rollbacks

For databases with very frequent rollback operations, it may indicate a problem with application writing.

insert image description here
insert image description here

Calculation idea:

First get the value commit_rollback1 of ** (Com_commit + Com_rollback) **, and then get the value commit_rollback2 of ** (Com_commit + Com_rollback) ** after a period of time.
TPS = (commit_rollback2-commit_rollback1)/(uptime_since_flush_status2-uptime_since_flush_status1);

3. Key Buffer hit rate

The key buffer hit rate represents the index cache hit rate of the Myisam table, and the hit rate directly affects the read and write performance of the Myisam table.
If the hit rate is too low, it means that there is a problem in reading and writing the myisam type table.
The key buffer hit rate actually includes two types, the read hit rate and the write hit rate, and the calculation formula is as follows:

key_buffer_read_hits = (1-key_reads / key_read_requests) * 100%
key_buffer_write_hits = (1-key_writes / key_write_requests) * 100%

show global status like 'key%';

insert image description here

4. InnoDB Buffer hit rate

Innodb buffer refers to innodb_buffer_pool, which is the internal space used to cache innodb type tables and indexes.
If the hit rate is too low, it indicates that there is a problem in reading and writing of InnoDB tables.
Calculation formula:
innodb_buffer_read_hits = (1 - innodb_buffer_pool_reads / innodb_buffer_pool_read_requests) * 100%

show global  status like 'innodb_buffer_pool_read%';

insert image description here

5. Query cache hit rate

query cache is the query cache of mysql. If it is enabled in the my.cnf configuration file, the result of the query statement can be cached.
For some platforms with a small number of users or one-time statistics, it is recommended to turn off the query cache.
If the query cache is enabled, it is also necessary to monitor the query cache hit rate, which can tell us whether the database is using the query cache correctly.
Calculation formula:
query_cache_hits =(Qcache_hits/(Qcache_hits+Qcache_inserts))* 100%;

show global status like 'Qcache%';

In mysql5, the function of query-cache is introduced, and query-cache is turned off by default.
Mysql recommends that we use third-party caching technology instead of using mysql's own query-cache to cache data. In mysql8, query-cache is also removed

6. table_cache (table_open_cache) hit rate

table_cache is a very important MySQL performance parameter, table_cache is mainly used to set the number of table caches. It is called table_open_cache in versions after 5.1.3 . Since each client connection will access at least one table, the value of this parameter is related to max_connections. When mysql accesses a table, if there is still space in the table cache, the table will be opened and the data will be put into it, so that the contents of the table can be accessed faster the next time the table is accessed. By checking the status values ​​open_tables and opened_tables at the peak time, you can determine whether to increase the table_cache value. It should be noted that if the table_cache setting is too high, it may cause insufficient file descriptors, resulting in unstable performance or connection failure.



Suggestion: The ratio between the state quantity open_tables and opened_tables should be greater than 80%

If the ratio between open_tables and opened_tables is too low, it means that the table cache setting is too small.

show global status like 'open%_tables';

insert image description here
View the value of table_open_cache:

show global variables like 'table_open_cache';

Modify the value of table_open_cache as follows:

  • Method 1: You can modify the value of the parameter table_open_cache in my.ini or my.cnf . To restart the mysql service.
  • Method 2: In SET GLOBAL table_open_cache= 2000; this method does not need to restart the mysql service. It will fail after restarting MySQL.
6. Thread cache hit rate

In mysql, in order to improve the process of client connection as much as possible, a thread cache pool is implemented, and idle connection threads are stored in it instead of being destroyed after the request is completed. When there is a new connection request, mysql first checks the thread Whether the cache stores idle connection threads, if they exist, take them out and use them directly, if there are no idle connection threads, create new threads.
The thread cache hit rate can directly reflect whether the setting of the system parameter thread_cache_size is reasonable. A reasonable read_cache_size parameter can save a lot of resources that need to be consumed when creating new connections. Normally, a thread cache hit rate above 90% is reasonable.

Calculation formula:

thread_cache_hits = (1- threads_created/connections) * 100 %;

show global status like 'Thread%';
show global status like 'Connections';

insert image description here
insert image description here
Parameter Description:

  • threads_created: Indicates the number of created threads. Obviously, if threads_created is too large, it indicates that the mysql server has been creating threads. This is also a relatively resource-intensive process, indicating that the server is not healthy
  • Connections: The number of connections to the MySQL server.
optimization

If the thread cache hit rate is lower than 90%
, check the size of thread_cache_size:

show global variables like 'thread_cache_size';

insert image description here
Appropriately increase the thread_cache_size value in the configuration file, and directly add thread_cache_size=64 to the my.cnf file.
Restart the Mysql service for the configuration to take effect.

Or execute the following command, this way does not need to restart the mysql service. It will fail after restarting MySQL.

set global thread_cache_size=30;
7, tmp table related status analysis

tmp table is mainly used to monitor whether mysql uses too much temporary tables, and whether there are temporary tables that are too large and have to be swapped out from memory to disk files

show global status like 'created_tmp%';

insert image description here

Parameter Description:

Created_tmp_disk_tables: The number of times the disk had to be used because the temporary table was too large to be done in memory.
If there are too many create_tmp_tables, there may be too many sorted sentences, or the connection method may not be very optimized.
And if the create_tmp_dis_table/create_tmp_tables ratio is too high, such as more than 10%, you need to consider whether the tmp_table_size parameter needs to be adjusted larger.
It is recommended that tmp_table_size and max_heap_table_size need to be set to the same size. Add/adjust the value of tmp_table_size
directly in the my.cnf file . Restart the Mysql service for the configuration to take effect.

Or execute the following command, this way does not need to restart the mysql service. It will fail after restarting MySQL.

set global tmp_table_size=自定义;

View the effective configuration:

show global variables like 'tmp_table_size';
show global variables like 'max_heap_table_size';

insert image description here
insert image description here

8、binlog cache

If you enable the binlog log function, you need to consider the binlog cache issue. The binlog is not written to the binlog as soon as there is data, but first written to the binlog cache and then written to the binlog.
Binlog_cache_disk_use is the amount of hard disk used by binlog, and Binlog_cache_use is the amount used by binlog. If Binlog_cache_disk_use is greater than 0, it means that binlog_cache is not enough, and binlog_cache_size needs to be increased.

show status like 'binlog_cache%';

insert image description here
Add/adjust the value of binlog_cache_size directly in the my.cnf file . Restart the Mysql service for the configuration to take effect.

Or execute the following command, this way does not need to restart the mysql service. It will fail after restarting MySQL.

set global binlog_cache_size=自定义;

View the effective configuration:

show global variables like 'binlog_cache_size';

insert image description here

9、innodb_log_waits
show status like 'innodb_log_waits';

If the Innodb_log_waits value is not equal to 0, it indicates that the innodb log buffer is waiting because of insufficient space. The value of innodb_log_buffer_size needs to be increased, and an appropriate increase will not cause insufficient memory. Add/adjust the value of innodb_log_buffer_size
insert image description here
directly in the my.cnf file . Restart the Mysql service for the configuration to take effect.

Or execute the following command, this way does not need to restart the mysql service. It will fail after restarting MySQL.

set global innodb_log_buffer_size=自定义;

View the effective configuration:

show global variables like 'innodb_log_buffer_size';

insert image description here

10. Lock status

Mysql locks include table locks and row locks. Myisam’s minimum lock is table locks, and InnoDB’s minimum locks are row locks. You can use the following commands to obtain the number of locks, the number of times other threads wait for locks, and the lock waiting time information.

 show status like '%lock%';

If the ratio of Table_locks_waited/Table_locks_immediate is relatively large, it means that the blocking caused by table locks is relatively serious. It may be necessary to adjust the Query statement, or change the storage engine, or adjust the business logic.

However, if Innodb_row_lock_waits is large, it means that Innodb's row lock is also serious and affects the normal processing of other threads.
The serious cause of Innodb row locks may be that the index used by the Query statement is not reasonable enough (Innodb row locks are locked based on indexes), resulting in excessive gap locks.

3. Summary

This article describes what SHOW STATUS is and how to use SHOW STATUS. Shared 10 commonly used mysql status queries:

  • QPS (Query amount per second)
  • TPS (transactions per second)
  • key buffer hit rate
  • InnoDB Buffer hit rate
  • query cache hit rate
  • table_cache (table_open_cache) hit rate
  • tmp table related status analysis
  • binlog cache
  • innodb_log_waits
  • lock state

So, um, the answer to this question is optional. . Tell Brother Xu Zhu loudly in the comment area.

4. Reference


MySQL Advanced Skill Tree > Query Optimization > How SHOW STATUS calculates MySQL’s QPS/TPS
MySQL debugging – show status

5. Fan Benefits

insert image description here
insert image description here

Welfare 0

Brother Xu Zhu directly sent a good book, the one with the highest number of likes~

  • The one with the highest number of likes will get a free book: the one with the highest number of likes on this article will get a physical book "Java Core Technology" (volume 1 or volume 2 of your choice)

  • Statistics deadline: 2023/08/10 19:00:00

Benefit 1

Brother Xuzhu will send 2 good books directly, and participate in the praise and give away books~

  • Participate in book delivery for praise: 2 lucky readers will be randomly selected, and each of the 2 lucky readers will get a physical book "Java Core Technology" (choose one from volume 1 or volume 2)

  • Statistics deadline: 2023/08/10 19:00:00

Benefit 2

Come to Xu Zhu’s circle of friends who send books to like, like and send books~
Xu Zhu opens up the friend position: Portal –”

  • For the 8th and 88th friends who like it, 2 lucky readers will each get a physical book "Java Core Technology" (choose one from Volume 1 or Volume 2)
  • Statistics deadline: 2023/08/10 19:00:00

Please send a private message to Brother Xu Zhu's delivery address within 7 days after the deadline for the statistics of the winners, the expired rewards will be voided!

I'm Brother Xuzhu, see you tomorrow~

Guess you like

Origin blog.csdn.net/shi_hong_fei_hei/article/details/132184603