当前位置:网站首页>[MySQL learning notes 22] index

[MySQL learning notes 22] index

2022-06-25 09:38:00 yqs_ two hundred and eighty-one million eight hundred and seven

Advantages and disadvantages

advantage Inferiority
Improve the efficiency of data retrieval , Reduce the io cost The index column takes up space
Sort data by indexing Columns , Reduce the cost of sorting data , Reduce CPU Consumption of Indexing greatly improves query efficiency , It also reduces the efficiency of updating tables , Because you need to update the index while updating the table

Index structure

Index structure describe
B+Tree Indexes ( Default ) The most common type of index , Most engines support
Hash Indexes The underlying principle is the hash table , Range query is not supported , Can only match exactly
R-Tree Commonly used for geospatial data types
Full-text( Full-text index ) It's a way of building inverted indexes , How to quickly match documents

Each engine supports indexing

 Insert picture description here

Index classification

classification meaning characteristic keyword
primary key The index created for the primary key in the table Automatically created by default , There can only be one PRIMARY
unique index Avoid a column with duplicate values There can be multiple UNIQUE, You can specify when creating a table , It can also be added manually later
General index Quickly locate data There can be multiple nothing , You need to create... Manually
Full-text index Fast Text Retrieval There can be multiple FULLTEXT

expand : The uniqueness of column value depends on the unique index .

classification meaning characteristic
Clustered index Put data storage and storage together , The leaf node of the index structure holds the row data There has to be , And there is only one , When creating a table, the primary key is automatically used as a clustered index , If there is no primary key , Use the first unique index as the clustered index , If not ,InnoDB Will automatically a hidden rowid As a clustered index
Secondary indexes Separate data from index , The leaf node of the index structure stores the corresponding primary key There can be multiple

example

 Insert picture description here
Such as execution select * from user where name =‘Arm’;
Then we'll go first name Search in the index Arm, Query to Arm The corresponding id yes 10, Then I'll take this 10 Go to the clustered index to find specific data , Then return . This process is called “ Return to the table for query ”.

Create index

create [unique|fulltext] index index_name on table_name (index_col_name1,…);

Look at the index

show index from table_name;

Delete index

drop index index_name on table_name;

原网站

版权声明
本文为[yqs_ two hundred and eighty-one million eight hundred and seven]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/176/202206250832477442.html