>showcreatetable rental; > explain select*from rental where rental_date >'2005-01-01'orderby rental_id;
# 可见type为index
2. 模拟Hash索引来优化查找速度
只能处理键值的全值匹配查找
所使用的Hash函数决定了索引键的大小
试验步骤
1 2 3 4 5 6 7 8 9 10 11 12 13
>showcreatetable film\G;
# 可以见列 `title` varchar(255) NOTNULL,
# 增加1列 >altertable film add title_md5 varchar(32); # 更新数据为md5的值 > update film set title_md5=md5(title); # 新建一个索引来模拟hash索引 >create index idx_md5 on film; # 通过下面语句,通过Btree索引模拟hash索引 > explain select*from film where title_md5=('EGG IGBY')and title='EGG IGBY';
3. 利用索引优化锁
索引可以减少锁定行数
索引可以加快处理速度,同时加快锁的释放
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
>showcreatetable actor\G;
# 删除索引idx_actor_last_name >drop index idx_actor_last_name on actor;
# 随意执行一个查询 > explain select*from actor where last_name='WOOD'\G;
# 开始优化,先执行一个事务 >begin; # 增加1个排查锁,其他SQL链接则无法访问. >select*from actor where last_name='WOOD'for update;
# 增加一个索引 >create index idx_last_name on actor(last_name); # 查看查询计划 > explain select*from actor where last_name='WOOD'\G;
select object_schema,object_name,index_name,b.`table_rows` from performanc_schema.table_io_waits_summary_by_index_usage a join information_schema.table b ON a.`object_shcema`=b.`object_schema` and a.`object_schema`=b.`object_name` where index_name isnotnull and count_star=0 orderby object_schema,object_name;