Mysql中组合索引排序问题

如果我们建立单列索引(A),实际上相当于在(A,ID)上建立了索引,其中ID为主键。这中情况下对于 where A=xx order by ID的查询是非常有帮助的。但是如果我们建立了复合索引(A,B),那么就相当于在(A,B,ID)上建立了索引,那么对于where A=xx order by ID这样的查询,就使用不到索引扫描排序,只能用filesort排序(using filesort)了。

字符条件强制要求必须使用字符类型参数匹配

背景

create table user  
(  
    user_id   bigint auto_increment comment '主键'  
        primary key,  
    source_key    varchar(30)      not null comment '结算事务编号',  
    tenant_id bigint default 0 not null ,  
    constraint user_u1  
        unique (tenant_id, source_key)  
)

select * from user where tenant_id  = 82058 and source_key = 470888; -- 只有租户id字段索引生效

select * from user where tenant_id  = 82058 and source_key = '470888'; -- tenant_id和source_key字段索引均生效

select * from user where user_id = 118099017;-- settle_id字段索引生效
select * from user where user_id = '118099017'; -- settle_id字段索引生效

总结

mysql查询时,针对字段索引命中场景,想要索引生效,查询条件中字符条件不可以使用Long类型参数进行匹配命中索引,Long类型条件可以使用字符类型参数进行匹配命中索引 字符条件强制要求必须使用字符类型参数匹配