Analysis & Answer
when are indexes useless
- 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.
- 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.
- like begins with %;
- 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.
- The index column in where has operations;
- The index column in where uses a function;
- If mysql thinks that the full table scan is faster (less data);
Index is not necessary when
- 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.
- 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.
- 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.
- Indexes When using <>, the effect is average; indexing is not recommended.
Reflect & Expand
Why is there an auto-increment ID when creating a table?
- 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)."
- 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.
- 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.
- 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!