当前位置:网站首页>[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 .
边栏推荐
- Iterator function operation of iterator learning
- Skillfully use stack backtracking to help you quickly locate problems
- Notes on writing questions in sword finger offer
- In December, the PMP Exam adopted the new syllabus for the first time. How to learn?
- Prefix-Tuning: Optimizing Continuous Prompts for Generation
- cookie与Session
- Detailed explanation of pointer written test questions (C language)
- Crowdfunding platform system based on JSP & Servlet
- Day08 redis的基础知识
- Summary and interpretation of CONDA virtual environment
猜你喜欢

Weekly recommended short video: how to correctly understand the word "lean"?

高等数学(第七版)同济大学 习题3-5 个人解答

Iterator function operation of iterator learning

Summary of static blog building tools

Data mining-01

Advanced Mathematics (Seventh Edition) Tongji University exercises 3-5 personal solutions

Qt:qmessagebox message box, custom signal and slot

Notes on writing questions in sword finger offer

Basic knowledge of day08 redis

In December, the PMP Exam adopted the new syllabus for the first time. How to learn?
随机推荐
面试必备杀技:SQL查询专项训练!
一个仿win10蓝屏的404页面源码
Selenium -- Web automated testing tool
高等数学(第七版)同济大学 习题3-4 个人解答(后8题)
Mysql基础篇(创建、管理、增删改表)
Qt:qmessagebox message box, custom signal and slot
Day08 redis的基础知识
[openvx] VX for basic use of objects_ convolution
C语言力扣第45题之跳跃游戏 II。遍历跳跃
Detailed explanation of pointer written test questions (C language)
[wrong question]mocha and railgun
LeetCode_409_最长回文串
[错题]Concatenation
Interface automation test, complete introduction
[P4] check the differences between the two historical versions of the library file
Monotonous stack -- 42. Receiving rain -- a difficult problem that big factories must know
递归和非递归分别实现求第n个斐波那契数
Ch340 RTS DTR pin programming drives OLED
Istio's Traffic Management API
【无标题】