MySQL slow query or sql stuck problem (Waiting for table metadata lock)

View configuration:

//View slow query time
show variables like "long_query_time";默认10s
//View slow query configuration
show status like "%slow_queries%";
//View slow query log path
 show variables like "%slow%";

 

Modify the configuration file

Add the following two sentences to my.ini
log-slow-queries = E:\mysql\mysql_slow_query.log
long_query_time=5
The first sentence is used to define the path of the slow query log (because it is windows, it does not involve permission issues)
The second sentence is used to define how many seconds a query has been checked as a slow query. I define 5 seconds here.
Step 2: Check the status of the slow query.
Execute the following SQL statement to check the status of the mysql slow query.
show variables like '%slow %';
The execution result will print information such as whether to enable slow query, the number of seconds for slow query, and slow query log on the screen.
Step 3: Execute a slow query operation
In fact, it is difficult to execute a meaningful slow query, because when you test it yourself, even if you query a massive table with 200,000 pieces of data, it only takes a few seconds. We can replace it with the following statement:
SELECT SLEEP(10);
Step 4: Check the number of slow queries
Use the following sql statement to check how many slow queries have been executed:
show global status like '%slow%';

 

Configuration of mysql log (requires restart):

 

#Record all sql statements
log=E:/mysqllog/mysql.log

#Record database startup and shutdown information, as well as error messages generated during operation
log-error=E:/mysqllog/myerror.log

# Record all sql statements except select statements to the log, which can be used to restore data files
log-bin=E:/mysqllog/bin

#Record query slow sql statement
log-slow-queries=E:/mysqllog/slow.log  

# slow query time
long_query_time=0.5
 

 

 sql stuck problem: first, through SHOW FULL PROCESSLIST; query which processes are currently operating, and find out the problematic sql
Waiting for table metadata lock :
1. The problem-related query is to perform some DDL operations such as alter table, if the If there are ongoing operations (including reading) on ​​the table, the metadata exclusive lock cannot be obtained and it will block.
2. Uncommitted transactions block DDL, and then block all subsequent operations on the same table. You can't see any operations on TableA through show processlist, but there are actually uncommitted transactions, which can be viewed in information_schema.innodb_trx. Before the transaction is completed, the lock on TableA will not be released, and the alter table will also not be able to obtain the exclusive lock on the metadata.
处理方法:通过 select * from information_schema.innodb_trx\G, 找到未提交事物的sid, 然后 kill 掉,让其回滚。
3、通过show processlist看不到TableA上有任何操作,在information_schema.innodb_trx中也没有任何进行中的事务。这很可能是因为在一个显式的事务中,对TableA进行了一个失败的操作(比如查询了一个不存在的字段),这时事务没有开始,但是失败语句获取到的锁依然有效,没有释放。从performance_schema.events_statements_current表中可以查到失败的语句。
        alter table的语句是很危险的(其实他的危险其实是未提交事物或者长事务导致的),在操作之前最好确认对要操作的表没有任何进行中的操作、没有未提交事务、也没有显式事务中的报错语句。如果有alter table的维护任务,在无人监管的时候运行,最好通过lock_wait_timeout设置好超时时间,避免长时间的metedata锁等待。
Processing method: Find the sid of the uncommitted transaction by selecting * from information_schema.innodb_trx\G, then kill it and let it roll back.
3. You can't see any operations on TableA through show processlist, and there are no transactions in progress in information_schema.innodb_trx. This is probably because in an explicit transaction, a failed operation is performed on TableA (such as querying a non-existing field), and the transaction is not started at this time, but the lock acquired by the failed statement is still valid and not released. . Failed statements can be found in the performance_schema.events_statements_current table.
        The statement of alter table is very dangerous (in fact, its danger is caused by uncommitted transactions or long transactions). Before operating, it is best to confirm that there are no ongoing operations, no uncommitted transactions, or no operations on the table to be operated. Error statement in explicit transaction. If there is an alter table maintenance task that runs unsupervised, it is best to set the timeout time through lock_wait_timeout to avoid long-term metedata lock waiting. Attachment: Introduction to Knowledge Points

The information_schema database comes with MySQL and provides a way to access database metadata. What is metadata? Metadata is data about data, such as database or table names, data types of columns, or access rights, etc. Other terms sometimes used to describe this information include "data dictionary" and "system catalog".
In MySQL, the information_schema is regarded as a database, to be precise, an information database. It holds information about all other databases maintained by the MySQL server. Such as database name, database table, table column data type and access rights. In INFORMATION_SCHEMA, there are several read-only tables. They are actually views, not base tables, so you won't be able to see any files related to them.

可以参考:http://dev.mysql.com/doc/refman/5.5/en/information-schema.html

MySQL中information_schema  简略的介绍了Mysql中元数据信息库的各个表的作用,可以大致了解各个表的作用。这里主要介绍下Innodb事务锁相关的三个表: INNODB_TRX表、INNODB_LOCKS表、INNODB_LOCK_WAITS表。通过查看这三个表可以事务加锁的情况以及事务锁等待的情况,从而可以更简单地监控当前事务并分析可能存在的锁问题,例如分析死锁。  

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326616872&siteId=291194637