当前位置:网站首页>What is a database index? Xinhua dictionary to help you

What is a database index? Xinhua dictionary to help you

2022-06-23 21:15:00 Qingyang

There's a hot bar in my pocket

segmentfault.com/a/1190000018153249

picture

* * *

Said in the previous

Database is super important , We should be clear about this , A friend who has studied database must know , Database in use , Even if there is no citation, it can run , But all the data of learning database 、 course , There must be a lot of space for database indexing , All kinds of back-end development work interview I can't get around the index , It can even be said that the database index is from the back-end primary development to the advanced development Dragon Sword , So what role does index play in the server program ?

* * *

What is index ?

Describe... In one sentence : Database index is a kind of The key technology to speed up massive data query . I don't understand this sentence yet ? It doesn't matter , To look down ,20 Minutes later, you can make your own conclusion .

First of all, let's show you a picture

picture

Everyone must be familiar with this book , The first lesson of elementary school must be to teach children how to use this book . So what does this have to do with our database index ? take it easy , Let's turn to the first page .

picture

Pay attention to the line in the upper right corner , In fact, the catalog is the legendary index ! From the front “ One sentence description ” We can know , The purpose of index is to speed up data query . So where is the first place we look up in the dictionary , I believe most people will turn to the Pinyin directory first , After all, now many people forget to write .

The function of database index is the same as pinyin catalog Of , It's the fastest way to lock the location range of the target data . For example, we need to check risk This word , So we found Xx Parts can then be found in order xian The page number of this Pinyin , According to the page numbers before and after, we can know that the word must be stay 519 Page to 523 Between pages Of , The scope will be narrow To only 4 page . It's a lot faster than turning from head to tail , At this time, the first professional term appears —— Full table scan , That is to say, we have to find the end from the beginning .

Sure enough , We are the first 521 Page found what we were looking for “ risk ” word .

picture

Now we know what a database index is : Database index is a technology similar to directory to speed up data query .

* * *

What is a joint index ?

I'm sure you've seen some Database index with multiple fields , such as INDEX idx_test(col_a,col_b). This index with multiple fields is called “ Joint index ”. So what's the effect of building indexes on multiple fields ? Take the Xinhua Dictionary as an example , Let's see what a joint index is .

There is also a catalog in Xinhua Dictionary called “ Radical catalogue ”, As you can see below , To use this directory, we will first find the right parts according to the number of strokes of the radicals , Then we can find the radicals we are looking for in it . For example, if we still want to find risk The position of the word :

picture

After finding the radicals , The page number on the right is not risk The real page number of the word , We also need to find the position of the corresponding radicals in the checklist according to the page number on the right . Find No 93 After the page's typewriter, we can follow the risk word The number of strokes left (7 draw ) stay “6-8 draw ” Find... In this section risk The real page number of the word .

picture

In the process , We used... In order “ Two directories ”, A place called “ Radical catalogue ”, A place called “ Checklists ”. In addition, we can see that the contents of the checklists in the above figure are organized according to the categories of radicals . Together, these two parts are the topic of our discussion in this section —— Joint index . That is, through the value of the first field ( Radicals ) stay First level index Find the corresponding Second level index Location ( Page number of the typewriter ), Then in the second level index according to the value of the second field ( stroke ) Find the location of the qualified data ( The real page number of the danger word ).

* * *

Leftmost prefix matches

As you can see from the previous example of using the radical Directory , If we don't know the radical of a word , It's basically impossible to use this directory . This shows that just by stroke count ( Second field ) There's no way to use the directory .

This leads to A rule of joint index : Fields in union index , Only one field ( stroke ) All fields on the left ( Radicals ) It's all used , To use the index on this field . for example , There is an index INDEX idx_i1(col_a,col_b), If the query condition is wherecol_b =1, Index cannot be used idx_i1.

But if we know the radicals but don't know the number of strokes , I don't know “ Turn the hook horizontally and vertically ” Is it one or two , Then we can still use “ Radical catalogue ” Partial content , Just want to “ Checklists ” All the words in the corresponding radicals can be read once to find the words we are looking for .

This leads to Another rule of Federated indexing : Fields in union index , Even if a certain field ( Radicals ) Other fields on the right ( stroke ) Not being used , Before this field ( contain ) All of the fields of can still use the index normally . for example , There is an index INDEX idx_i2(col_a,col_b,col_c), Then the query conditions wherecol_a =1andcol_b =2 In the field col_a and col_b You can still go on the index .

however , If we're after we've identified the radicals , I don't know whether a word is two paintings or three paintings , In this case, we only need to find the two paintings and three paintings of the corresponding radicals , That is to say, we still use the contents of the checklists . therefore , Index can also be used when range condition query is used .

Last , Let's put it completely The meaning of the leftmost prefix matching principle : For one Joint index , If there is a SQL The query statement needs to execute , Only from the first field on the left of the index to SQL Statement query conditions do not contain fields ( Not included ) Or range condition fields ( contain ) So far, we will use index to speed up .

* * *

What is a clustered index ?

It can be seen from the fact that the catalogue of radicals and Pinyin exist at the same time, but the actual contents of the dictionary are only one , In the database A table can have multiple indexes Of . So what's the difference between different indexes ?

We can see a V The small black squares of the shape , There are a lot of people who write on the side A, B, C, D This corresponds to the alphabet . Because all the words in the dictionary are arranged in pinyin order , Sometimes it's also quick to use the initial to open the corresponding part .

picture

Index like Pinyin catalog , The data will be arranged and organized according to the order in the index , Such an index is called Clustered index , Non clustered indexes are other general indexes . Because the data can only be sorted according to one rule , therefore A table can have at most one clustered index , But there can be multiple nonclustered indexes .

stay MySQL Database InnoDB In the storage engine , A primary key index is a clustered index , All data will be organized according to the primary key index ; And in the MyISAM In the storage engine , There is no clustered index , because MyISAM Data in the storage engine is not stored in index order .

原网站

版权声明
本文为[Qingyang]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/12/202112251755178035.html