The output from 当MySQL使用完整表扫描来解析查询时,EXPLAIN
shows ALL
in the type
column when MySQL uses a full table scan to resolve a query. EXPLAIN
的输出在type
列中显示ALL
。This usually happens under the following conditions:这通常在以下情况下发生:
The table is so small that it is faster to perform a table scan than to bother with a key lookup. 表太小了,执行表扫描比查找键要快。This is common for tables with fewer than 10 rows and a short row length.这对于行数少于10行且行长较短的表很常见。
There are no usable restrictions in the 对于索引列,ON
or WHERE
clause for indexed columns.ON
或WHERE
子句中没有可用的限制。
You are comparing indexed columns with constant values and MySQL has calculated (based on the index tree) that the constants cover too large a part of the table and that a table scan would be faster. 您正在将索引列与常量值进行比较,MySQL(基于索引树)计算出常量覆盖了表中过大的一部分,并且表扫描会更快。See Section 8.2.1.1, “WHERE Clause Optimization”.请参阅第8.2.1.1节,“WHERE子句优化”。
You are using a key with low cardinality (many rows match the key value) through another column. 您正在通过另一列使用基数较低的键(许多行与键值匹配)。In this case, MySQL assumes that by using the key probably requires many key lookups and that a table scan would be faster.在这种情况下,MySQL假设使用键可能需要许多键查找,并且表扫描会更快。
For small tables, a table scan often is appropriate and the performance impact is negligible. 对于小表,表扫描通常是合适的,并且对性能的影响可以忽略不计。For large tables, try the following techniques to avoid having the optimizer incorrectly choose a table scan:请使用以下技术来避免不正确地扫描大型表:
Use 使用ANALYZE TABLE
to update the key distributions for the scanned table. tbl_name
ANALYZE TABLE
以更新扫描表的键分布。tbl_name
See Section 13.7.3.1, “ANALYZE TABLE Statement”.请参阅第13.7.3.1节,“ANALYZE TABLE语句”。
Use 对扫描的表使用FORCE INDEX
for the scanned table to tell MySQL that table scans are very expensive compared to using the given index:FORCE INDEX
告诉MySQL,与使用给定索引相比,表扫描非常昂贵:
SELECT * FROM t1, t2 FORCE INDEX (index_for_column
) WHERE t1.col_name
=t2.col_name
;
Start mysqld with the 使用--max-seeks-for-key=1000
option or use SET max_seeks_for_key=1000
to tell the optimizer to assume that no key scan causes more than 1,000 key seeks. --max-seeks-for-key=1000
选项启动mysqld,或者使用SET max_seeks_for_key=1000
告诉优化器假设没有任何键扫描导致超过1000次键搜索。See Section 5.1.8, “Server System Variables”.请参阅第5.1.8节,“服务器系统变量”。