What are the types of index failure?

Analysis & Answer

when are indexes useless

  1. If there is an or, there must be an index; if you want to use or and want the index to take effect, you can only add an index to each column in the or condition.
  2. The composite index does not use the left column field; if the first column of the composite index is not used, the subsequent columns will also be unusable, similar to the phone book.
  3. like begins with %;
  4. Type conversion is required; if there is an implicit conversion of the data type of the index column, the index cannot be used. For example, if the column type is a string, the data must be quoted in the condition, otherwise the index will not be used.
  5. The index column in where has operations;
  6. The index column in where uses a function;
  7. If mysql thinks that the full table scan is faster (less data);

Index is not necessary when

  1. Poor uniqueness; such as gender, there are only two possible data. It means that the binary tree level of the index is less, mostly flat. Such a binary tree lookup is tantamount to a full table scan.
  2. Fields that are frequently updated are not used (updating the index consumes); for example, the login times field, frequent changes cause the index to change frequently, which increases the workload of the database and reduces efficiency.
  3. Fields that are not used in where; Do not add an index when the field does not appear in the where statement. If there are conditions such as IS NULL /IS NOT NULL/ like '% input character %' after where, it is not recommended to use the index.
  4. Indexes When using <>, the effect is average; indexing is not recommended.

Reflect & Expand

Why is there an auto-increment ID when creating a table?

  1. Business irrelevance: When creating a primary key, try to use the MySQL auto-increment primary key instead of using the value generated by the business as the primary key. Official advice: "When choosing primary key values, consider using arbitrary values ​​(a synthetic key) rather than relying on values ​​derived from some other source (a natural key)."
  2. The primary key can uniquely identify this row of data, so as to ensure that only this row of data is operated when deleting and updating operations.
  3. Index needs, each InnoDB table has a special index, clustered index, used to store row data. Usually, clustered index and primary key are synonymous.
    • Declare the primary key, and InnoDB will use the primary key as a clustered index.
    • When not declared, the first index will be found where all key columns of UNIQUE are located, NOT NULL will be used as a clustered index
    • If it is not declared and no suitable UNIQUE index can be found, a hidden clustered index GEN_CLUST_INDEX is generated internally. The hidden row ID is 6 bytes and monotonically increases.
  4. In terms of use: it will be troublesome if there is no data migration.

Meow Interview Assistant: One-stop solution to interview questions, you can search the WeChat applet [Meow Interview Assistant]  or follow [Meow Brush Questions] -> Interview Assistant  free questions. If you have good interview knowledge or skills, look forward to your sharing!

Guess you like

Origin blog.csdn.net/jjclove/article/details/127391512