zoukankan      html  css  js  c++  java
  • MySQL 查询优化之 or

    当使用or的时候是不会用到索引的

    mysql> explain SELECT * FROM aladdin_resource WHERE  state = 1 OR state = 2;
    +----+-------------+------------------+------+---------------+------+---------+------+-------+-------------+
    | id | select_type | table            | type | possible_keys | key  | key_len | ref  | rows  | Extra       |
    +----+-------------+------------------+------+---------------+------+---------+------+-------+-------------+
    |  1 | SIMPLE      | aladdin_resource | ALL  | state         | NULL | NULL    | NULL | 59074 | Using where |
    +----+-------------+------------------+------+---------------+------+---------+------+-------+-------------+
    1 row in set (0.00 sec)
    

      解决办法就是用union替换or

     explain select * from aladdin_resource where state=1 union select * from aladdin_resource where state=2;
    +----+--------------+------------------+------+---------------+-------+---------+-------+-------+-------------+
    | id | select_type  | table            | type | possible_keys | key   | key_len | ref   | rows  | Extra       |
    +----+--------------+------------------+------+---------------+-------+---------+-------+-------+-------------+
    |  1 | PRIMARY      | aladdin_resource | ref  | state         | state | 2       | const |   383 | Using where |
    |  2 | UNION        | aladdin_resource | ref  | state         | state | 2       | const | 21370 | Using where |
    | NULL | UNION RESULT | <union1,2>       | ALL  | NULL          | NULL  | NULL    | NULL  |  NULL |             |
    +----+--------------+------------------+------+---------------+-------+---------+-------+-------+-------------+
    3 rows in set (0.05 sec)
    

      高下立判

  • 相关阅读:
    **没有规则可以创建“XXX”需要的目标“XXX”问题的解决方案
    牛逼博主
    tiny4412 busybox制作根文件系统rootfs nfs 挂载 ubuntu 14.04
    解决 mounting /dev/block/mmcblk0p1 on /sdcard failed
    如何在虚拟机上配置hadoop集群
    数据结构线性表
    a伪类,关于图片
    脱离标准文档流(2)---定位
    脱离标准文档流(1)---浮动
    初窥css---盒子以及盒子扩展
  • 原文地址:https://www.cnblogs.com/xiongji/p/3823403.html
Copyright © 2011-2022 走看看