關係數據庫 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    

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章