当前位置:网站首页>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 .
边栏推荐
- jenkins 凭证管理
- Log4j2
- 浅谈sklearn中的数据预处理
- PgSQL string is converted to array and associated with other tables, which are displayed in the original order after matching and splicing
- Codeforces 771 div2 B (no one FST, refers to himself)
- YYGH-BUG-04
- 多文件程序X32dbg动态调试
- CDH存在隐患 : 该角色的进程使用的交换内存为xx兆字节。警告阈值:200字节
- (C language) octal conversion decimal
- Leetcode739 daily temperature
猜你喜欢
File operation (detailed!)
Applet link generation
自然语言处理系列(一)——RNN基础
CDH存在隐患 : 该角色的进程使用的交换内存为xx兆字节。警告阈值:200字节
HOW TO ADD P-VALUES ONTO A GROUPED GGPLOT USING THE GGPUBR R PACKAGE
Thesis translation: 2022_ PACDNN: A phase-aware composite deep neural network for speech enhancement
WSL 2 will not be installed yet? It's enough to read this article
Take you ten days to easily finish the finale of go micro services (distributed transactions)
Deep understanding of NN in pytorch Embedding
Depth filter of SvO2 series
随机推荐
Pytorch builds LSTM to realize clothing classification (fashionmnist)
Dynamic debugging of multi file program x32dbg
ESP32 Arduino 引入LVGL 碰到的一些问题
Some problems encountered in introducing lvgl into esp32 Arduino
File operation (detailed!)
堆(优先级队列)
测试左移和右移
Yygh-9-make an appointment to place an order
(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?
post请求体内容无法重复获取
to_ Bytes and from_ Bytes simple example
Easyexcel and Lombok annotations and commonly used swagger annotations
使用Sqoop把ADS层数据导出到MySQL
YYGH-9-预约下单
PgSQL string is converted to array and associated with other tables, which are displayed in the original order after matching and splicing
Enter the top six! Boyun's sales ranking in China's cloud management software market continues to rise
Larvel modify table fields
GGHIGHLIGHT: EASY WAY TO HIGHLIGHT A GGPLOT IN R
K-Means Clustering Visualization in R: Step By Step Guide
Codeforces 771-div2 C (trouble, permutation is not very good)