索引
注意
要注意的是,建立太多的索引将会影响更新和插入的速度,因为它需要同样更新每个索引文件。
对于一个经常需要更新和插入的表格,就没有必要为一个很少使用的where字句单独建立索引了,
对于比较小的表,排序的开销不会很大,也没有必要建立另外的索引。
建立索引会占用磁盘空间
索引最主要解决的问题
创建一张表
create table test_index(title varchar(10));
--测试步骤
1. 开启运行时间监测:
set profiling=1;
2. 查找第1万条数据ha-99999
select * from test_index where title='ha-99999';
3. 查看执行的时间:
show profiles;
4. 为表title_index的title列创建索引:
create index 索引名称 on 表名(字段名称)
create index my_index on test_index(title);
5. 执行查询语句:
select * from test_index where title='ha-99999';
6. 再次查看执行的时间
show profiles;
--查看索引
-- show index from 表名;
show index from test_index;
--删除索引
---drop index 索引名 on 表名;
drop index my_index on test_index;