下面1和2的效率一样吗?1....
select value from riqi where (id=2 or id=3 or id=7 or id=8 or id=9) and articles is not null2....
select value from riqi where id=2 and articles is not null
select value from riqi where id=3 and articles is not null
select value from riqi where id=7 and articles is not null
select value from riqi where id=8 and articles is not null
select value from riqi where id=9 and articles is not null

解决方案 »

  1.   

    就是1....的一条sql语句 与 2....的5条sql语句 比较起来
    哪个更快一些
      

  2.   

    你的数据库版本,正常来说,现在高版本的数据库在这两条sql上,应该执行计划都是一样的。
      

  3.   

    mysql> explain extended select * from test where id=2
        -> union select * from test where id=3;
    +----+--------------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
    | id | select_type  | table      | type  | possible_keys | key     | key_len | ref   | rows | filtered | Extra |
    +----+--------------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
    |  1 | PRIMARY      | test       | const | PRIMARY       | PRIMARY | 4       | const |    1 |   100.00 |       |
    |  2 | UNION        | test       | const | PRIMARY       | PRIMARY | 4       | const |    1 |   100.00 |       |
    | NULL | UNION RESULT | <union1,2> | ALL   | NULL          | NULL    | NULL    | NULL  | NULL |     NULL |       |
    +----+--------------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
    3 rows in set, 1 warning (0.00 sec)mysql> explain extended select * from test where (id=2 or id=3 or id=7 or id=8 or id=9);
    +----+-------------+-------+------+---------------+------+---------+------+------+----------+-------------+
    | id | select_type | table | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra       |
    +----+-------------+-------+------+---------------+------+---------+------+------+----------+-------------+
    |  1 | SIMPLE      | test  | ALL  | PRIMARY       | NULL | NULL    | NULL |    5 |   100.00 | Using where |
    +----+-------------+-------+------+---------------+------+---------+------+------+----------+-------------+
    1 row in set, 1 warning (0.00 sec)