0

如何使用 OR、AND 子句改进查询?

explain 
       select fileds 
       from table_name 
       where (fldlp>='2012-09-15 13:00:34' and fldlp<='2012-09-16 06:00:02') 
    or (flexp>='2012-09-15 13:00:34' and flexp<='2012-09-16 06:00:02');

+----+-------------+------------+------+---------------+------+---------+------+---------+-------------+
| id | select_type | table      | type | possible_keys | key  | key_len | ref  | rows    | Extra       |
+----+-------------+------------+------+---------------+------+---------+------+---------+-------------+
|  1 | SIMPLE      | table_name | ALL  | NULL          | NULL | NULL    | NULL | 2928790 | Using where | 
+----+-------------+------------+------+---------------+------+---------+------+---------+-------------+
4

2 回答 2

4

您可以使用BETWEEN子句。

WHERE (fldlp BETWEEN '2012-09-15 13:00:34' AND '2012-09-16 06:00:02') 
   OR (flexp BETWEEN '2012-09-15 13:00:34' AND '2012-09-16 06:00:02');
于 2012-09-18T10:37:57.367 回答
4

改进的技巧

  • fldlp在和上创建索引flexp

  • 上述列的数据类型应为dateTime

于 2012-09-18T10:41:45.490 回答