我一张表中有两列,两个都是字符串,一个是字符串代表的日期startDay,格式诸如"2020-03-23",一个是字符串orderId,都是可null的
创建一个联合索引, 现在想要查
select * from table where startDay < '2020-03-23' and orderId is null
由于第一个列是范围索引,导致第二个列索引不可用,联合索引(startDay,orderId), 这样不行,因为startDay < '2020-03-23'的候选记录太多,这样性能很糟糕,所以想创建联合索引(orderId, startDay)
select * from table where orderId is null and startDay < '2020-03-23'
select * from table where orderId is not null and startDay < '2020-03-23'
但是不知道用这个(orderId,startDay)联合索引会让两个列都起作用嘛?
###可以.
实验如下:
create table user_test(
`id` bigint auto_increment COMMENT '主键ID',
`birthday` varchar(1024) null COMMENT '出生日期',
`name` varchar(1024) null COMMENT '姓名',
`country` varchar(1024) not null COMMENT '国家',
`city` varchar(1024) not null COMMENT '城市',
PRIMARY KEY (`id`),
KEY `IDX_BIRTHDAY_NAME` (`name`,`birthday`)
)ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用户信息';
INSERT user_test (`birthday`,`name`,`country`,`city`) values ('1990-03-01','Ben','china','shanghai'),('1993-03-01','Tom','china','shanghai'),('1998-03-01',null,'china','shanghai'),('1990-03-01','Ben','china','shanghai'),('2004-03-01',null,'china','shanghai');
explain select * from user_test where birthday > '1995-03-01' and name is null;
可以看到命中了联合索引
虽然MySQL可以在含有null的列上使用索引,但不代表null和其他数据在索引中是一样的。不建议列上允许为空。最好限制not null
,并设置一个默认值,比如0
和''
空字符串等,如果是datetime类型,可以设置成'1970-01-01 00:00:00'
这样的特殊值。对MySQL来说,null
是一个特殊的值,Conceptually, NULL means “a missing unknown value” and it is treated somewhat differently from other values
。比如:不能使用=,<,>
这样的运算符,对null
做算术运算的结果都是null
,count
时不会包括null
行等,null
比空字符串需要更多的存储空间等。
参考文章: