当前位置:网站首页>Life experience of an update statement
Life experience of an update statement
2022-07-07 05:23:00 【A little dog】
One update Life experience of sentences
Server layer and storage engine layer
One SQL Statement execution on , Generally speaking, it can be divided into :Server Layer and storage engine layer (InnoDB)
1. Server layer

1.1 The connector
Responsible for establishing connections with clients 、 Account password verification 、 Access permissions 、 Maintaining and managing connections .
1.2 analyzer
After passing the verification , The parser will analyze the statement , Judge whether there are grammatical errors, etc .
1.3 Optimizer
Choose the index , Generate execution plan .
1.4 actuator
According to the execution plan generated by the optimizer , Call the storage engine API perform SQL.
2. InnoDB Engine layer


2.1 Transaction execution
Read the data page
Get into InnoDB Behind the engine layer , First of all, we will judge that SQL Whether the data page involved exists in BP(buffer pool) in ;
If it doesn't exist, go through B+Tree Read the data page to the disk , And then load it into BP:
Find the record corresponding to this page by dichotomy
- adopt space id and page no After hashing, put The index page is loaded into the specified buffer pool instance in
- Judge free list Whether there are free pages available ( Innodb_buffer_pool_pages_free、 Innodb_buffer_pool_wait_free ), If not, the dirty pages will be eliminated lru list Of old page
- Put data page copy To free list in , And then load it into lru list Of old District midpoint( Head )
Lock
Try to put an exclusive lock on the corresponding row record , The process is as follows :
- Whether the row lock of the corresponding row record is occupied by other transactions , When occupied, enter the lock and wait ;
- After entering the lock and waiting , At the same time, judge whether it will lead to deadlock due to your participation ;
- After detecting that there is no lock waiting and no deadlock is caused , Row record with exclusive lock ;
Write logic undo log
Write the record before modification to undo in ;
Modify the value of the current row , Fill in the transaction number ;
Use the rollback pointer to point to undo log Line before modification in , Build rollback segment , Used to roll back data and implement MVCC Multiple versions of .
Write redo log buffer
First judge redo log buffer Is it enough for , If not enough, wait , It can be done by Innodb_log_waits It's worth checking ;
Update the field value of the corresponding line record , And record the modification operation to redo log buffer in ;
The corresponding data page is added flush list In the list .
Write binlog cache
The modified information will correspond to event Format to write binlog cache in .
Write change buffer
If this time update The operation involves modifying the secondary index , Then write change buffer page .
2.2 Transaction submission
InnoDB Storage engine transaction submission is divided into prepare、commit Two-phase commit
- redo log prepare
take redo log buffer Refresh to disk file , For crash recovery ; The way to brush the disc is determined by innodb_flush_log_at_trx_commit decision ( unmark commit), The storage engine tier is in prepare state . - binlog write & fsync
Actuator handle binlog cache The whole business and redo log prepare Medium XID event Write to binlog in ;
send out binlog_cache Inside event To slave And wait for ( Asynchronous mode does not wait ) slave ack ;
perform fsync Brush set ( For big business, this step is very time-consuming ), And empty binlog cache;# binlog The way to brush the disc is determined by sync_binlog decision . - redo log commit
commit Stage , Due to the previous transaction redo log already sync It's on disk , So this step is just redo log Inside mark commit, Indicates that the transaction was committed successfully . In semi synchronous mode, if it cannot be received slave ack This step will be in a waiting state . - Transaction submitted successfully , Release the exclusive lock held by the row record .
- Refresh the dirty pages
The database performs dirty page brushing operations according to certain policies .
2.3 Transaction rollback
If the transaction is explicitly rolled back because of an exception or , Is with the aid of undo log To recover the data in :
- about in-place( In situ ) to update , Rollback data to the oldest version ;
- about delete + insert By , Clean up deleted records marked for deletion , At the same time, the inserted clustered index and secondary index records will also be deleted directly .
Please correct me if there is any mistake
边栏推荐
- JHOK-ZBL1漏电继电器
- pytest测试框架——数据驱动
- SQL injection - secondary injection and multi statement injection
- If you‘re running pod install manually, make sure flutter pub get is executed first.
- 漏电继电器JD1-100
- Is PMP really useful?
- Leetcode(417)——太平洋大西洋水流问题
- 阿里云的神龙架构是怎么工作的 | 科普图解
- Where is NPDP product manager certification sacred?
- 【问道】编译原理
猜你喜欢

10 distributed databases that take you to the galaxy

The sooner you understand the four rules of life, the more blessed you will be

实现网页内容可编辑

If you‘re running pod install manually, make sure flutter pub get is executed first.

Phenomenon analysis when Autowired annotation is used for list

Full link voltage test: the dispute between shadow database and shadow table

Safe landing practice of software supply chain under salesforce containerized ISV scenario

带你遨游银河系的 10 种分布式数据库

Weebly mobile website editor mobile browsing New Era
![[opencv] image morphological operation opencv marks the positions of different connected domains](/img/c3/f437bad9432dedbbb14c8a62ba5180.png)
[opencv] image morphological operation opencv marks the positions of different connected domains
随机推荐
漏电继电器JELR-250FG
Longest common subsequence (LCS) (dynamic programming, recursive)
Understand common network i/o models
app clear data源码追踪
高压漏电继电器BLD-20
y58.第三章 Kubernetes从入门到精通 -- 持续集成与部署(三一)
《五》表格
U++4 interface learning notes
实现网页内容可编辑
DOM-节点对象+时间节点 综合案例
最长回文子串(动态规划)
设f(x)=∑x^n/n^2,证明f(x)+f(1-x)+lnxln(1-x)=∑1/n^2
《2》 Label
The execution order of return in JS' try catch finally
DBSync新增对MongoDB、ES的支持
《5》 Table
漏电继电器LLJ-100FS
【最佳网页宽度及其实现】「建议收藏」
batch size设置技巧
vector和类拷贝构造函数