首页 > 试题广场 >

Mysql中表user的建表语句如下, CREATE TAB

[单选题]
Mysql中表user的建表语句如下,
CREATE TABLE `user` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键Id',
  `name` varchar(255) DEFAULT NULL COMMENT '名称',
  `age` int(11) DEFAULT NULL COMMENT '年龄',
  `address` varchar(255) DEFAULT NULL COMMENT '地址',
  `created_time` datetime DEFAULT NULL COMMENT '创建时间',
  `updated_time` datetime DEFAULT NULL COMMENT '更新时间',
  PRIMARY KEY (`id`),
  KEY `idx_com1` (`name`,`age`,`address`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用户表';
以下哪个查询语句没有使用到索引idx_com1?
  • select  *  from user where name='张三' and age = 25 and address='北京大兴区';
  • select  *  from user where name='张三' and address='北京大兴区';
  • select  *  from user where age = 25 and address='北京大兴区';
  • select  *  from user where address='北京大兴区'  and age = 25 and name='张三'
注意索引 idx_com1 的顺序 `name`,`age`,`address`
B选项name属性是有序的,在name有序的情况下,加上address属性是会用到索引的
C选项age,address属性是无序的,所以只能是一行一行去查找,不会用到索引
这就是mysql索引中所谓的最左匹配原则。
发表于 2019-05-29 17:09:05 回复(0)

答案应该选B、C吧

KEY(a,b,c) //创建索引
//生效
WHERE a=1 and b=2 and c=3
WHERE a=1 and b=2
WHERE a=1
WHERE c=3 and b=2 and a=1 //乱序也是可以生效的

//无效
WHERE b=2 and c=3 //跳过了1 
WHERE a=1 and c=3 //跳过了2
发表于 2019-04-11 14:30:21 回复(0)
最左前缀树匹配以及where语句的重排。
比如a = 1 and b = 2 and c = 3 建立(a,b,c)索引可以任意顺序,mysql的查询优化器会帮你优化成索引可以识别的形式。
发表于 2019-02-16 14:52:15 回复(0)