Some rules we should remember when doing MySQL index query:
- Index uses B-TREE, so be sure we understand how B-Tree works.
- Index is left-most, so if there is (c1, c2, c3) index, then query WHERE c1 = 1 AND c2 = 2 does use index, but not WHERE c2 = 2
- When considering indexing, remember the following orders:
- fields in WHERE is index left-most
- then, fields in GROUP BY, ORDER BY
- if it is possible, just select fields in the created index so that mysql optimizer does not need to touch the real table data
- when there is many fields in WHERE, index exact search (= condition) first, then range (<>, >, <, etc.)
- In some case, if the range conditions (<>, >, <, etc.) causing temporary table and filesort, try using MySQL Query Optimization using UNION
- If there are many possible indexes, but mysql optimizer chooses the wrong one when querying, try to use FORCE INDEX