mysql-索引相关

CREATE TABLE `blog`  (
  `bid` bigint NOT NULL,
  `name` varchar(255)  NULL DEFAULT NULL,
  `author_id` int NOT NULL,
  `insert_time` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`bid`),
  INDEX `name_author_id_index`(`name`, `author_id`) 
) ENGINE = InnoDB;

INSERT INTO blog(bid,name,author_id,insert_time)VALUES(1475797674679549853,"cc",3,CURRENT_TIMESTAMP);

#创建索引
create INDEX author_id_index on blog(author_id);

create INDEX name_author_id_index on blog(name,author_id);
#删除索引
drop INDEX b_id_author_id_index on blog
drop INDEX author_id_index on blog
#查看索引
show index from blog;
#执行解释 extra 表示是否覆盖索引 意思就是不用回表
EXPLAIN SELECT * from blog;

#索引下推 mysql5.6以上才有 #5.6之前根据name_author_id_index联合索引查询所有满足名称以“a”开头的索引,然后回表查询出相应的全行数据,然后再筛选出满足author_id等于3的用户数据
#5.6之后根据name_author_id_index查询所有满足名称以“a”开头的索引,然后直接再author_id=2的索引,之后再回表
#extra=Using index condition 表示使用了索引下推
EXPLAIN SELECT * from blog where name like "a%" and author_id=2;

#索引失效
#查询条件中有or (type=ALL,全表扫描)
EXPLAIN SELECT * from blog where name like "a%" or author_id=2;

#like查询是以%开头
EXPLAIN SELECT * from blog where name like "%a%" ;

#索引列上参与计算会导致索引失效
EXPLAIN SELECT * from blog where author_id =3-1 ;

#违背最左匹配原则
EXPLAIN SELECT * from blog where author_id =3;

你可能感兴趣的:(mysql,mysql,数据库,sql)