当前位置:网站首页>18、 MySQL -- index
18、 MySQL -- index
2022-07-03 20:21:00 【Seven peanuts】
1. What is index ?
Indexes It's a separate 、 Physical to database tables A column of or Multiple columns The value of the to Sort A kind of Storage structure .
It's equivalent to a book catalog , The function is to improve SELECT Statement efficiency .
Since it is equivalent to a directory , The catalogue is also the content of the book , The stored content is in the form of data , therefore Index is also data .
2. Why index is needed ?
We need to search data from the table , The common method is full table scanning , That is, search from the first data block of the table , Find the last data block in order . When the amount of data is small , Yes MySQL Service requirements are not high , But when the amount of data is large MySQL High performance consumption , Inefficient search , So is there an efficient way ?
When we read books looking for specific content , You can find the target content from the directory first , Then go to the specific number of pages to find the content , The index came into being .
3.MySQL Index in
innodb Storage engine support 3 Class index
b+ Tree index
Hash index
Full table index
among b+ Tree index is used most , Therefore, this article only discusses b+ Tree index
b+ A tree is a data structure , Data used to store indexes
b+ Trees (BTREE) It is divided into Clustered index (clustered index) and Secondary index (secondary index or non-clustered index)
Clustered index
Definition : According to each table Primary key Build a tree b+ Trees , At the same time, what is stored in the leaf node is the The whole line of data .
The leaf node of the clustered index is also called Data pages , Index organization table ( That is, the data in the table is stored in the order of the primary key ) The data in is also part of the index . Because the actual data page can only follow one b+ Trees sort , Therefore, a table can only have One Clustered index . Query optimizer Tends to use clustered indexes , Because the clustered index can find data directly on the leaf node of the index .

# Because the index has sorting function , So when using query statements to query rows with indexes , There is no need to add sorting statements
Secondary index
Leaf node No Containing rows All data . Leaf nodes contain the index key values of rows , Each index row also contains a primary key value , The primary key value tells innodb Storage engine , Where can I find the data corresponding to the index
# Contains data from other columns and pointers to the primary key index
Single index
The number of key values of the index is 1
Composite index
Unlike a single column index, the number of keys in a composite index is not 1, But greater than or equal to 2
# Query a single leading column in the composite index , You can also use this composite index , However, compound queries cannot be used for queries with non leading columns .
Create index
CREATE INDEX id_name_idx ON IG(id,name);
# In the table IG To create id As the leader name Is a non leading composite index id_name_idx
Look at the index
show index from IG;
# See the table IG All the indexes of

Some parameters in the table :
Key_name --》 Index name
Non_unique--》 by 0 Expressed as a unique index , by 1 Expressed as a non unique index
Collation --》 How to save column index as .A Expressed as ascending sort ,null It means no sorting
Cardinality--》 The closer the value is to the number of rows , The richer the fields , The more suitable for indexing , On the contrary, it is not suitable for indexing
Delete index
DROP INDEX id_name_idx on IG;
# Delete table IG Index in id_name_idx
Two sides of index
advantage : Improve query efficiency
shortcoming : Modified the data ,MySQL I will modify the index , Lead to MySQL Performance degradation . The cost of maintaining indexes is relatively large
边栏推荐
- How to handle wechat circle of friends marketing activities and share production and release skills
- In 2021, the global general crop protection revenue was about $52750 million, and it is expected to reach $64730 million in 2028
- HCIA-USG Security Policy
- Gym welcomes the first complete environmental document, which makes it easier to get started with intensive learning!
- [Yugong series] February 2022 Net architecture class 004 ABP vNext used in WPF project
- Network security Kali penetration learning how to get started with web penetration how to scan based on nmap
- Global and Chinese market of two in one notebook computers 2022-2028: Research Report on technology, participants, trends, market size and share
- 【c】 Digital bomb
- How to read the source code [debug and observe the source code]
- Test access criteria
猜你喜欢

LabVIEW training
![Oak-d raspberry pie cloud project [with detailed code]](/img/34/76b461bf03fba373da5b5898c5204c.jpg)
Oak-d raspberry pie cloud project [with detailed code]

Popularize the basics of IP routing

Gym welcomes the first complete environmental document, which makes it easier to get started with intensive learning!

In 2021, the global foam protection packaging revenue was about $5286.7 million, and it is expected to reach $6615 million in 2028

Detailed and not wordy. Share the win10 tutorial of computer reinstallation system

Task of gradle learning

Explore the internal mechanism of modern browsers (I) (original translation)

44. Concurrent programming theory

In 2021, the global revenue of thick film resistors was about $1537.3 million, and it is expected to reach $2118.7 million in 2028
随机推荐
Introduction to golang garbage collection
Typora charges, WTF? Still need support
Network security Kali penetration learning how to get started with web penetration how to scan based on nmap
Vscode reports an error according to the go plug-in go get connectex: a connection attempt failed because the connected party did not pro
4. Data splitting of Flink real-time project
Microservice knowledge sorting - search technology and automatic deployment technology
FAQs for datawhale learning!
The 29th day of force deduction (DP topic)
In 2021, the global revenue of thick film resistors was about $1537.3 million, and it is expected to reach $2118.7 million in 2028
Blue Bridge Cup: the fourth preliminary - "simulated intelligent irrigation system"
QT tutorial: signal and slot mechanism
Use of CMD command
First knowledge of database
Global and Chinese market of micro positioning technology 2022-2028: Research Report on technology, participants, trends, market size and share
11-grom-v2-05-initialization
Point cloud data denoising
【c】 Digital bomb
Titles can only be retrieved in PHP via curl - header only retrieval in PHP via curl
Utilisation de base du cadre unitest
The 15 year old interviewer will teach you four unique skills that you must pass the interview