当前位置:网站首页>Sometimes only one line of statements are queried, and the execution is slow
Sometimes only one line of statements are queried, and the execution is slow
2022-07-02 19:54:00 【Orpheus group】
List of articles
Sometimes only one line of statements is queried , Slow implementation
1 Query does not return... For a long time
If there is one SQL
mysql> select * from t where id=1;The query result will not be returned for a long time

It's usually a watch t Locked in , Execute at this time show processlist
1.1 etc. MDL lock
Use show processlist Command view Waiting for table metadata lock Schematic diagram .

- This state indicates , Now there is a thread working on the table t To request or hold MDL Write lock , hold select Blocked
- Inquire about sys.schema_table_lock_waits This table , Find the blocked process id, use kill Command will disconnect

1.2 etc. flush
perform SQL
select * from information_schema.processlist where id=1;Find out that the status of this thread is Waiting for table flush, You can imagine why .

This state represents , Now another thread is about to heap the table t do flush operation ,MySQL There are tables piled inside flush Usage of operation , There are generally the following two :
flush tables t with read lock; flush tables with read lock; //flush tables Yes, it will buffer pool Data is brushed back to diskthese two items. flush sentence , If you specify a table t Words , On behalf of just closing the watch t; If no specific indication is specified , It means off MySQL All the open watches in .
- Normally, this statement executes very quickly , Unless it is also blocked .
- therefore , appear Waiting for table flush The possible state is : There is one flush tables The command is blocked by another statement , Then it blocked our select sentence .
Repeat step

stay session A in , Call once per line sleep(1), In this way, this statement is executed by default 10 Thousands of seconds , In the meantime, watch t Has been session A open .
show processlist

take sessionA Threads kill 13
KILL [CONNECTION | QUERY] processlist_id kill query 13 // The connection is SQL Execution will continue kill 13 // With or without , Will kill the thread .
1.3 Wait for the line lock
SQL
select * from t where id=1 lock in share mode; select k from t where id=1 lock in share mode; select k from t where id=1 for update; select If the statement is locked , It's the current reading ; Read locks are added respectively (S lock , Shared lock ) And write lock (X lock , Exclusive lock ).because id = 1 This record is read locked , If there is already a transaction holding a read lock on this row of records , our select The statement will be locked .

Query statements are locked :

So it is session A Start transaction , After occupying the write lock , No submission yet , Led to session B Locked up .
Query occupancy
select * from t sys.innodb_lock_waits where locked_table='`test`.`t`'\GAnd then query kill fall , It needs to be done directly kill id , Disconnect the connection directly , The transaction rollback will release the lock .
2 Slow query
select * from t where c=50000 limit 1;Due to fields c There is no index on , So this sentence goes straight id primary key , Perform a full table scan .
- There are many scanning lines , Slow execution . Bad queries are not necessarily slow queries
diselect * from t where id=1; select * from t where id=1 lock in share mode;The first sentence is slow query :

The second sentence is slow query :
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-b9UYTdjJ-1656643464196)(https://cdn.jsdelivr.net/gh/Cltlient/PiGoCDN/img/20220113171449.png)]
Execution results :

Repeat step :

- session A First use start transaction with consistent snapshot The command starts a transaction , after session B Just started to execute update sentence .、
- session B Executive ten thousand 100 Ten thousand times update after ,id = 1 The status of this line is as follows :[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-P5g5ZU38-1656643464197)(https://cdn.jsdelivr.net/gh/Cltlient/PiGoCDN/img/20220113171702.png)]
- session B Update complete 100 Ten thousand times , Generated 100 10000 rollback logs (undo log).
- belt lock in share mode Of SQL sentence , It's the current reading , So you will read directly 100001 This result , So it's very fast ;
- select * from where id =1 This statement , It's consistent reading , So we need to 100001 Start , One time execution undolog, perform 100 Ten thousand times , take 1 This demerit is returned .
3 Summary :
One line short : Possible examples of locking and slow execution , Mainly watch lock 、 The concepts of row locking and consistent reading .
problem :
For example, when locking reading , Use this statement ,select * from t where id=1 lock in share mode. because id There's an index on , So you can directly locate id=1 This business , Therefore, read lock is only added to this line .
But if it's the following SQL sentence ,
begin; select * from t where c=5 for update; commit;How to lock this statement ? When is the lock added released ?
边栏推荐
- 浏览器缓存机制概述
- 中缀表达式转换为后缀表达式(C语言代码+详解)
- KS004 基于SSH通讯录系统设计与实现
- Implementation of online shopping mall system based on SSM
- AcWing 181. 回转游戏 题解(搜索—IDA*搜索)
- 开始练习书法
- AcWing 1125. Cattle travel problem solution (shortest path, diameter)
- AcWing 1135. Happy New Year (shortest path + search)
- Registration opportunity of autowiredannotationbeanpostprocessor under annotation development mode
- Think about the huge changes caused by variables
猜你喜欢

AcWing 1126. 最小花费 题解(最短路—dijkstra)

SQLite 3.39.0 发布,支持右外连接和全外连接

AcWing 342. Road and route problem solving (shortest path, topological sorting)

JASMINER X4 1U深度拆解,揭开高效省电背后的秘密

AcWing 340. Solution to communication line problem (binary + double ended queue BFS for the shortest circuit)

MySQL function
In depth understanding of modern web browsers (I)

勵志!大凉山小夥全獎直博!論文致謝看哭網友

Overview of browser caching mechanism

C language linked list -- to be added
随机推荐
API文档工具knife4j使用详解
KT148A语音芯片ic的开发常见问题以及描述
Kt148a voice chip IC user end self replacement voice method, upper computer
AcWing 1126. Minimum cost solution (shortest path Dijkstra)
AcWing 1131. 拯救大兵瑞恩 题解(最短路)
八年测开经验,面试28K公司后,吐血整理出高频面试题和答案
AcWing 1126. 最小花费 题解(最短路—dijkstra)
Istio部署:快速上手微服务,
After writing 100000 lines of code, I sent a long article roast rust
Self-Improvement! Daliangshan boys all award Zhibo! Thank you for your paper
rxjs Observable 自定义 Operator 的开发技巧
Py之interpret:interpret的简介、安装、案例应用之详细攻略
Solution: vs2017 cannot open the source file stdio h main. H header document [easy to understand]
Understanding and function of polymorphism
Educational codeforces round 129 (rated for Div. 2) supplementary problem solution
From 20s to 500ms, I used these three methods
中缀表达式转换为后缀表达式(C语言代码+详解)
Set up sentinel mode. Reids and redis leave the sentinel cluster from the node
JASMINER X4 1U深度拆解,揭开高效省电背后的秘密
Think about the huge changes caused by variables