当前位置:网站首页>MySQL index test
MySQL index test
2022-06-30 23:52:00 【Dark hyacinth】
MySQL Index test
1 Index type
1 primary key (PRIMARY KEY)
| name | effect |
|---|---|
| primary key (PRIMARY KEY) | A primary key index is an index created specifically for primary key fields , Duplicate or empty values are not allowed , And a table can only have one primary key index , It can improve query efficiency , And provide uniqueness constraints . |
| name | effect |
|---|---|
| id | Yes means that the query is executed select The order of clauses or operation tables .ID identical ( It can be considered the same group ) From top to bottom , Subquery ID The value is incremented ,ID The higher the value, the earlier it is executed . |
| select_type | Represents each... In the query select The type of clause ( Simple OR complex ) |
| table | indicate |
| partitions | |
| type | all < index < range < ref < eq_ref < const/system < null |
| possible_keys | Pointed out that MySQL Which index can be used to find rows in the table , If there is an index on the field involved in the query , Then the index will be listed , But not necessarily used by queries . |
| key | Show MySQL The index actually used in the query , If you don't use an index , Is shown as NULL. |
| key_len | Represents the number of bytes used in the index , You can use this column to calculate the length of the index used in the query . |
| ref | Indicates the join matching condition of the above table , That is, which columns or constants are used to find values on index columns . |
| rows | Express MySQL According to table statistics and index selection , Estimated number of rows to read to find the required record . |
| filtered | |
| Extra | Contains additional information that is not suitable for display in other columns but is important |
| select_type | effect |
|---|---|
| SIMPLE | The query does not contain subqueries or UNION. |
| PRIMARY | If the query contains any complex sub parts , The outermost query is marked :PRIMARY . |
| SUBQUERY | stay SELECT or WHERE The list contains subqueries , The subquery is marked as :SUBQUERY. |
| DERIVED | stay FROM The subqueries contained in the list are marked with :DERIVED( derivative ). |
| DERIVED | If the second SELECT Appear in the UNION after , Is marked as UNION; if UNION Included in FROM Clause , Outer layer SELECT Will be marked as :DERIVED. |
| UNION RESULT | from UNION Table to get the result SELECT Marked as :UNION RESULT |
| type | effect |
|---|---|
| all | Full Table Scan, MySQL Will traverse the entire table to find the matching rows . |
| index | Full Index Scan,index And ALL The difference for index Type only traverses the index tree |
| range | Index range scan , The scan of the index starts at a certain point , Return the row matching the value field , Common in between、<、> And so on |
| ref | Non unique index scan , Returns all rows that match a single value . It is commonly used in searches using non unique indexes, that is, non unique prefixes of unique indexes |
| eq_ref | Unique index scan , For each index key , Only one record in the table matches it . Common in primary key or unique index scan |
| const/system | When MySQL Optimize some part of the query , And convert to a constant , Use these types to access . For example, place the primary key in where In the list ,MySQL You can convert the query to a constant |
| null | MySQL Decompose statements during optimization , Execute without even accessing tables or indexes |
| Extra | effect |
|---|---|
| Using index | The value represents the corresponding select The override index is used in the operation (Covering Index). |
| Using where | Express MySQL The server does it after the storage engine receives the record “ Post filtration ”(Post-filter), If the query fails to use the index ,Using where Our role is just to remind us MySQL Will use where Clause to filter the result set . |
| Using temporary | Express MySQL You need to use temporary tables to store result sets , Common in sorting and grouping queries . |
1 Add index
alter table `student` add primary key `id` (`id`);
explain select * from student where id = 1;
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | SIMPLE | student | ALL | PRIMARY | 18 | 10 | Using where |
2 Delete index
# If the primary key is self incrementing, you need to delete the self incrementing ( Modify field type )
alter table `student` drop primary key;
explain select * from student where id = 1;
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | SIMPLE | student | ALL | 18 | 10 | Using where |
2 General index (INDEX/KEY)
| name | effect |
|---|---|
| General index (INDEX/KEY) | General index is MySQL The most basic index type in , It has no data type restrictions , Allows you to insert duplicate and null values in a column that defines an index , It can speed up the system's access to data . |
1 Single index
Using short index can not only improve query speed , And it can save disk operation and I/O operation , Such as address by varchar(200) If it is often stored as 100 Then set the index 100 Better than default and 200
alter table `student` add index `address` (`address`(100)) using btree;
2 Composite index
Composite index has the leftmost principle
alter table `student` add index `mult` (`age`,`name`) using btree;
3 unique index (UNIQUE KEY)
| name | effect |
|---|---|
| unique index (UNIQUE KEY) | The value of a unique index column must be unique , Null value allowed . If it's a composite index , The combination of column values must be unique , The purpose of unique index is not to improve access speed , It's to avoid duplication of data . |
alter table `student` add unique index `phone` (`phone`);
4 Full-text index (FULL TEXT)
| name | effect |
|---|---|
| Full-text index (FULL TEXT) | Full text indexing allows the insertion of duplicate and null values in the indexed columns , Full text index is mainly used to find keywords in text , Only in CHAR、VARCHAR or TEXT Create on column of type , stay MySQL There are only MyISAM The storage engine supports full-text indexing . |
1 How it was created
alter table `student` add fulltext index `name` (`name`);
2 Usage mode
select * from `student` where match(`name`) against(' Zhang San ')
5 Spatial index (SPATIAL)
| name | effect |
|---|---|
| Spatial index (SPATIAL) | A spatial index is an index of a field of a spatial data type , Use SPATIAL Keyword extension , Columns that create spatial indexes must be declared as NOT NULL, Spatial index can only be used when the storage engine is MyISAM Create... In the table of , Spatial index is mainly used for geospatial data types GEOMETRY. This kind of index is rarely used . |
alter table `student` add spatial index `map` (`map`);
2 Index method
1 BTREE
BTREE The index is stored in a tree structure , Usually used in like "=,>,>=,<,<=、BETWEEN、Like" The query efficiency of equal operator is higher .
B-Tree Is the most common type of index , All worth ( Indexed columns ) It's all sorted out , The distance from each leaf node to the following node is equal . therefore B-Tree It is suitable for finding data in a certain range , And it can directly support data sorting (ORDER BY)
B-Tree stay MyISAM The form and Innodb not quite the same
| MySQL engine | Data files |
|---|---|
| MyISAM | MyISAM The table data file and the index file are separate , The index file only stores the disk address of the data record . |
| InnoDB | InnoDB The table data file itself is the primary index , Leaf nodes data Domain holds complete data records . |
2 HASH
1 Support only “=”,“IN” and “<=>” Precise query , Can't use range query
because Hash Index comparison is to carry out Hash After the operation Hash value , So it can only be used for equivalent filtering , Can't be used for range based filtering , Because after the corresponding Hash After algorithm processing Hash
2 Sort is not supported
because Hash What the index stores is the process Hash After calculation Hash value , and Hash The magnitude of the value does not necessarily relate to Hash The key value before the operation is exactly the same , So the database can't use the index data to avoid any sort operation
3 Table scanning cannot be avoided at any time
because Hash Index comparison is to carry out Hash After the operation Hash value , So even if you meet someone Hash Number of records of key value data , I can't go from Hash Direct query in index , You still need to access the actual data in the table for corresponding comparison , And get the corresponding result
4 High retrieval efficiency
The index can be retrieved at one time , Unlike B-Tree The index needs to be from the root node to the branch node , Finally, you can access the page node for many times IO visit , therefore Hash The query efficiency of index is much higher than B-Tree Indexes
5 Only Memory The engine supports explicit Hash Indexes
But it's Hash yes nonunique Of , Too many conflicts will also affect the search performance .Memory The default index type of the engine is Hash Indexes , Although it also supports B-Tree Indexes
3 RTREE
R-Tree stay MySQL Rarely used , Support only geometry data type , The only storage engine that supports this type is MyISAM、BDb、InnoDb、NDb、Archive several .
3 Index usage considerations
1 Do not use NULL
As long as the column contains NULL Values will not be included in the index , A composite index has only one column containing NULL value , Then this column is invalid for the composite index , Therefore, when designing the database, we'd better not let the default value of the field be NULL;
2 Use short index
Using short index can not only improve query speed , And it can save disk operation and I/O operation .
3 Index column sort
MySQL Only one index will be used when querying , So if where If the clause has used an index , that order by The columns in will not use indexes , therefore order by Try not to include sorting of multiple columns , If you have to sort multiple columns , It's best to use a composite index .
4 Like sentence
In general, the use of like, If it is not used, you need to pay attention like ‘% Zhang San %’ No index ; but like ‘ Zhang San %’ To use the index .
5 Don't use NOT IN and <> operation
边栏推荐
- [NLP] [textcnn] text classification
- 在指南针上买基金安全吗?
- Cesiumjs 2022 ^ source code interpretation [6] - new architecture of modelempirical
- 什么是SRM系统,如何规范公司内部采购流程
- Understand target detection in one article: r-cnn, fast r-cnn, fast r-cnn, Yolo, SSD "suggestions collection"
- Qt笔记(七十四)之QLineEdit指定输入类型
- Operation record of reinitialization instance of Dameng database
- Solution to the conflict between unique index and logical deletion
- 网上开华泰证券的股票账户是否安全呢?
- shell 同时执行多任务下载视频
猜你喜欢

