MySQL index insights

1. Index generation

- 键 (Key)

First of all, we found that in most cases, positioning operations do not need to match the entire row of data. Instead, it matches only one
or several column values regularly , that is, a certain value or several column values ​​are used as query conditions. For example, the first column in the figure can be used to identify a record. These columns used to determine a piece of data are
collectively referred to as keys.
Insert picture description here

According to the principle of reducing invalid data access, we take the key value and store it in a separate block. And
add a pointer for each key value to point to the original data block. as the picture shows.

Insert picture description here

This is the Dense Index, the ancestor of the'index'. When the positioning operation is performed, the table scan is no longer performed. Instead, perform an
Index Scan, read out all index blocks in turn, and perform key-value matching. When the matching key value is found,
the corresponding data block is directly read according to the pointer of the row, and the operation is performed.

Second, the syntax of indexes in MySQL

Create index

1. Add an index when creating a table

CREATE TABLE mytable(  
    ID INT NOT NULL,   
    username VARCHAR(16) NOT NULL,  
    INDEX [indexName] (username(length))  
); 

2. Add an index after creating the table

ALTER TABLE my_table ADD [UNIQUE] INDEX index_name(column_name);
或者
CREATE INDEX index_name ON my_table(column_name);

3. Query based on the index

具体查询:
SELECT * FROM table_name WHERE column_1=column_2;(为column_1建立了索引)

或者模糊查询
SELECT * FROM table_name WHERE column_1 LIKE '%三'
SELECT * FROM table_name WHERE column_1 LIKE '三%'
SELECT * FROM table_name WHERE column_1 LIKE '%三%'

SELECT * FROM table_name WHERE column_1 LIKE '_好_'

如果要表示在字符串中既有A又有B,那么查询语句为:
SELECT * FROM table_name WHERE column_1 LIKE '%A%' AND column_1 LIKE '%B%';

SELECT * FROM table_name WHERE column_1 LIKE '[张李王]三';  //表示column_1中有匹配张三、李三、王三的都可以
SELECT * FROM table_name WHERE column_1 LIKE '[^张李王]三';  //表示column_1中有匹配除了张三、李三、王三的其他三都可以

//在模糊查询中,%表示任意0个或多个字符;_表示任意单个字符(有且仅有),通常用来限制字符串长度;[]表示其中的某一个字符;[^]表示除了其中的字符的所有字符

或者在全文索引中模糊查询
SELECT * FROM table_name WHERE MATCH(content) AGAINST('word1','word2',...);

4. Delete the index

DROP INDEX my_index ON tablename;
或者
ALTER TABLE table_name DROP INDEX index_name;

5. View the index in the table

SHOW INDEX FROM tablename

6. View the use of the index in the query statement

//explain 加查询语句
explain SELECT * FROM table_name WHERE column_1='123';

Three, the advantages and disadvantages of the index

Advantages: fast retrieval, reducing I/O times, and speeding up retrieval; grouping and sorting according to index can speed up grouping and sorting;

Disadvantages: The index itself is also a table, so it will take up storage space. Generally speaking, the space occupied by the index table is 1.5 times that of the data table; the maintenance and creation of the index table requires time and cost, and this cost increases as the amount of data increases ; Building an index will reduce the efficiency of data table modification operations (delete, add, modify), because the index table needs to be modified while the data table is modified;

Fourth, the classification of the index

Common index types are: primary key index, unique index, ordinary index, full-text index, composite index

1. Primary key index: that is, the primary index, which is indexed according to the primary key pk_clolum (length), does not allow duplicates, and does not allow null values;

ALTER TABLE 'table_name' ADD PRIMARY KEY pk_index('col')

2. Unique index: the value of the column used to build the index must be unique, and null values ​​are allowed

ALTER TABLE 'table_name' ADD UNIQUE index_name('col')

3. Ordinary index: an index constructed with ordinary columns in the table, without any restrictions

ALTER TABLE 'table_name' ADD INDEX index_name('col')

4. Full-text index: an index built with columns of large text objects

ALTER TABLE 'table_name' ADD FULLTEXT INDEX ft_index('col')

5. Joint index: an index constructed with a combination of multiple columns. The values ​​in these multiple columns are not allowed to have null values

ALTER TABLE 'table_name' ADD INDEX index_name('col1','col2','col3')

*Follow the principle of "leftmost prefix", put the most commonly used columns for retrieval or sorting on the far left, in descending order, the joint index is equivalent to the establishment of col1, col1col2, col1col2col3 three indexes, and col2 or col3 cannot use the index .

