当前位置:网站首页>[MySQL database] index and transaction (often used in interview)
[MySQL database] index and transaction (often used in interview)
2022-07-28 03:55:00 【Bit fly】
Indexes and transactions
- Indexes
- Business
- 1. What is business
- 2. Application scenarios of transactions
- 3. How to guarantee transaction atomicity
- 4. Use of transactions
- 5. The four characteristics of business :
- 6. Graphic dirty reading 、 It can't be read repeatedly 、 Fantasy reading
- 7. Isolation of transactions
- 8. MySQL Isolation level of transactions in
Indexes
1. What is index
The index is equivalent to the catalogue of the book , The main function is Improve search efficiency .
2. How to improve search efficiency
When searching from the database , For example, according to the conditions id=3 lookup : You can traverse the table to query , But it's less efficient . How to improve efficiency ?
This requires finding ways to avoid traversal , The characteristics of some records can be represented by some special data structures , Use these features to reduce the number of comparisons , Speed up the comparison .
When the search efficiency is improved , It will also pay some price :
It's equivalent to adding some paper to the book , namely Adding indexes will consume a certain amount of storage space , More data , The more space is consumed
The catalogue of the book identifies , Every time the content of the book is adjusted later , May affect the accuracy of the directory , You need to readjust the directory , Empathy , The index of the database is the same , When adding, deleting, checking and modifying , It is often necessary to adjust the index structure synchronously
3. The advantages and disadvantages of indexing
Brought by index benefits : Improve the search speed
The index brings Disadvantage : Takes up more space , It slows down the speed of adding, deleting, checking and modifying
Be careful :
Although the disadvantages of index seem to be more , But index is still a very common thing , Because in daily use , Search operations are often more frequent .
4. Some basic operations of index
- Look at the index :show index from Table name
Some constraints are automatically indexed , such as :primary ,unique - Create an index for the specified column
create index Index name on Table name ( Name );
for example :create index class_index on student(class);-- to class This column is indexed
Be careful : Creating an index is a very inefficient thing , Especially when there are a lot of data in the current table
therefore , When you target online databases , If the table has no index , Don't rush to add an index … - Delete index
drop index Index name on student Table name
for example :drop index class_index on student;
Be careful : Deleting an index is the same as creating an index , They are all inefficient operations , It's also easy to hang up the database
therefore , When creating tables , You should plan the index .
5. Data structure behind index ( a key )
To make the search faster , The most important thing is to reduce access disk IO The number of times , Because of entering disk IO More time than Compare It takes much longer .
therefore , use B+ Trees Structure , Here's why :
- Use B+ When searching the tree , Access to the IO The number of times is relatively small
- be-all Queries will eventually fall on leaf nodes , Every time I look up IO The times are almost the same , The query speed is stable
- After the leaf node is linked with a linked list , very Suitable for range finding
- All data storage ( load ) They are all placed on leaf nodes , Non leaf nodes are only saved key that will do , Therefore, non leaf nodes occupy less space as a whole , It can even be cached in memory , This makes Access disk IO The number of times is almost gone , Greatly speed up the query speed .

The reason why binary search trees and red black trees are not used is : The height of the tree is too high , Lead to IO More visits .
Reasons for not using hash tables : Cannot support range lookup .
Business
1. What is business
Transaction is a function born to package several independent operations into a whole .
Transactions are atomic : Execution time , Or it can be implemented as a whole , Or none at all , That is, inseparable .
2. Application scenarios of transactions
such as :A——>B Transfer accounts 500
A Balance of -500
update Account form set balance = balance-500 where name = "A";
B Balance of +500
update Account form set balance = balance+500 where name = "B";
When performing this transfer operation , There may be a problem : When A Turn around 500 when ,B I haven't received , But the machine broke down .
Obviously, this state is unscientific , But the use of The atomicity of transactions Can solve this problem
3. How to guarantee transaction atomicity
In execution to the second SQL Before , It is impossible to predict that this implementation will fail ( Suppose that the second execution will fail )
therefore , When execution fails , There are databases that perform some “ Restore ” operation , To eliminate the front SQL The impact . This reductive operation , be called “ Roll back ”.
So it looks : When execution fails , None of them .
4. Use of transactions
- Open transaction :start transaction;
- Execute more than one SQL
- Roll back or commit :rollback ,commit
explain :rollback It's all failure ,commit It's all success
5. The four characteristics of business :
1) Atomicity :
2) Uniformity : Before and after transaction execution , The data in the database must be legal . for example : After the transfer , You cannot have a negative account
3) persistence : Once the transaction is committed , It is persistent and stored , The data is stored in the hard disk .
4) Isolation, : It's important , Let's introduce it separately
6. Graphic dirty reading 、 It can't be read repeatedly 、 Fantasy reading

