当前位置:网站首页>[MySQL] MySQL Performance Optimization Practice: introduction of database lock and index search principle
[MySQL] MySQL Performance Optimization Practice: introduction of database lock and index search principle
2022-07-03 08:32:00 【keeper42】
Row-level locks & Table lock
What is a row level lock ? What is a table level lock ? Students who have learned the program must have a certain understanding of the concept of lock , As the name suggests, one is to lock a line , One is to lock a whole watch , Let's look at the following example :
Typical cases
Existing table T_A The structure and data are as follows


Start a transaction (session_1) And execute the following statement

Business one
stay session_1 Without submitting , Start a new business (session_2) And execute the statement

Business two
As a result, the transaction timed out , The problem is coming. , An error is a lock timeout, indicating that a table level lock is used , But it doesn't mean InnoDB Is it a row lock ? It should be noted that row level locks cannot take effect until indexes are used

We give fields column_2 Add index after , After performing the above operation again, it is found that the two transactions do not interfere with each other, and the execution is successful , In high concurrency scenarios, it can greatly improve the efficiency of the program .
Deadlock
Deadlock is mainly caused by different transactions competing for the same resource .

Specific cases
It is also the above T_A Update rows in the table , Deadlock caused when competing for resources


It can be seen that the deadlock will immediately report an error after it occurs . There is a mistake here , In our development, we often encounter too high concurrency, which leads to a large number of locks in the database that cannot be released in time, which is often described as deadlock by developers , It's not , The real deadlock will report an error at the moment of occurrence .
The query principle of index
In the last article, I made a brief introduction to the use of index , Some students may not be very clear about the difference between the two concepts of primary key and index , Let me briefly introduce how to find records through primary keys and indexes in the database :

Primary key storage structure

Index storage structure
The above figure shows the storage structure of primary key and index respectively , You can see that the corresponding record can be found directly through the primary key search , To search by index, you need to find the primary key of the corresponding record , Then find the corresponding record according to the primary key .
Two conclusions can be drawn from the above analysis :
1、 You have to set the primary key , Because the index ultimately needs to locate specific records through the primary key
2、 The primary key should preferably be numeric , In this way, the storage distribution of primary keys is more uniform and the search efficiency is higher
边栏推荐
- KunlunBase MeetUP 等您来!
- UE4 source code reading_ Bone model and animation system_ Animation process
- How to establish rectangular coordinate system in space
- Compilation error: "not in executable format: file format not recognized"“
- Osganimation library parsing
- animation
- Visual Studio (VS) shortcut keys
- Osgearth north arrow display
- Golang json格式和结构体相互转换
- MySQL 8
猜你喜欢

matlab神經網絡所有傳遞函數(激活函數)公式詳解

Data analysis exercises

How to establish rectangular coordinate system in space

【Rust笔记】02-所有权

Base64 and base64url

了解小程序的笔记 2022/7/3

Vscode, idea, VIM development tool shortcut keys

C#课程设计之学生教务管理系统

Student educational administration management system of C # curriculum design

Unity editor expansion - controls, layouts
随机推荐
单调栈-42. 接雨水
Osgearth topographic shading map drawing
Jupyter remote server configuration and server startup
Cloudcompare learning (1) - cloudcompare compilation and common plug-in implementation
Golang's range
Unity learning notes
About Wireshark's unsuccessful installation of npcap
【Rust 笔记】07-结构体
[K & R] Chinese Second Edition personal questions Chapter1
数据分析练习题
數據庫應用技術課程設計之商城管理系統
【云原生】微服务之Feign的介绍与使用
【Rust 笔记】13-迭代器(上)
go 解析身份证
Intersectionpicker in osgearth
使用base64编码传图片
Talking about: is the HashSet set ordered or disordered /hashset set unique, why can we store elements with the same content
Base64和Base64URL
Solution détaillée de toutes les formules de fonction de transfert (fonction d'activation) du réseau neuronal MATLAB
Unity editor expansion - the design idea of imgui