当前位置:网站首页>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
边栏推荐
- Detailed explanation of conv2d -- use in arrays and images
- Operation record of reinitialization instance of Dameng database
- 2022-06-30: what does the following golang code output? A:0; B:2; C: Running error. package main import “fmt“ func main()
- 1175. prime number arrangement / Sword finger offer II 104 Number of permutations
- How to edit special effects in VR panorama? How to display detailed functions?
- CesiumJS 2022^ 源码解读[6] - 三维模型(ModelExperimental)新架构
- Is it safe to choose mobile phone for stock trading account opening in Hangzhou?
- [fundamentals of wireless communication-13]: illustrated mobile communication technology and application development-1-overview
- When is it appropriate to replace a virtual machine with a virtual machine?
- Flitter - sort list sort
猜你喜欢
Wordpress blog uses volcano engine veimagex for static resource CDN acceleration (free)
Software engineering best practices - project requirements analysis
The programmer's girlfriend gave me a fatigue driving test
如何使用 DataAnt 监控 Apache APISIX
Gateway service gateway
Cesiumjs 2022 ^ source code interpretation [6] - new architecture of modelempirical
5G智慧建筑解决方案2021
How to use dataant to monitor Apache APIs IX
Solve arm_ release_ ver of this libmali is ‘g2p0-01eac0‘,rk_ so_ Ver is' 4 ', libgl1 mesa dev will not be installed, and there are unsatisfied dependencies
Why did kubernetes win? The changes in the container circle!
随机推荐
35家巨头科技公司联合组成元宇宙标准论坛组织
Operation record of reinitialization instance of Dameng database
Detailed explanation of conv2d of pytorch
The college entrance examination in 2022 is over. Does anyone really think programmers don't need to study after work?
8253A寄存器浅析
Solution to the conflict between unique index and logical deletion
Prospects of world digitalization and machine intelligence in the next decade
Matlab saves triangulation results as STL files
Flitter - sort list sort
Solutions to errors in installing OpenSSL for CentOS 6.3 x64 PHP 5.2.6 extensions
未来十年世界数字化与机器智能展望
女朋友说:你要搞懂了MySQL三大日志,我就让你嘿嘿嘿!
Reason why wechat payment wxpaypubhelper V3 callback XML is empty
Online customer service system code_ H5 customer service_ Docking with official account_ Support app_ Support for multiple languages
IFLYTEK active competition summary! (12)
6-1 exploit -ftp exploit
Netease cloud sign in lottery? That year I could sign in for 365 days. No? Look.
flutter - sort List排序
How to mention hot fix and cherry pick
Software engineering best practices - project requirements analysis