一般优化mysql首先看查找的数据有没有用到索引,没有索引就加索引,有索引时候避免索引失效。
看下面两条语句
MySQL [release_test_oa]> EXPLAIN SELECT * FROM WORKFLOW_LOGS WHERE ID -100 < 300000; +----+-------------+---------------+------+---------------+------+---------+------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+---------------+------+---------------+------+---------+------+------+-------------+ | 1 | SIMPLE | WORKFLOW_LOGS | ALL | NULL | NULL | NULL | NULL | 3998 | Using where | +----+-------------+---------------+------+---------------+------+---------+------+------+-------------+
MySQL [release_test_oa]> EXPLAIN SELECT * FROM WORKFLOW_LOGS WHERE ID < 300000-100 ; +----+-------------+---------------+-------+---------------+---------+---------+------+------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+---------------+-------+---------------+---------+---------+------+------+-------------+ | 1 | SIMPLE | WORKFLOW_LOGS | range | PRIMARY | PRIMARY | 8 | NULL | 1999 | Using where | +----+-------------+---------------+-------+---------------+---------+---------+------+------+-------------+
上面那句没有使用到ID字段上的索引,而下面使用到了,所以要避免在列上进行表达式计算。