Table of contents
The B+Tree index structure is classified according to the storage form
Classify according to index features
Primary key index - must be a clustered index
Unique index - can be a clustered index or a secondary index
Regular Index - Secondary Index
Full-text index - Full-Tree index structure
Spatial index - R-Tree index structure
Sort by number of fields indexed
Single-column index - clustered index and secondary index
Two Indexes to Optimize Indexes
Prefix Index - Secondary Index
Covering index - an optimization method for index query
Index Category Summary
Classify according to B+Tree storage form
Clustered index and secondary index (the storage format of the two indexes)
Classify according to the characteristics of the index
Primary key index, unique index, regular index, full-text index, spatial index
Among them, the primary key index, unique index, and regular index are all B+Tree index structures
The full-text index is a Full-Text index structure
The spatial index is an R-Tree index structure
Classify by the number of fields in a single index
Single column index and joint index
For index optimization, there are two kinds of indexes
Prefix index and covering index
Before explaining the specific types of indexes, let's understand the relevant knowledge of indexes
What is back query
Find the corresponding primary key value according to the secondary index, and then find the data of the row where the primary key value is located through the primary key index
The B+Tree index structure is classified according to the storage form
Clustered Index
It is to construct a B+ tree according to the primary key of each table, in which the data stored in the leaf nodes is the record data of the row corresponding to this index; the data of all the leaf nodes add up to the record data of the entire table
Each table must have and only one clustered index
Election rules for clustered indexes - clustered indexes are generally primary key indexes
If there is a primary key, the primary key index is a clustered index
If no primary key exists, the first unique index will be used as the clustered index
If there is no primary key and unique index, InnDB will automatically generate a DB_ROW_ID as a hidden clustered index
Secondary Index
Secondary indexes can also be called non-primary key indexes, such as unique indexes (if the unique index is not a clustered index), joint indexes, etc. can be collectively referred to as secondary indexes
A B+ tree is also constructed according to the primary key of each table, but the data stored in the leaf nodes is the value of the primary key index corresponding to this index and the value of the column
Each table can have multiple secondary indexes
Classify according to index features
Primary key index - must be a clustered index
The primary key index is an index created for the primary key in the table. There can only be one primary key index per table
Each table has at most one primary key. By default, a primary key index is automatically created for the primary key when creating the primary key.
The primary key index must meet three conditions:
- Primary key values must be unique
- Cannot contain Null values
- Make sure that the value is an auto-increment property
Create a primary key index - we only need to set the primary key constraint for the field to create a primary key index for the field
#Set the primary key when creating the table
CREATE TABLE table name (
Field 1 Type of field 1 PRIMARY KEY AUTO_INCREMENT,
……
);
#After the table is created successfully, set the field in the table as the primary key
ALTER TABLE table name MODIFY field name field type AUTO_INCREMENT PRIMARY KEY;
Unique index - can be a clustered index or a secondary index
The index can be set as a unique index by using the Unioue parameter
Uniqueness means that duplicate values are not allowed, but Null values are allowed and Null values are allowed to be repeated
There can be multiple unique indexes in a table
Create a unique index
#Set a unique index for the field when creating the table
CREATE TABLE table name (
field 1 type of field 1,
……,
UNIQUE KEY index name INDEX (field 1)
);
# After the table is created successfully, set a unique index for the fields in the table
CREATE UNIQUE INDEX index name ON table name (field 1);
Regular Index - Secondary Index
Also known as a non-unique index, it is the most basic index type in MySQL, which allows duplicate values in the indexed columns
The purpose of this index is only to improve query efficiency, quickly locate specific data, so that the database does not need full table scan
Can be created in any data structure; whether its value is unique and non-null is determined by the constraints of the field itself
create regular index
#Create a regular index for the field when creating the table
CREATE TABLE table name (
field 1 type of field 1,
……
KEY index name (field 1)
);
#After the table is created successfully, create a regular index for the fields of the table (common syntax)
CREATE INDEX index name ON table name (field 1);
#After the table is created successfully, create a regular index for the fields of the table (this syntax is not commonly used)
ALTER TABLE table name ADD INDEX index name (field 1);
Full-text index - Full-Tree index structure
The type of index built through the Full-Tree index structure; build an inverted index; find keywords in the text instead of comparing the values in the index
It is a key technology currently used by search engines. It can use word segmentation technology and other algorithms to intelligently analyze the frequency and importance of keywords in text, and then intelligently filter out the search results we want according to certain algorithm rules.
Can only be created on fields of CHAR, VATCHAR, TEXT types and their series types
Multiple full-text indexes can be created in a table
Create a full-text index FULLTEXT
#Create a full-text index for the field when creating a table (the tokenizer is ngram)
CREATE TABLE table name (
field 1 type of field 1,
……
FULLTEXT INDEX (field 1) WITH PARSER tokenizer type (generally use ngram)
);
#After the table is created successfully, create a full-text index for the fields of the table (common syntax)
CREATE FULLTEXT INDEX index name ON table name (field 1) WITH PARSER tokenizer type;
#After the table is created successfully, create a full-text index for the fields of the table (this syntax is not commonly used)
ALTER TABLE table name ADD FULLTEXT INDEX index name (field 1) WITH PARSER tokenizer type;
Full-text index query
Keywords for full-text index queries
MATCH: Columns to match (columns with full-text indexes)
AGAINST: what to find
Full-text index query statement (build a full-text index for the name field in the following table)
select * from test.emp where match ( name ) against ( ' playing mobile phone ' ) ; #Segment the word for playing mobile phone, extract the value after the word segmentation
select * from test.emp where match ( name ) against ( ' like to learn ' ) ; #For those who like to learn word segmentation, extract the value after word segmentation
Through the above experiments: we can understand that for the InnoDB storage engine, its tokenizer ngram will tokenize the value in the field when indexing; it will also tokenize the content to be searched when querying
Spatial index - R-Tree index structure
The index type established by the R-Tree index data structure; it is mainly used to store geographic location data, and it is rarely used
#After creating the table successfully, create a spatial index for the field
CREATE SPATIAL INDEX index name ON table name (field name);
Sort by number of fields indexed
Single-column index - clustered index and secondary index
As long as the index is established on a single column, it is called a single-column index
The above primary key indexes, unique indexes, regular indexes, full-text indexes, and spatial indexes are all types of single-column indexes
Joint Index - Secondary Index
Create an index for multiple columns on the table, that is, an index contains multiple columns, it becomes a joint index
In a business scenario, if there are multiple query conditions, it is recommended to create a joint index when considering building an index for the query field to avoid querying back to the table
The index points to multiple fields corresponding to the creation time, and the leftmost matching principle should be followed when querying
Create a joint index
CREATE INDEX index name ON table name (field 1, field 2,...);
When creating a joint index, pay attention to the order (sort from left to right according to the frequency of use, and place the frequently used ones on the far left)
For example: first sort by field 1, if the values of field 1 are consistent, then sort by field 2
Why do this is because the joint index needs to follow the leftmost matching principle
Two Indexes to Optimize Indexes
Prefix Index - Secondary Index
When the field type is a string (varchar, text, etc.), sometimes a very long string needs to be indexed, which will make the index large and waste a lot of disk IO during query, affecting query efficiency
At this time, only a part of the prefix of the string can be indexed, which greatly saves the index space and improves the index efficiency
Create a prefix index
CREATE INDEX index name ON table name (field name (extract the first few digits of the string));
How to choose the prefix length
It can be determined according to the selectivity of the index. Selectivity refers to the ratio of the unique index value (cardinality) to the total number of records in the data table
The higher the selectivity, the higher the query efficiency. The selectivity of the unique index is 1, which is the best index selectivity
How to Calculate Index Selectivity
SELECT COUNT(DISTINCT field) / COUNT(*) FROM table name; #Calculate the index selectivity of this field
SELECT COUNT(DISTINCT SUBSTRING(field,1,end bit n)) /count(*) FROM table name; #Calculate the index selectivity of the first n bits of the string
Covering index - an optimization method for index query
When we create an index, we actually create an index for the column
Covering index means that the index is used for conditional judgment when querying, and the column returned by the query is in the index column or the primary key index column
for example
For a table named user, there are id, name, age, job_number fields; create a primary key index for the id column; create a joint index for name and age; create a unique index for job_number
select * from user where id = 2; #This is the covering index;
Although the data of the row with id=2 is returned, since the id is the primary key index, that is, the clustered index, the data saved to the leaf node is the data of each row
select * from user where job_number = 123456; #At this time, it is not a covering index and needs to be returned to the table for query ;
We created a unique index for job_number. At this time, the entire table already has a clustered index, so this unique index is a secondary index. The data stored in its leaf nodes is the primary key index value and the key value of job_number; the data we want to divide is job_number=123456 All the data in this row, at this time, you need to use the value of the primary key index to find the corresponding row data according to the B+ tree generated by the primary key index
select id, job_number from user where job_number = 123456; #This is the covering index
select name, age from user where name = ' Zhang San'; #This is the covering index; because name and age have established a joint index; non-leaf nodes store the values of name and age and the primary key index value
Index Syntax Summary
CREATE INDEX
Create a primary key index - we only need to set the primary key constraint for the field to create a primary key index for the field
#Set the primary key when creating the table
CREATE TABLE table name (
Field 1 Type of field 1 PRIMARY KEY AUTO_INCREMENT,
……
);
#After the table is created successfully, set the field in the table as the primary key
ALTER TABLE table name MODIFY field name field type AUTO_INCREMENT PRIMARY KEY;
Create a unique index
#Set a unique index for the field when creating the table
CREATE TABLE table name (
field 1 type of field 1,
……,
UNIQUE KEY index name INDEX (field 1)
);
# After the table is created successfully, set a unique index for the fields in the table
CREATE UNIQUE INDEX index name ON table name (field 1);
create regular index
#Create a regular index for the field when creating the table
CREATE TABLE table name (
field 1 type of field 1,
……
KEY index name (field 1)
);
#After the table is created successfully, create a regular index for the fields of the table (common syntax)
CREATE INDEX index name ON table name (field 1);
#After the table is created successfully, create a regular index for the fields of the table (this syntax is not commonly used)
ALTER TABLE table name ADD INDEX index name (field 1);
Create a full-text index FULLTEXT
#Create a full-text index for the field when creating a table (the tokenizer is ngram)
CREATE TABLE table name (
field 1 type of field 1,
……
FULLTEXT INDEX (field 1) WITH PARSER tokenizer type (generally use ngram)
);
#After the table is created successfully, create a full-text index for the fields of the table (common syntax)
CREATE FULLTEXT INDEX index name ON table name (field 1) WITH PARSER tokenizer type;
#After the table is created successfully, create a full-text index for the fields of the table (this syntax is not commonly used)
ALTER TABLE table name ADD FULLTEXT INDEX index name (field 1) WITH PARSER tokenizer type;
create spatial index
CREATE SPATIAL INDEX index name ON table name (field name);
Create a joint index
CREATE INDEX index name ON table name (field 1, field 2,...);
View index SHOW INDEX
View all indexes in the specified table
SHOW INDEX FROM table name;
Tabele : table name
Non_unique : Whether to allow duplicate values
If it is 1, it means that duplicate values are allowed; if it is 0, it means that duplicate values are not allowed
Key_name : The index name to create the index (primary key is PRIMARY by default)
Seq_in_index : The serial number of the column in the index (for a joint index, it means the serial number of the column in the joint index, the smaller the more left)
Column_name : the field name in the table involved in the index
Collation : collation
A means ascending order (default), NULL means not sortable
Cardinality : the cardinality of the index
Indicates the number of unique values in the index; estimated value; index optimization can be done based on this value
Sub_part : the prefix length of the index
Packed : Whether the index is compressed (Null means it is not compressed)
NULL : Whether the column contains NULL values (Yes means to allow inclusion)
Index_type : Indicates the data structure used by the index (BTREE, Hash, FULLTEXT, etc.)
Comment : Comments for the index
DROP INDEX
Delete an index in the specified table
DROP INDEX index name ON table name;