当前位置:网站首页>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 !
↓ ↓ ↓ ↓ ↓
边栏推荐
- Web3 DApp用户体验最佳实践
- GBASE 8s的并行操作问题场景描述
- JS' sort() function
- 多睡觉,能减肥,芝加哥大学最新研究:每天多睡1小时,等于少吃一根炸鸡腿...
- Data view for gbase 8s
- Upgrade PHP to php7 The impact of X (I). The problem of session retention. Keep login
- CTF_ Web: deserialization of learning notes (II) CTF classic test questions from shallow to deep
- PHP encapsulates curl to send get and post request methods, and uses
- Code scanning payment flow chart of Alipay payment function developed by PHP
- 2.0SpingMVC使用RESTful
猜你喜欢

CTF_ Web: Changan cup-2021 old but a little new & asuka

At the age of 30, I began to learn programming by myself. Is it still time for me to have difficulties at home?

Vscode 设置clang-format

SOC验证环境的启动方式

Cnpm: unable to load file c:\users\administrator\appdata\roaming\npm\cnpm PS1 because running scripts is prohibited on this system.
![[esp32 learning path 6 - Flash encryption]](/img/4c/f317ca4823dca50a9bccd285967ab0.png)
[esp32 learning path 6 - Flash encryption]

MySQL concept and operation (III)

【无标题】

Code scanning payment flow chart of Alipay payment function developed by PHP

电脑的dwg文件怎么打开
随机推荐
Web3 DApp用户体验最佳实践
Records of ros2/dds/qos/ topics
CTF_ Web: Learn flask template injection (SSTI) from 0
CTF_ Web: Advanced questions of attack and defense world expert zone WP (15-18)
My IC journey - the growth of senior chip design verification engineers - "Hu" said that IC engineers are perfect and advanced
30岁了开始自学编程,家里比较困难还来得及吗?
Cascading deletion of gbase 8s
CTF_ Web: how to recognize and evaluate a regular expression
Construction scheme of distributed websocket
CTF_ Web: deserialization of learning notes (II) CTF classic test questions from shallow to deep
Why PHP is not safe
Sleep more, you can lose weight. According to the latest research from the University of Chicago, sleeping more than 1 hour a day is equivalent to eating less than one fried chicken leg
Immutable learning road -- farewell to traditional copy
使用文本分析识别一段文本中的主要性别
「 每日一练,快乐水题 」1108. IP 地址无效化
本轮压力测试下,DeFi协议们表现如何?
The solution of wechat applet switchtab unable to take parameters
Trigger for gbase 8s
Cannot import name 'escape' from 'jinja2' [solved successfully]
JS call() and apply()