当前位置:网站首页>[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 .
边栏推荐
- [image classification] 2021 MLP mixer nips
- Super easy to use PC end long screenshot tool
- Dynamic programming - 509. Fibonacci number
- How does MySQL ensure high availability
- 【无标题】
- CANopen learning notes
- Selenium -- Web automated testing tool
- test case management tool
- ES6 from getting started to mastering 08: extended object functions
- Appnium--APP自动化测试工具
猜你喜欢

Is there a bonus period for robot engineering

Appnium--APP自动化测试工具

leetcode刷题:动态规划08(分割等和子集)

Basic knowledge of day08 redis

测试用例管理工具

I did these three things before the interview, and the result was actually direct

ES6 from getting started to mastering 08: extended object functions

Fourier series

Developing rc522 module based on c8t6 chip to realize breathing lamp

【无标题】
随机推荐
AI chief architect 12 AICA Baidu OCR vertical large-scale landing practice
Monotonous stack -- 42. Receiving rain -- a difficult problem that big factories must know
Data mining-02
【OPENVX】对象基本使用之vx_pyramid
离职前一定要做好这7件事情,少一件都很麻烦。
What is tor? What is the use of tor browser update?
Error no matching function for call to 'std:: exception:: exception (const char [15])' problem solving
CH340 RTS DTR引脚编程驱动OLED
STC timer is abnormal (how to modify the initial value, the timing time is 100ms)
Is there a bonus period for robot engineering
Ch340 RTS DTR pin programming drives OLED
[image classification] 2021 MLP mixer nips
Summary of static blog building tools
In depth introduction to sap ui5 fileuploader control - why do you need a hidden iframe trial
LeetCode_ 409_ Longest palindrome string
【力扣】1337.矩阵中战斗力最弱的k行
Convert py file to exe executable file
Analysis of static broadcast transmission process
In the official online CV2 document, check the optional values of OpenCV specific parameters
Dynamic planning - 1049. Weight of the last stone II