当前位置:网站首页>[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
边栏推荐
- UE4 source code reading_ Bone model and animation system_ Animation compression
- 【更新中】微信小程序学习笔记_3
- go 解析身份证
- Scite change background color
- Markdown directory generation
- Redis的数据结构
- [linear table] basic operation of bidirectional linked list specify node exchange
- Talking about: is the HashSet set ordered or disordered /hashset set unique, why can we store elements with the same content
- MySQL 8
- 796 · 开锁
猜你喜欢

Scite change background color

Unity editor expansion - draw lines

Graphics_ Games101/202 learning notes

Get to know unity2 for the first time

Three characteristics

php-fpm软件的安装+openresty高速缓存搭建

Gradle's method of dynamically modifying APK package name

Redis data structure

Student educational administration management system of C # curriculum design

Jupyter remote server configuration and server startup
随机推荐
数据分析练习题
Mall management system of database application technology course design
【音视频】ijkplayer错误码
Location of package cache downloaded by unity packagemanager
Map的实现类的顺序性
Markdown learning
MySQL 8
Golang中删除字符串的最后一个字符
Redis的数据结构
Unity interactive water ripple post-treatment
100 GIS practical application cases (78) - Multi compliance database design and data warehousing
UE4 source code reading_ Bone model and animation system_ Animation node
redis集群系列四
Data analysis exercises
单调栈-84. 柱状图中最大的矩形
P1596 [USACO10OCT]Lake Counting S
Unity learning notes
[K & R] Chinese Second Edition personal questions Chapter1
GIS实战应用案例100篇(七十八)-多规合一数据库设计及数据入库
796 · unlock