当前位置:网站首页>[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 .
边栏推荐
- 【luogu P4590】游园会(DP套DP)
- Developing rc522 module based on c8t6 chip to realize breathing lamp
- Move notice!
- MySQL是怎么保证高可用的
- 【OPENVX】对象基本使用之vx_convolution
- C语言:不创建临时变量实现两数交换
- 【OPENVX】对象基本使用之vx_matrix
- Advanced Mathematics (Seventh Edition) Tongji University exercises 3-4 personal solutions (the last 8 questions)
- C语言力扣第45题之跳跃游戏 II。遍历跳跃
- ES6 from entry to mastery 07: Deconstruction assignment
猜你喜欢

Mysql基础篇(创建、管理、增删改表)

servlet使用

静态博客搭建工具汇总
![[prototype and prototype chain] get to know prototype and prototype chain~](/img/8a/d6362fdd50dc883ff817a997ab9e1e.png)
[prototype and prototype chain] get to know prototype and prototype chain~

How does MySQL ensure high availability

jdbc使用

Collection | 0 basic open source data visualization platform flyfish large screen development guide

How to solve MySQL deep paging problem

WordPress simple mkblog blog theme template v2.1

Dynamic planning - 63. Different paths II
随机推荐
【luogu P4590】游园会(DP套DP)
What is interface testing and its testing process
Leetcode 0140. word splitting II
Interface automation test, complete introduction
From Clickhouse to Snowflake: MPP query layer
Qt:qmessagebox message box, custom signal and slot
Implementation of online rental system based on SSM
静态博客搭建工具汇总
The latest version of pagoda installs the zip extension, and PHP -m does not display the processing method
C language: find the number of 1 in binary stored in memory as an integer
Dynamic programming - 474. One and zero
【力扣】1337.矩阵中战斗力最弱的k行
构建“产业大脑”,以“数字化”提升园区运营管理及服务能力!
Ch340 RTS DTR pin programming drives OLED
Weekly recommended short video: how to correctly understand the word "lean"?
Differences among BRD, MRD and PRD
Advanced Mathematics (Seventh Edition) Tongji University exercises 3-5 personal solutions
[openvx] VX for basic use of objects_ image
Interview essential skills: SQL query special training!
Practical scripts of mangopapa (contents)