招聘网站排行榜2021,激光东莞网站建设,网站类型分类有哪些,免费域名注册教程三种新的索引方式1、隐藏索引MySQL8.0 支持隐藏索引(invisible index)#xff0c;不可见索引隐藏索引不会被优化器使用#xff0c;但需要维护。应用场景#xff1a;软删除、灰度发布。软删除#xff1a;不确定当前索引是否需要删除的时候#xff0c;软删除#xff0c;不会…三种新的索引方式1、隐藏索引MySQL8.0 支持隐藏索引(invisible index)不可见索引隐藏索引不会被优化器使用但需要维护。应用场景软删除、灰度发布。软删除不确定当前索引是否需要删除的时候软删除不会彻底删除可以恢复索引不需要重新创建但需要维护。灰度发布测试索引对当前数据库不会参生太多影响确认有效后可以取消隐藏改变为正常索引。操作create table app_user (pkid int,age int);-- 正常索引create index age_idx on app_user(age) ;-- 隐藏索引 主键不可以设置尾隐藏索引create index id_idx on app_user(pkid) invisible;-- 有一个参数Visible为NOshow index from app_user;-- 查询优化器对索引的使用情况-- 会使用索引explain select * from app_user where age18;-- 不会使用索引explain select * from app_user where pkid1;-- 查询优化器的隐藏索引的开关select optimizer_switch\G-- 查询优化器使用隐藏索引在当前会话中set session optimizer_switchuse_invisible_indexeson;-- 打开之后可以使用索引explain select * from app_user where pkid1;-- 设置索引可见alter table app_user index id_idx visiblle;-- 设置索引隐藏alter table app_user index id_idx invisiblle;2、降序索引MySQL8.0真正支持降序索引(descending index)。只有InnoDB存储引擎支持降序索引只支持BTREE降序索引。MySQL8.0不再对GROUP BY操作进行隐式排序也就是说排序必须要使用ORDER BY。操作create table app_dept(pkid int,num int,cou int,index idx1(num asc,cou desc));-- 在5.7中是没有desc的只有8.0才会有descshow cteate table app_dept\Ginsert into app_dept values(1,1,300),(2,6,500),(5,1,256),(3,4,400);-- 查询优化器使用索引的情况会发现使用当前索引但不用额外的排序(using filesort)操作explain select * from app_dept order by num,cou desc;-- 反顺序查询只会出现反向索引扫描(backward index scan)不会重新排序explain select * from app_dept order by num desc,cou ;-- GROUP BY 没有默认排序select count(*) ,cou from app_dept group by cou;3、函数索引MySQL8.0支持在索引中使用函数(表达式)的值。支持降序索引支持JSON数据索引。函数索引基于虚拟列功能实现。create table t1(c1 varchar(100),c2 varchar(100));create index idx1 on t1(c1);-- 创建函数索引create index fun_idx2 on t1(UPPER(c1);show index from t1\G-- 当使用函数时候就不会走当前普通索引explain select * from t1 where upper(c1)A;-- 走当前函数索引explain select * from t1 where upper(c2)A;-- 添加一个 计算列,并未该列实现索引虚拟列实现函数索引alter table t1 add column c3 varchar(100) generated always as (upper(c1));-- 创建JSON数据索引测试data-$.name as char(30) 意思是取当前name值类型尾charcreate table emp(data json,index((CAST(data-$.name as char(30)))));show index from emp\G-- 当前就会使用JSON索引explain select * from emp where CAST(data-$.name as char(30))A;