Deal with dirty reading : In the process of writing , Others can't read ( Locked state ) After the modification , Others can read ( Unlock )
Processing non repeatable : Lock the reader , I can't write when I read , It solves the problem of non repeatable reading .
Deal with unreal reading : Serialization completes the transaction , namely : Write ——> read ——> Write ——> read
7. Isolation of transactions
Isolation describes , When transactions are concurrent , What happened . When multiple transactions are executed concurrently , In particular, many transactions try to modify / When reading the same data , It's easy to have some problems , Transaction isolation can solve this problem .
Between transactions concurrency and Isolation, You can't have both :
When concurrency is high , Poor isolation ; When concurrency is low , High isolation . The balance between the two depends on the needs of the characteristics of the transaction .
The higher the concurrency , The faster transactions are executed ; The higher the isolation , The result of executing transactions is more accurate .
8. MySQL Isolation level of transactions in
- read uncommitted: Allow reading of data submitted for , The highest degree of concurrency , Minimum isolation , Will introduce dirty reading + It can't be read repeatedly + The problem of unreal reading .
- read ncommitted: Only the submitted data is allowed to be read , It is equivalent to locking the write , The degree of concurrency is reduced , The degree of isolation has increased a little , Solved dirty reading , Will introduce non repeatable + The problem of unreal reading
- repeatable read: It's equivalent to locking both read and write , The degree of concurrency is reduced again , Isolation has increased again , Solve the problem of dirty reading and non repeatable reading , Will introduce unreal reading
- serialization: Serialization ( After one execution , To execute the next ), The highest degree of isolation , Solved dirty reading + It can't be read repeatedly + The problem of unreal reading .
When the isolation level needs to be adjusted : It can be modified by my.ini This configuration file , To set the current isolation level , According to the actual needs , Adjust the isolation level .
边栏推荐
- 高等数学(第七版)同济大学 习题3-4 个人解答(前8题)
- Leetcode 0140. word splitting II
- In the official online CV2 document, check the optional values of OpenCV specific parameters
- 超好用的 PC 端长截图工具
- [prototype and prototype chain] get to know prototype and prototype chain~
- Common weak network testing tools
- [P4] solve the conflict between local file modification and library file
- 【LeetCode】34、在排序数组中查找元素的第一个和最后一个位置
- How to solve MySQL deep paging problem
- C language: realize the exchange of two numbers without creating temporary variables
猜你喜欢

超好用的 PC 端长截图工具

Differences among BRD, MRD and PRD

Tungsten Fabric SDN — BGP as a Service

xml文件使用及解析
![[paper notes] mobile robot autonomous navigation experimental platform based on deep learning](/img/6a/7f0c2b2a53332636f3172bc3b0b74d.png)
[paper notes] mobile robot autonomous navigation experimental platform based on deep learning

A 404 page source code imitating win10 blue screen

Super easy to use PC end long screenshot tool

Read Plato farm's eplato and the reason for its high premium

数据挖掘-02

8000 word explanation of OBSA principle and application practice
随机推荐
Dynamic programming - 474. One and zero
Developing rc522 module based on c8t6 chip to realize breathing lamp
Crowdfunding platform system based on JSP & Servlet
Differences among BRD, MRD and PRD
Common interface testing tools
常用的接口测试工具
Web Security Foundation - Command Execution Vulnerability
【无标题】
In the official online CV2 document, check the optional values of OpenCV specific parameters
[leetcode] 34. Find the first and last positions of elements in the sorted array
WordPress简约mkBlog博客主题模板v2.1
ES6 from entry to mastery 07: Deconstruction assignment
[paper notes] mobile robot autonomous navigation experimental platform based on deep learning
[openvx] VX for basic use of objects_ lut
Capacity expansion and reduction of RBD block storage device (VI)
MySQL是怎么保证高可用的
巧用栈回溯,帮你快速定位问题
Interface automation test, complete introduction
Leetcode 0140. word splitting II
LightPicture – 精致图床系统