关系数据库 MySQL sql 优化 or

/*
Navicat MySQL Data Transfer

Source Server         : 192.168.254.128
Source Server Version : 50725
Source Host           : 192.168.254.128:3307
Source Database       : test

Target Server Type    : MYSQL
Target Server Version : 50725
File Encoding         : 65001

Date: 2019-03-23 01:08:44
*/

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for test
-- ----------------------------
DROP TABLE IF EXISTS `test`;
CREATE TABLE `test` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT '主键',
  `first_name` varchar(255) DEFAULT NULL COMMENT '姓',
  `second_name` varchar(255) DEFAULT NULL COMMENT '名',
  `code` varchar(255) DEFAULT NULL COMMENT '业务code',
  `other_id` int(11) DEFAULT NULL COMMENT '其他id',
  `des` varchar(255) DEFAULT NULL COMMENT '描述',
  PRIMARY KEY (`id`),
  UNIQUE KEY `code_index` (`code`) USING BTREE COMMENT '业务code唯一索引',
  KEY `name_index` (`first_name`,`second_name`) USING BTREE COMMENT '姓名组合索引',
  KEY `other_id_index` (`other_id`) USING BTREE COMMENT '其他id普通索引'
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;

-- ----------------------------
-- Records of test
-- ----------------------------
INSERT INTO `test` VALUES ('1', 'fa', 'sa', 'c1', '1', null);
INSERT INTO `test` VALUES ('2', 'fb', 'fb', 'c2', '2', null);

情况1

explain select * from test where id=1 or id=2

1    SIMPLE    test        range    PRIMARY    PRIMARY    4        2    100    Using where

情况2

explain select * from test where id=1 or other_id=2

1    SIMPLE    test        ALL    PRIMARY,other_id_index                2    75    Using where

情况3

explain select * from test where id=1 or des='2'

1    SIMPLE    test        ALL    PRIMARY                2    75    Using where

情况4

explain select * from test where first_name='1' or second_name='2'

1    SIMPLE    test        ALL    name_index                2    75    Using where

情况5

explain select * from test where other_id=1 or other_id=2

1    SIMPLE    test        ALL    other_id_index                2    100    Using where

情况6

explain select * from test where code='c1' or code='c2'

1    SIMPLE    test        ALL    code_index                2    100    Using where

优化

union /union all

情况2

explain select * from test where id=1 
union all
select * from test where other_id=2

1    PRIMARY    test        const    PRIMARY    PRIMARY    4    const    1    100    
2    UNION    test        ref    other_id_index    other_id_index    5    const    1    100  

情况5 

explain select * from test where other_id=1 
UNION all 
  select * from test where other_id=2

1    PRIMARY    test        ref    other_id_index    other_id_index    5    const    1    100    
2    UNION    test        ref    other_id_index    other_id_index    5    const    1    100    

情况6

explain 
select * from test where code='c1' 
UNION all
 select * from test where code='c2'

1    PRIMARY    test        const    code_index    code_index    258    const    1    100    
2    UNION    test        const    code_index    code_index    258    const    1    100    

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值