(mysql.info.gz) How to avoid table scan
Info Catalog
(mysql.info.gz) LIMIT optimization
(mysql.info.gz) Query Speed
(mysql.info.gz) Insert speed
7.2.13 How to Avoid Table Scans
-------------------------------
The output from `EXPLAIN' will show `ALL' in the `type' column when
MySQL uses a table scan to resolve a query. This usually happens under
the following conditions:
* The table is so small that it's faster to do a table scan than a
key lookup. This is a common case for tables with fewer than 10
rows and a short row length.
* There are no usable restrictions in the `ON' or `WHERE' clause for
indexed columns.
* 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. Where optimizations.
* 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 it will probably do a lot of key lookups and that
a table scan would be faster.
For small tables, a table scan often is appropriate. For large tables,
try the following techniques to avoid having the optimizer incorrectly
choose a table scan:
* Use `ANALYZE TABLE TBL_NAME' to update the key distributions for
the scanned table. `ANALYZE TABLE' ANALYZE TABLE.
* Use `FORCE INDEX' for the scanned table to tell MySQL that table
scans are very expensive compared to using the given index.
`SELECT' SELECT.
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 will cause more than 1,000 key seeks. Server
system variables.
Info Catalog
(mysql.info.gz) LIMIT optimization
(mysql.info.gz) Query Speed
(mysql.info.gz) Insert speed
automatically generated byinfo2html