1. Declaration and use of index
1.1 Index classification
Functional logic : common index, primary key index, unique index, full-text index
Physical implementation : clustered index and non-clustered index
The number of effective fields : single column index and joint index
1. Ordinary index
Any field in the table can be created without restrictions. For example: create a common index for the name field in the student table, and query according to the index when querying records
2. Unique index
Use the unique parameter to set the unique index, unique can be empty, and a table has multiple unique indexes. For example: create a unique index for the email field in the student table
3. Primary key index
There can be at most one primary key (non-empty and unique) in a table, and only one primary key index. Because the primary key index is determined by the physical implementation, the data is stored in the file in an order
4. Single column index
Indexes on a single field
5. Composite index
The combination of multiple fields creates an index, using a composite index follows the leftmost prefix set. Such as: id and name create an index idx_id_name
6. Full text index
Full-text indexes can only be created on fields of char, varchar, and text types. Use full-text search to improve efficiency when querying strings with a large amount of data. For example, the student table value information is of text type.
7. Summary
1.2 Create an index
Create a table and add an index: create table table name (index (field name));
Modify the table to add an index: alter table table name add index (field name)
1. Create a table and add an index
【Format】
① There are primary key constraints, unique constraints, foreign key constraints, automatic index creation
CREATE TABLE dept(
dept_id INT PRIMARY KEY AUTO_INCREMENT,
dept_name VARCHAR(20)
);
CREATE TABLE emp(
emp_id INT PRIMARY KEY AUTO_INCREMENT,
emp_name VARCHAR(20) UNIQUE,
dept_id INT,
CONSTRAINT emp_dept_id_fk FOREIGN KEY(dept_id) REFERENCES dept(dept_id)
);
② Create a common index
When creating the table at the end, add index (field name)
CREATE TABLE book(
book_id INT ,
book_name VARCHAR(100),
authors VARCHAR(100),
info VARCHAR(100) ,
comment VARCHAR(100),
year_publication YEAR,
INDEX(year_publication)
);
Command to view the index:
show create table book;
或
show index from book;
③ Create a unique index
CREATE TABLE test1(
id INT NOT NULL,
name varchar(30) NOT NULL,
UNIQUE INDEX uk_idx_id(id)
);
④Primary key index: automatically created when the primary key is created
create table student(
id int(10) primary key,
no varchar(10),
name varchar(20)
);
view index
show index from student
delete index
alter table student
drop primary key
Modify the index: you must first delete (drop) the original index, and then create (add) the index
⑤ Create a combined index
create table book2(
id int,
name varchar(100),
authors varchar(100),
index mul_id_name_(id,name)
);
2. Modify the table to add the index add index (field name)
3. Delete the index of the table
Delete the column in the table and delete the corresponding index
2. Index Design Principles
1. Which situations are suitable for index creation
① Fields with unique restrictions
For example: the student number in the student table is unique, creating a unique index for this field can quickly determine the information of a student, and using the name query will have the same name phenomenon, which will slow down the query speed
②Fields with frequent where query conditions
A certain field is often used in the WHERE condition of the SELECT statement, so it is necessary to create an index for this field. Especially in the case of a large amount of data, creating a common index can greatly improve the efficiency of data query.
③Frequent group by and order by fields
Improve grouping and sorting queries
Indexing is to store data in a certain order. Using group by and order by requires grouping or sorting to be indexed
④Where condition column for update and deletion
When updating or deleting, we need to retrieve this record according to the where condition column, and update and delete it. If the update is a non-index field, the efficiency will be more obvious, and the index does not need to be maintained after the update
⑤ Distinct deduplication fields need to create an index
To deduplicate a field, use distinct to create an index for this field to improve query efficiency. Because it will be sorted according to this de-duplicated field, the same ones are all together, so the query speed becomes faster
⑥Precautions when multi-table join query
There should be no more than 3 tables, create an index for the where condition, and create the same index type for the connection field.
⑦ Create an index with a small column type
The smaller the data type, the less memory the index occupies, more records can be placed on a data page, and the query speed is faster. apply to primary key
⑧ Use the prefix of the string to create an index
The string is very long, and creating an index takes up a lot of space, so the first part needs to be intercepted, which is called a prefix index .
Although the search record cannot accurately locate the location of the record, it can locate the location of the corresponding prefix, and then query back to the table based on the primary key value.
Formula to choose the length of a string:
count ( distinct left ( column name , index length ))/count(*)
The closer it is to 1 , the lower the repetition rate and the closer to uniqueness. The index length is best at this moment . For general string type data, the index with a length of 20 can distinguish over 90% . Prefix index sorting may not be accurate
⑨ Columns with high discrimination are suitable as indexes, and those with low data duplication
count ( distinct left ( column name , index length ))/count(*)
The closer it is to 1 , the lower the repetition rate and the closer to uniqueness. The index length is best at this moment . More than 33% better index.
⑩Joint index: Frequent columns are on the left
[Supplement] Combined index is better than single value index
2. Limit the number of indexes, no more than 6 for a single table
①The index takes up disk space, the more indexes, the larger the disk space
②The index affects the update operation (addition, deletion and modification), the data in the table is modified, and the index will be modified
③ When the optimizer optimizes the query, it evaluates through the index. Multiple indexes increase the execution plan time of the optimizer and reduce the query performance.
3. Which situations are not suitable for index creation
① Do not set indexes for fields that are not used in the where condition . The value of the index is quick positioning, if it doesn't work, don't use the index
Only create an index on student_id, other fields do not need
② It is best not to use indexes for tables with a small amount of data
If the number of records is less than 1000, whether to create an index has little effect on query efficiency
③ Fields with a large amount of repeated data are not suitable for indexing
④ Do not create too many indexes for frequently updated tables
When updating data, the index must also be updated. The index improves the query efficiency, but reduces the update efficiency.
⑤ Do not use unordered values as indexes
For example: ID card, UUID
3. Summary
Indexes can improve query efficiency, but slow down insert and update speeds and take up memory.
The ultimate purpose of choosing an index is to make the query faster