Some index related summary~

22. Types of Indexes

Single-column index : An index contains only a single column, but there can be multiple simple-interest indexes in a table

  (Ordinary index: the basic index type in MySQL, there are no restrictions, it is allowed to insert duplicate values ​​​​and null values ​​​​in the columns that define the index, purely for faster query efficiency. Unique index: the value in the index column must be unique , but allows null  primary key index : a special unique index that does not allow null primary key constraints is a primary key index)

Composite index : An index created on the combination of multiple fields in the table. The index will be used only when the left field of these fields is used in the query condition. Use the composite index to follow the leftmost prefix collection .

Full-text index : In a large amount of data, the record row to which the field belongs can be found through a certain keyword. Full-text indexes are rarely used in development because they take up a lot of physical space and reduce record modification.

  1. The primary key index is a constraint, and the unique index is an index, and the two are essentially different
  2. After the primary key is created, it must contain a unique index, and the unique index is not necessarily the primary key
  3. Unique index columns allow null values, while primary key columns do not allow null values
  4. A primary key can be referenced as a foreign key by other tables, while a unique index cannot)

25 Under what circumstances does the index fail?

  Optimize the query, try to avoid full table scans, and avoid index failure

  Avoid using it in where clauses! Symbols such as =, <>, otherwise the engine will give up the index and generate a full table scan

  Avoid using or to connect conditions in the where clause, because if one of the two fields has no index, the engine will give up and scan the entire table

  Avoid using expression operations or function operations on the left side of = in the where clause

Avoid using Like fuzzy queries in where clauses

The steps of index optimization are:

  1. Use explain to view the sql execution plan
  2. Determine which indexes are being used incorrectly
  3. Optimize sql, sql may need multiple optimizations to achieve the optimal value of index usage

Guess you like

Origin blog.csdn.net/qq_42082023/article/details/126010901