当前位置:网站首页>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
* * *
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
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 .
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 .
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 :
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 .
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 .
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 .
边栏推荐
- JS to get the screen size, current web page and browser window
- Install bitgarden open source password manager
- The substring() method in. JS can be used to intercept all characters after the specified string
- 打新债到底是用什么软件比较安全?打新债平台有哪些
- Machine learning related
- JS takes two decimal places
- How to Net project migration to NET Core
- Pathname attribute of link a object
- Global and Chinese market of American football catch gloves 2022-2028: Research Report on technology, participants, trends, market size and share
- Short video intelligent audit software intelligent audit or manual audit
猜你喜欢

How to gradually improve PMO's own ability and management level
Application of JDBC in performance test

How to view the role of PMO in agile organizations?

How PMO uses two dimensions for performance appraisal

What are the main dimensions of PMO performance appraisal?

Steps for formulating the project PMO strategic plan

3000 frame animation illustrating why MySQL needs binlog, redo log and undo log

How does PMO select and train project managers?

I am 30 years old, no longer young, and have nothing

Four aspects of PMO Department value assessment
随机推荐
【TypeScript】在实战中的一些总结
Initial experience of nodejs express framework
Process injection
How to handle the prompt that DNS is incorrect when adding resolution to Tencent cloud?
JS delete object attribute
Disaster recovery series (VII) -- hybrid cloud public network export disaster recovery construction
On line project LAN on-line software use ----phpstudy
Command line add user set password never expires add remote group add administrator group
[hot sales at the beginning of the year] | the first special offer of popular cloud products is second to none, and the first year of 1-core 2G cloud server is 38 yuan!
[open source] goravel (golang Web Framework) - new cache module
ES6 promise detailed sewing Red Treasure Book Introduction to ES6 standard
How to dispose of the words on the picture? How do I add text to a picture?
December 29, 2021: the elimination rules of a subsequence are as follows: 1. In a subsequence
How to log in to the server through the fortress machine to transfer files? What are the specific steps?
Talk about leap seconds
Strokeit- the joy of one handed fishing you can't imagine
[redis] intersection and union of ordered sets
Process crash does not generate dump. Configure localdumps
Newbeecoder. UI new open source control library DataGrid instructions
. Net Core 3. X MVC built-in log extension log4net