Ten years of JAVA moving bricks - basic knowledge of MYSQL optimization

1. Possibility of MYSQL index failure

1. The index column is not used by the query : If the query does not use the index column, MySQL will not be able to use the index for fast search, resulting in the index failure.

2. Functions are used on the index column : If functions (such as LOWER, UPPER, etc.) are used on the index column, MySQL cannot use the index for fast search, causing the index to become invalid.

**3. The data type of the index column does not match: **If the data type in the query condition does not match the data type of the index column, MySQL may not be able to use the index to search, causing the index to fail.

**4. The amount of data is too small: **For non-unique indexes, if the amount of data is too small, MySQL may choose to scan the entire table instead of using the index, causing the index to fail.

(1) 表的大小:如果表中的数据量非常小,比如只有几行或几十行,MySQL可能会选择全表扫描而不是使用索引。这是因为对于非常小的表来说,使用索引进行查找可能会导致额外的开销,而全表扫描可能更快。 
 
(2) 索引的选择性:索引的选择性是指索引中不同值的数量与表中总行数的比率。如果索引的选择性非常低,即索引列上的值重复较多,MySQL可能会认为使用索引进行查找并不高效,而选择全表扫描。 
 
(3) 查询优化器的判断:MySQL的查询优化器会根据统计信息和查询复杂度等因素来决定是使用索引还是全表扫描。如果查询优化器认为全表扫描更高效,即使数据量较小,也可能选择全表扫描。 

5. There are NULL values ​​in the index column : For ordinary indexes, if there are a large number of NULL values ​​in the index column, MySQL may give up using the index and choose a full table scan.

**6. The order of index columns does not match: **For a joint index, if the column order of the query condition does not match the column order of the index definition, MySQL may not be able to use the index to search, resulting in index failure.

Use explain to view the index usage of SQL

The meaning of the Type column
**1. system:** This is the highest level query type, indicating that there is only one row of data (such as a system table).

**2. const:** This is the best query type, which means that MySQL can use constants to match a single row during the query process. For example, MySQL can use the const query type when using a primary key or unique index for equivalent queries.

**3. eq_ref:** This is a very good query type, indicating that MySQL uses a join index to match a single row during the query process. For example, MySQL can use the eq_ref query type when using a primary key or unique index for equivalent queries in a join query.

**4. ref:** This is a better query type, indicating that MySQL uses a non-unique index to find matching rows during the query process. For example, MySQL can use the ref query type when a non-unique index is used in the query for equivalent query or IN query.

**5. range:** This is a better query type, indicating that MySQL uses the index range to find matching rows during the query process. For example, MySQL can use the range query type when a non-unique index is used in the query for a range query.

**6. index:** This is a general query type, indicating that MySQL uses an index to find matching rows during the query process. For example, MySQL can use the index query type when a non-unique index is used in the query for a LIKE query.

**7. all:** This is the worst query type, which means that MySQL needs to scan the entire table to find matching rows during the query process. For example, MySQL may use the all query type when no index is used or an inappropriate index is used in the query.

Extra column value and meaning

1. Using index: Indicates that the query is using the index to retrieve data, usually a good sign. This means that queries can quickly access the required rows through the index structure.

2. Using where: Indicates that the query is using the WHERE clause for filtering conditions. While this is necessary for result filtering, this flag alone doesn't tell much about performance.

3. Using temporary: Indicates that MySQL needs to create a temporary table to process queries. This can happen when complex GROUP BY, DISTINCT or ORDER BY clauses are present. While it can affect performance, it's usually not a significant issue.

4. Using filesort: Indicates that the query needs to perform additional sorting operations to satisfy the ORDER BY clause. This can happen when a query's ORDER BY clause cannot be satisfied by an index. File sorting can be resource intensive, especially for large result sets, affecting performance.

5. Using join buffer: Indicates that MySQL is using the connection buffer to perform join operations between tables. Usually efficient, but if the connection buffer size is too small, it may cause disk-based operations, affecting performance.

6. Using index condition : Indicates that MySQL is using index pushdown, which uses the index to evaluate part of the WHERE clause. It can improve performance by reducing the number of rows that need to be examined.

7. Impossible where: Indicates that the WHERE clause of the query contains contradictory conditions, making it impossible to satisfy the query. This may result in an empty result set.

index classification

1. Clustered index:

  • A clustered index determines the physical order of rows in a table.
  • Only one clustered index can be created per table.
  • The InnoDB storage engine in MySQL uses clustered indexes on primary keys by default.
  • When a table has a clustered index, the actual data rows are stored in the leaf nodes of the index itself.
  • The leaf nodes of a clustered index contain the entire row of data, so the entire row can be efficiently retrieved by primary key.
  • The order of the clustered index affects the physical order of the data on disk.

2. Non-clustered index:

  • Nonclustered indexes are structures that are separate from the actual data.
  • Multiple nonclustered indexes can be created per table.
  • A nonclustered index contains a copy of the indexed columns as well as pointers to the actual data.
  • The leaf nodes of a nonclustered index contain only the indexed columns and pointers to the corresponding data rows.
  • Nonclustered indexes are useful for efficient searching and sorting on specific columns, but may require additional lookups to retrieve entire rows of data

Guess you like

Origin blog.csdn.net/weixin_43485737/article/details/132280449