(Incomplete, MySQL does a lot of optimisations.)
The first issue about making a slow SELECT ... WHERE faster
is to check if one could add an index. All references between
different tables should usually be done with indexes. One can use
the EXPLAIN command to check which indexes are used in a
select. See section EXPLAIN syntax. Get information about a SELECT.. See section How does MySQL use indexes?
((a AND b) AND c OR
(((a AND b) AND (c AND d)))) -> (a AND b) OR (a AND b AND c AND d)
(a<b AND b=c) AND a=5 -> b>5 AND b=c AND a=5
(b>=5
AND b=5) OR (b=6 AND 5=5) OR (B=7 AND 5=6) -> B=5 OR B=6
CONST(*) on a single table without a WHERE is retrieved
directly from the table. This is also done for any NOT NULL
expression under the same conditions.
HAVING is merged with WHERE if one doesn't use GROUP
BY or group functions.
WHERE is constructed to get a fast
WHERE evaluation for each sub join and also to skip records as
soon as possible.
=, >,
>=, <, <=, BETWEEN and a LIKE with
a character prefix like 'something%'.
AND levels.
index = 1 or A = 10 -> NULL (can't use index.)
index = 1 or A = 10 and index=2 -> index = 1 OR index = 2
index_part_1 = const and index_part_3 = const -> index_part_1 =
const
const_table.index = constant
const_table.index_part_1 = const_table2.column and const_table.index_part_2 = constant
ORDER BY and in GROUP
come from the same table, then this table is preferred first when joining.
HAVING clause.
Go to the first, previous, next, last section, table of contents.