当前位置:网站首页>MySQL indexes and transactions
MySQL indexes and transactions
2022-07-02 12:09:00 【The dishes are not right】

Catalog
🥬 Common operations of indexing
🥬 The basic characteristics of a transaction
🥬 What is the index
🥬 Index usage scenarios
1、 Large amount of data , And we often make conditional queries on these columns .
2、 The insert operation of the database table , And modify these columns less frequently .
3、 Indexes take up extra disk space .
When the above conditions are met , Consider indexing these fields in the table , To improve query efficiency .
conversely , If the non conditional query column , Or insert it often 、 Modify the operating , Or when there is not enough disk space , Don't think about creating indexes .
🥬 Common operations of indexing
create index Index name on Table name ( Field name );
2、 Look at the index
show index from Table name ;3、 Delete index
drop index Index name on Table name ;🥬 Data structure behind index
The data structure behind the index is B+ Trees .

B+ Tree advantage :
1、 Use B+ When searching the tree, the whole disk IO The number of times is relatively small .
2、 All queries will eventually fall on the leaf node , Disk per query IO The times are the same , The query speed is relatively stable .
3、 After the leaf nodes are connected with a linked list , Very suitable for range finding .
4、 All data storage ( load ) They are all placed on leaf nodes , Only... Are saved in non leaf nodes key value , Therefore, the overall space occupied by non leaf nodes is small , It can even be cached in memory ( Once you can put it all in memory , At this time, the disk IO It's almost gone ).

🥬 What is the business
start transaction;2、 Roll back or commit
rollback/commit;
//rollback It's all failure ,commit It's all success .🥬 The basic characteristics of a transaction
1、 Atomicity
Atomicity means that either both operations are performed , Or none of them , Operations cannot be subdivided .
2、 Uniformity
Consistency means that the data in the database should be reasonable and legal before and after the transaction execution .( For example, the above transfer , The account cannot be negative )
3、 persistence
Persistence means that once a transaction is committed , The data is written to the hard disk , Persistent storage .
4、 Isolation,
Isolation refers to the situation caused by the concurrent execution of transactions .
Concurrent transaction execution refers to the simultaneous execution of multiple transactions , When multiple transactions are executed concurrently , Especially when multiple transactions are trying to modify 、 When reading the same data , It's easy to have problems , Isolation is dealing with these problems .
Possible problems caused by concurrent transaction execution :
1、 Dirty reading problem
Business A Entering some data , It's business B Come and read the transaction A Data being entered , then B Go back and follow A Data input , If business A In the transaction B After reading the data, the data is modified , It's time for business B The data read may be a " Dirty data "( This data is only a temporary result , Not the end result ), This is the dirty reading problem .
How to deal with dirty reading ?
Lock when writing data , etc. A Let's write it completely and revise it before B see , At this time, the isolation between transactions is improved , But at the same time, the concurrency is also reduced .
2、 Unrepeatable read problem
Handle according to the above , wait until A Write it all B Look again , But if A stay B When reading, I modified the code , Now B Let's go back , The data is different again , This data cannot be read repeatedly , What should I do ? At this time, simply lock the reading , stay A When writing data B Cannot read , stay B Reading the data A It can't be modified . At this time, the isolation of transactions is improved , Concurrency is reduced again .
3、 The problem of unreal reading
The above two operations , Lock both reading and writing , Although its isolation is improved , But it reduces efficiency , As a result, it takes longer to read and write , At this time, in the process of writing or reading, transactions can change other tables or other rows of this table ( Although transactions have a series of locks when committing isolation , But the whole database is not locked ), Now B When reading data, you will find that the amount of data has changed , There was only one A.java, Now there are more B.java, This is the unreal reading problem . In general, multiple queries are made during the execution of a transaction , The result set of each query is different ( More or less ), This is also a special non repeatable problem .
How to solve it ?
Transaction serialization execution .( Serialization is the execution of transactions one by one )
From the above, we also know that isolation and concurrency cannot be achieved at the same time , To improve isolation , Concurrency must be reduced .
The above modifications have been made , At this time, the degree of isolation is the highest , The degree of concurrency is the lowest , The most reliable data , And the slowest .
According to the above summary ,MySQL Isolation level of transactions in , The following are provided :
1. read uncommitted: Allow reading uncommitted data , The highest degree of concurrency , Minimum isolation , Will introduce dirty reading 、 It can't be read repeatedly 、 The problem of unreal reading .
2. read committed: Only data after submission is allowed to be read , Equivalent to writing and locking , The degree of concurrency is reduced , The degree of isolation has increased a little , Solved dirty reading , Will introduce non repeatable reading and unreal reading .
3. repeatable read: It's equivalent to locking both read and write , The degree of concurrency is reduced again , Isolation has increased again , It solves dirty reading and non repeatability , Will introduce unreal reading .
4. serializable: Serialization , The lowest degree of concurrency ( Serial execution ), The highest degree of isolation , Solved dirty reading , Non repeatability , The problem of unreal reading , But the slowest execution .
🥬 Summary
Here we mainly know what the index is , Its applicable scenarios , What is the cost and the data structure behind the index , The four basic characteristics of transactions . That's what we have today , If you have any questions, you can leave a message in the comment area .

边栏推荐
- (C language) input a line of characters and count the number of English letters, spaces, numbers and other characters.
- 自然语言处理系列(二)——使用RNN搭建字符级语言模型
- (C语言)3个小代码:1+2+3+···+100=?和判断一个年份是闰年还是平年?和计算圆的周长和面积?
- Natural language processing series (II) -- building character level language model using RNN
- Fabric. JS 3 APIs to set canvas width and height
- YYGH-BUG-05
- 字符串回文hash 模板题 O(1)判字符串是否回文
- Log4j2
- Full link voltage measurement
- SCM power supply
猜你喜欢

xss-labs-master靶场环境搭建与1-6关解题思路

机械臂速成小指南(七):机械臂位姿的描述方法

MSI announced that its motherboard products will cancel all paper accessories

Jenkins用户权限管理

(C语言)输入一行字符,分别统计出其中英文字母、空格、数字和其它字符的个数。

The blink code based on Arduino and esp8266 runs successfully (including error analysis)

H5,为页面添加遮罩层,实现类似于点击右上角在浏览器中打开

ESP32 Arduino 引入LVGL 碰到的一些问题

YYGH-BUG-05

Mish-撼动深度学习ReLU激活函数的新继任者
随机推荐
HOW TO CREATE A BEAUTIFUL INTERACTIVE HEATMAP IN R
PHP 2D and multidimensional arrays are out of order, PHP_ PHP scrambles a simple example of a two-dimensional array and a multi-dimensional array. The shuffle function in PHP can only scramble one-dim
Take you ten days to easily finish the finale of go micro services (distributed transactions)
mysql索引和事务
Depth filter of SvO2 series
还不会安装WSL 2?看这一篇文章就够了
Implementation of address book (file version)
The most understandable f-string tutorial in history, collecting this one is enough
(C language) 3 small Codes: 1+2+3+ · · +100=? And judge whether a year is a leap year or a normal year? And calculate the circumference and area of the circle?
测试左移和右移
PyTorch搭建LSTM实现服装分类(FashionMNIST)
Orb-slam2 data sharing and transmission between different threads
刷题---二叉树--2
Mish shake the new successor of the deep learning relu activation function
CDH6之Sqoop添加数据库驱动
WSL 2 will not be installed yet? It's enough to read this article
YYGH-9-预约下单
CONDA common command summary
Small guide for rapid formation of manipulator (VII): description method of position and posture of manipulator
深入理解P-R曲线、ROC与AUC
