1.对多列进行索引(组合索引),列的顺序很重要,MySQL仅能对索引最左边的前缀进行有效的查找。
2.explain显示了mysql如何使用索引来处理select语句以及连接表
3.创建组合索引:
create index index_id on test(name,phone);
4.2.MySQL下创建一张表test,含有5个字段(id,name,age,phone,address),选择对应的数据类型创建该表,任意插入3行数据。创建name与phone的组合索引。
– 新建一张表test
create table test(
id int(3) primary key not null,
name varchar(10) not null,
age int(3),
phone char(11),
address varchar(30)
);
– 添加第1条数据
insert into test(id,name,age,phone,address)
values
(1,“张一”,18,11111111111,“中国北京”);
– 添加第2条数据
insert into test(id,name,age,phone,address)
values
(2,“张二”,19,22222222222,“中国上海”);
– 添加第3条数据
insert into test(id,name,age,phone,address)
values
(3,“张三”,20,“33333333333”,“中国台湾”);
– 创建复合索引
create index index_id on test(name,phone);
select * from test where name = “张二” and phone=“22222222222” ;
explain select * from test where name = “张二” and phone=“22222222222” ;

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