当前位置:网站首页>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
边栏推荐
- Phpexcel import export
- 11-grom-v2-04-advanced query
- Vscode reports an error according to the go plug-in go get connectex: a connection attempt failed because the connected party did not pro
- 2.1 use of variables
- 3. Data binding
- HCIA-USG Security Policy
- MySQL dump - exclude some table data - MySQL dump - exclude some table data
- How to do Taobao full screen rotation code? Taobao rotation tmall full screen rotation code
- Microservice knowledge sorting - search technology and automatic deployment technology
- Preliminary practice of niuke.com (11)
猜你喜欢
Virtual machine installation deepin system
Today's work summary and plan: February 14, 2022
Professional interpretation | how to become an SQL developer
Native table - scroll - merge function
Teach you how to quickly recover data by deleting recycle bin files by mistake
Don't be afraid of no foundation. Zero foundation doesn't need any technology to reinstall the computer system
An old programmer gave it to college students
Shortest path problem of graph theory (acwing template)
Part 28 supplement (XXVIII) busyindicator (waiting for elements)
FPGA learning notes: vivado 2019.1 project creation
随机推荐
Kubernetes cluster builds efk log collection platform
Ruby replaces gem Alibaba image
What is the difference between a kill process and a close process- What are the differences between kill process and close process?
How to modify the network IP addresses of mobile phones and computers?
11-grom-v2-04-advanced query
Change deepin to Alibaba image source
thrift go
Titles can only be retrieved in PHP via curl - header only retrieval in PHP via curl
Rad+xray vulnerability scanning tool
Exercises of function recursion
2.5 conversion of different data types (2)
How to handle wechat circle of friends marketing activities and share production and release skills
In 2021, the global foam protection packaging revenue was about $5286.7 million, and it is expected to reach $6615 million in 2028
Fingerprint password lock based on Hal Library
Node MySQL serialize cannot rollback transactions
Make a simple text logo with DW
Instructions for common methods of regular expressions
Teach you how to quickly recover data by deleting recycle bin files by mistake
6006. Take out the minimum number of magic beans
Nacos usage of micro services