A.6. Optimizer-Related Issues

MySQL 5.0

A.6. Optimizer-Related Issues

MySQL uses a cost-based optimizer to determine the best way to resolve a query. In many cases, MySQL can calculate the best possible query plan, but sometimes MySQL doesn't have enough information about the data at hand and has to make “educated” guesses about the data.

For the cases when MySQL does not do the "right" thing, tools that you have available to help MySQL are:

  • Use the statement to get information about how MySQL processes a query. To use it, just add the keyword to the front of your statement:

    mysql> 
    

    is discussed in more detail in Section 7.2.1, “Optimizing Queries with .

  • Use to update the key distributions for the scanned table. See Section 13.5.2.1, “ Syntax”.

  • Use for the scanned table to tell MySQL that table scans are very expensive compared to using the given index. See Section 13.2.7, “ Syntax”.

    SELECT * FROM t1, t2 FORCE INDEX (index_for_column)
    WHERE t1.col_name=t2.col_name;
    

    and may also be useful.

  • Global and table-level . See Section 13.2.7, “ Syntax”.

  • You can tune global or thread-specific system variables. For example, Start mysqld with the option or use to tell the optimizer to assume that no key scan causes more than 1,000 key seeks. See Section 5.2.2, “Server System Variables”.