*When using a composite index, the key of the index may be too long due to the length of the column name being too long, resulting in lower efficiency. If allowed, only the first few characters of col1 and col2 can be used as the index

ALTER TABLE ‘table_name’ ADD INDEX index_name(col1(4),col2(3));

Indicates to use the first 4 characters of col1 and the first 3 characters of col2 as indexes

Five, the use strategy of the index

1. When should I use the index?

The primary key automatically creates a unique index;

Columns that are often used as query conditions in WHERE or ORDER BY statements should be indexed;

As a sorted column to be indexed;

Query the fields associated with other tables in the query, and create an index for the foreign key relationship

Prone to combined index under high concurrency conditions;

Columns used for aggregate functions can be indexed, for example, column_1 when max(column_1) or count(column_1) is used, it needs to be indexed

2. When should you not use the index?

Do not create indexes for columns that are frequently added, deleted, and modified;

There are a large number of duplicate columns without indexing;

Do not create an index if there are too few table records. Only when there is enough test data in the database, its performance test results have actual reference value. If there are only a few hundred data records in the test database, they are often all loaded into the memory after the first query command is executed, which will make subsequent query commands executed very fast-regardless of whether the index is used or not. Only when the records in the database exceed 1,000 and the total amount of data exceeds the total amount of memory on the MySQL server, the database performance test results are meaningful.

Six, the situation of index failure

There cannot be a column whose value is NULL in the composite index. If there is, then this column is invalid for the composite index;

In a SELECT statement, the index can only be used once. If it is used in WHERE, then do not use it in ORDER BY; In
LIKE operation,'%aaa%' will not use the index, that is, the index will fail, but 'aaa%' can use index;

Using expressions or functions on the indexed columns will invalidate the index, for example: select * from users where YEAR(adddate)<2007, operations will be performed on each row, which will cause the index to fail and perform a full table scan, so We can change it to: select * from users where adddate<'2007-01-01'. Other wildcards are the same, that is, when using regular expressions in query conditions, the index can only be used if the first character of the search template is not a wildcard.
Use inequality in query conditions, including <symbol,> symbol and! = Will cause the index to become invalid. Especially if it is used for the primary key index! = Will not invalidate the index. If you use the <symbol or> symbol for the primary key index or integer type index, the index will not be invalidated. (As reminded by classmate erwkjrfhjwkdb, it is not equal to, including <symbol,> symbol and !, if it accounts for a small proportion of the total record, it will not be invalid);

Using IS NULL or IS NOT NULL in the query conditions will cause the index to become invalid;

The string without single quotes will cause the index to become invalid. To be more precise, inconsistent types will lead to invalidation. For example, if the field email is of string type, using WHERE email=99999 will result in failure. It should be changed to WHERE email='99999';

Using OR to connect multiple conditions in the query conditions will cause the index to fail, unless each condition of the OR link is added with an index, then it should be changed to two queries, and then connected with UNION ALL;

If the sorted field uses an index, then the select field must also be an index field, otherwise the index becomes invalid. In particular, if the sorting is the primary key index, select * will not cause the index to become invalid;

Try not to include multi-column sorting, if you must, it is best to build a composite index for this queue;

Seven, index optimization

1. The leftmost prefix

The leftmost prefix of the index is related to the "leftmost prefix principle" in B+Tree. For example, if the combined index <col1,col2,col3> is set, then the index can be used in the following 3 situations: col1,<col1, col2>, <col1,col2,col3>, other columns, such as <col2,col3>, <col1,col3>, col2, col3, etc. cannot use indexes.

According to the principle of the leftmost prefix, we generally put the column with the highest sorting frequency on the left, and so on.

2. Fuzzy query optimization with index

As mentioned above, when using LIKE for fuzzy query,'%aaa%' will not use the index, that is, the index will be invalid. If this is the case, you can only use full-text indexing for optimization (described above).

3. Build a full-text index for the search conditions, and then use

SELECT * FROM tablename MATCH(index_colum) ANGAINST(‘word’);

4. Use short indexes

To index the list, you should specify a prefix length if possible. For example, if there is a CHAR(255) column, if the multi-value is unique within the first 10 or 20 characters, then do not index the entire column. Short index can not only improve query speed but also save disk space and I/O operations.

Guess you like

Origin blog.csdn.net/baidu_24752135/article/details/110496520