1.什么是组合索引
创建的索引时,添加的索引至少包含两列的就是组合索引,组合索引遵循最左前缀,通常将最常用的列放到最左边,组合索引实际上不是一个索引,而是多个
creat index index_name on table_name (column1,column2,column3)
-- 实际创建的索引有 (column1),(colume1,column2),(,colume1,column2,column3)
-- 举例:
CREATE index mul on t_user (id,username,type)
-- 只有以下三种情况查询走索引
EXPLAIN select * from t_user where id =1
EXPLAIN select * from t_user where id =1 and username = 'admin'
EXPLAIN select * from t_user where id =1 and username = 'admin' and type =1
-- 索引可以加快查询但是会降低表更新速度
-- 大数据量内容的字段添加索引可能导致索引失效
-- order by(主键)性能更好,使用其他索引排序,不能提升性能
2.性能级别
system>const>eq_ref>ref>range>index>ALL,其中
1.system,const为理想状态,system出现的情况为只有一条数据的系统表或衍生表只能有一条数据的主查询
2.const出现的情况为,仅仅能查出一条的SQL语句并且用于Primary key 或 unique索引
3.eq_ref常见的情况为唯一索引查询和主键索引查询
4.ref 非唯一性索引
5.range 搜索一定范围
6.index 查询全部索引中的数据
7.all 全表查询