An index is a sorted data structure! The fields that can be used for searching in the where condition and sorting in the order by condition can be quickly located and found by indexing the physical address of the data. Index Classification 1. Normal index: no constraints, mainly used to improve query efficiency 2. Unique index (UNIQUE): adds data uniqueness constraints based on ordinary indexes. There can be multiple 3. Primary key index: The primary key index adds a not-null constraint on the basis of the unique index, that is, NOT NULL+UNIQUE, and there can only be one 4. Full-text index (FULLTEXT): MySQL's built-in full-text index only supports English. Generally, a dedicated full-text search engine is used, such as ES (ElasticSearch) Create Index #Normal index ALTER TABLE table name ADD INDEX index name (column_list); #Unique index ALTER TABLE table name ADD UNIQUE index name (column_list); #Primary key index ALTER TABLE table name ADD PRIMARY KEY index name (column_list); #Full-text index (string data type) ALTER TABLE table name ADD fulltext index name (column_list); #View index show index from student \G Here, \G is used instead of the semicolon terminator; the output data information can be formatted. #Delete index Modify the table to delete the index ALTER TABLE table name DROP INDEX index name; Drop the primary key index ALTER TABLE table name DROP PRIMARY KEY; When the index column is one column, it is a single index; Under what circumstances can an index be created? 1. The value of the field has uniqueness restrictions, such as id 2. Fields that are frequently used as WHERE query conditions, especially when the data table is large
3. Columns that need to be frequently GROUP BY and ORDER BY 4. The WHERE condition columns of UPDATE and DELETE generally also need to create indexes
5.DISTINCT fields need to create indexes 6. When performing multi-table JOIN operations, you need to pay attention to the following principles when creating indexes
When do you not need to create an index?
1. There is no need to create indexes for fields that are not used in the WHERE conditions (including GROUP BY and ORDER BY); 2. If the table has too few records, for example, less than 1,000 rows, then there is no need to create an index; 3. If there is a large amount of duplicate data in a field, there is no need to create an index, such as the gender field; 4. Frequently updated fields do not necessarily need to be indexed. Because when updating data, you also need to update the index. If there are too many indexes, it will cause a burden when updating the index, thus affecting efficiency. Under what circumstances will the index fail? 1. If the index column is used for expression calculation and function, it will fail 2. In the WHERE clause, if the condition column before OR is indexed, but the condition column after OR is not indexed, the index will become invalid. 3. When we use LIKE for fuzzy query, the expression cannot start with % 4. Try to set the index column to NOT NULL constraint
The above is the detailed content of the basic syntax of MySQL index. For more information about MySQL index syntax, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
<<: Detailed explanation of nginx's default_server definition and matching rules
>>: Detailed explanation of JavaScript array deduplication
This article describes the examples of creating a...
The search performance from fastest to slowest is...
This article shares the specific code for JavaScr...
1. Install cmake 1. Unzip the cmake compressed pa...
1. Install mysql5.6 docker run mysql:5.6 Wait unt...
Problem Description As we all know, the SQL to so...
Virtualization and containerization are two inevi...
<br />First of all, I have to state that I a...
Table of contents Overview Precautions 1. Usage 2...
Table of contents Component Design Defining the f...
1. Download the MySQL installation package First ...
Written in advance: In the following steps, you n...
1. The role of doctype, the difference between st...
Preface Js is the most commonly used code manipul...
Install ssh tool 1. Open the terminal and type th...