MySQL 查看优化后的SQL,为什么总是把整型判断条件前移了?

580 阅读1分钟

问题描述

MySQL版本:5.7.17-log 表结构

create table address
(
    id                int(11) unsigned auto_increment primary key,
    id_no              varchar(18)      default '' not null comment '身份证号',
    flow_id            varchar(24)      default '' not null comment '流水号',
    corp_name          varchar(100)     default '' not null comment '公司名称',
    city               varchar(30)      default '' not null comment '市',
    tel                varchar(13)                 null,
    type               int(11) unsigned default 0  not null comment '地址类别',
    test_int           int              default 3  null
);

create index idx_flow_id
    on address (flow_id);

create index idx_tel
    on address (tel);

其中,type表示类型,只有3个值1 2 null

原始SQL

select flow_id from api_address where  corp_name='北京市' and flow_id = '2342352352352353' and city = '北京' AND tel = '0571-63035037' and type = 1 and test_int = 503 ;

优化后的SQL

通过执行计划查看优化后的SQL如下:

select `address`.`flow_id` AS `flow_id` from `address` where ((`address`.`test_int` = 503) and (`address`.`type` = 1) and (`address`.`corp_name` = '北京市') and (`address`.`flow_id` = '2342352352352353') and (`api_address`.`city` = '北京') and (`address`.`tel` = '0571-63035037'))

不管是怎么调整 type与test_int列的顺序,这两个查询条件,优化后始终在最前面,并且出现的顺序是,哪个在后,哪个优化后在前。

执行计划
如果按照where后的条件执行顺序(从左到右),那这整形条件调整至前,也不是最优的啊,并且从执行计划来看,应该是走了索引之后,再走条件筛选的。

请问,这是为什么?