New trends of China's national tide development in 2022

未来十年世界数字化与机器智能展望

How to use dataant to monitor Apache APIs IX

Introduction to digital transformation solutions for enterprises going to sea

ABAQUS 2022 software installation package and installation tutorial

Bridge emqx cloud data to AWS IOT through the public network

"Paddle + camera" has become a "prefabricated dish" in the AI world, and it is easier to implement industrial AI quality inspection

shell 同时执行多任务下载视频

How does the VR cloud exhibition hall bring vitality to offline entities? What are the functions?

6-1 exploit -ftp exploit
随机推荐
1175. prime number arrangement / Sword finger offer II 104 Number of permutations
When is it appropriate to replace a virtual machine with a virtual machine?
深入理解 Jetpack Compose 内核:SlotTable 系统
异步過渡方案—Generator
MaxPool2d详解--在数组和图像中的应用
Fund managers' corporate governance and risk management
hot-fix、cherry-pick怎么提
shell 同时执行多任务下载视频
Red Hat将在Project Atomic上运用容器负载服务器
Dataloader source code_ DataLoader
JMeter cross thread parameter association requires no script
CTFSHOW权限维持篇
股票开户要如何办理呢?办理手机开户安全吗
5g smart building solution 2021
SSM integration process (integration configuration, function module development, interface test)
IFLYTEK active competition summary! (12)
What does project management really manage?
什么是SRM系统,如何规范公司内部采购流程
conv2d详解--在数组和图像中的使用
Online customer service chat system source code_ Beautiful and powerful golang kernel development_ Binary operation fool installation_ Attached construction tutorial