当前位置:网站首页>[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
边栏推荐
- Golang 时间格式整理
- Student educational administration management system of C # curriculum design
- Mall management system of database application technology course design
- Message pack in C deserializes array objects
- Unity editor expansion - scrolling list
- LinkedList set
- Unity Editor Extension - event handling
- 【Rust 笔记】07-结构体
- 【Rust 笔记】09-特型与泛型
- 数据分析练习题
猜你喜欢
Mall management system of database application technology course design
基于SSM的校园失物招领平台,源码,数据库脚本,项目导入运行视频教程,论文撰写教程
Graphics_ Learnopongl learning notes
matlab神经网络所有传递函数(激活函数)公式详解
Unity change default editor
UE4 source code reading_ Bone model and animation system_ Animation compression
【更新中】微信小程序学习笔记_3
animation
About the problem that the editor and the white screen of the login interface cannot be found after the location of unityhub is changed
Base64编码简介
随机推荐
Huawei interview summary during the epidemic
UE4 source code reading_ Bone model and animation system_ Animation node
Golang string segmentation, substitution and interception
【Rust 笔记】10-操作符重载
How to establish rectangular coordinate system in space
Unity editor expansion - draw lines
单调栈-84. 柱状图中最大的矩形
Golang 时间格式整理
Conversion between golang JSON format and structure
What is BFC?
Data analysis exercises
One dimensional array two dimensional array (sort Max insert sort)
Dotween plug-in
Minimap plug-in
Some understandings of 3dfiles
UE4 source code reading_ Bone model and animation system_ Animation compression
Visual Studio (VS) shortcut keys
Collection interface
Graphics_ Games101/202 learning notes
Markdown directory generation