当前位置:网站首页>Why does the SQL statement hit the index faster than it does not?
Why does the SQL statement hit the index faster than it does not?
2022-06-25 04:46:00 【Tom bomb architecture】
If you need a full version of the interview document, you can scan the QR code at the bottom of the article to get it for free !
A fan was asked when he was interviewing Gao Kai , Why? SQL Statements hit the index faster than they miss the index ? Although I know the answer , But the moment I was asked , I don't know how to organize language . today , Let me give you an in-depth analysis .
1、 The function of index
Imagine , Now there is a dictionary containing hundreds of thousands of words , Hundreds of pages thick , At the same time, the words inside are arranged in disorder . If you do not use a directory , How can we find the words we need from the dictionary ? without doubt , We can only turn page by page , obviously , This is an anti human work .
What we must think is to look at the catalogue first , then , Find the relevant word or radical , then , Find the corresponding page number and then find the text you want to find , such , The efficiency is greatly improved . As a matter of fact , A directory is an index , The idea of database index that we say comes down in one continuous line with the idea of directory .
The main function of database index is to help us quickly retrieve the data we want , Thus, it is not necessary to do a global scan for each query .
Assuming that no algorithm is used , We need to check 10 One of ten thousand records , In the worst case, you need to traverse 10 Ten thousand times .
But if you use the binary search algorithm , We just need to do log2 20000 Time , That is to say 14.287712 Next time . This means that we only need to sort the sorted values 14 Search , You can use bisection to find the unique value you want , Common index data structures are B Trees and B+ Trees .
Next we , With MySQL Of InnoDB Engine as an example , Analyze how indexes work .
2、 Index execution principle
We know MySQL Of InnoDB The engine uses B+ Tree data structure , When we go to execute SELECT Statement to query data ,InnoDB Need to read data from disk , And this process will involve disk And random disk IO , Let's take a look at this picture :
The system will transfer the logical address of the data to the disk , Disk control circuit translates logical address into physical address according to addressing logic . That is to determine which track the data to be read is on 、 Which sector? . In order to read the data of this sector , You need to put the head on this sector , In order to achieve such a point , The disk will continue to rotate . Rotate the target sector under the head , So that the head can find the corresponding track . There is also a loss of seek time and rotation time . Obviously disk IO The performance overhead of this process is very large , Especially when there is a large amount of data to query .
therefore InnotDB Inside , Simply build an index on the data stored on the disk , Then, the index data and the disk address corresponding to the index column are marked with B+ Tree storage . Look at this picture :
When we need to find the target data , According to the index from B+ Just find the target data in the tree . because B+ There are many subtrees in the tree , therefore , Only a few disks are required IO You can find the target data .
as for B+ The data structure of the tree , There is no analysis here . You can go to my personal home page to see the videos in previous periods .
3、 Disadvantages of index
although , Using indexes can reduce the number of disks IO frequency , Improve query efficiency , however , Indexes can't be built too much . If the indexes of all fields in a table are large , It also leads to performance l falling . Imagine , If an index is as long as a table , Then it will again become an overhead to check . It's like a dictionary with a very detailed catalogue , But its length is as long as all words , At this time, the efficiency of the directory itself is greatly reduced .
Is there any defect in the index ? There must be , Indexing can improve query read performance , And it will reduce write performance . When there is an index , If you change a record , Or insert a new record into the database , It will perform two write operations ( One operation is to write the record itself , Another operation is to update the index ).
therefore , When defining an index , The following must be kept in mind :
1、 Each field in the index table will reduce write performance .
2、 It is recommended to index fields with unique values in the table .
3、 Fields that act as foreign keys in a relational database must be indexed , Because they facilitate complex queries across multiple tables .
4、 Indexes also use disk space , Therefore, be careful when selecting fields to index .
Pay attention to WeChat public number 『 Tom Bomb architecture 』 reply “666” Available 200 page pdf Format document ( Complimentary gift 10 Internal interview materials of Wanzi factory ). Thank you for your support !
You can also scan the QR code below +\/ Get the full version free of charge !
↓ ↓ ↓ ↓ ↓
边栏推荐
- CTF_ Web: how to recognize and evaluate a regular expression
- CTF_ Web: deserialization learning notes (I) classes and objects in PHP
- OOP 向量加减(友元+拷贝构造)
- 为什么SQL语句命中索引比不命中索引要快?
- GBASE 8s的触发器
- Gbase 8s overall architecture
- Data view for gbase 8s
- Huawei Hongmeng development lesson 4
- DMA double buffer mode of stm32
- leetcode1221. Split balance string
猜你喜欢
随机推荐
本轮压力测试下,DeFi协议们表现如何?
GBASE 8s中DELIMIDENT环境变量的使用
Join() in JSZ
Gbase 8s overall architecture
Kotlin Compose 完善toDo项目 Surface 渲染背景 与阴影
[esp32 learning path 6 - Flash encryption]
OOP stack class template (template +ds)
OOP vector addition and subtraction (friend + copy construction)
Gbase 8s index R tree
js的arguments
深度学习——几种学习类型
js中的concat()
电脑的dwg文件怎么打开
Upgrade PHP to php7 X (III) failure of wechat payment callback
机器学习深度学习——向量化
"Daily practice, happy water" 1108 IP address invalidation
Immutable学习之路----告别传统拷贝
固態硬盤開盤數據恢複的方法
Upgrade PHP to php7 The impact of X (2), the obsolescence of mcrypt decryption
《牛客刷verilog》Part I Verilog快速入门