show index from 表名; # 主键列会自动创建索引
-- 创建索引的语法格式 -- alter table 表名 add index 索引名[可选](列名, ..) -- 给name字段添加索引 alter table classes add index my_name (name); # 索引名不指定,默认使用字段名
-- 删除索引的语法格式 -- alter table 表名 drop index 索引名 -- 如果不知道索引名,可以查看创表sql语句 show create table classes; alter table classes drop index my_name; - 开启运行时间监测: set profiling=1; -- 查找第1万条数据ha-99999 select * from test_index where title=‘ha-99999‘; -- 查看执行的时间: show profiles;
- 创建teacher表 create table teacher ( id int not null primary key auto_increment, name varchar(10), age int ); -- 创建联合索引 alter table teacher add index (name,age);
alter table xxx add index (a,b,c)
则可用索引分别为a,ab,abc
-- 下面的查询使用到了联合索引 select * from stu where name=‘张三‘ -- 这里使用了联合索引的name部分 select * from stu where name=‘李四‘ and age=10 -- 这里完整的使用联合索引,包括 name 和 age 部分 -- 下面的查询没有使用到联合索引 select * from stu where age=10 -- 因为联合索引里面没有这个组合,只有 name | name age 这两种组合
原文:https://www.cnblogs.com/tanhuan-share/p/13066372.html