电脑疯子技术论坛|电脑极客社区

微信扫一扫 分享朋友圈

已有 1844 人浏览分享

MySQL 8 新特性之Invisible Indexes

[复制链接]
1844 0

这篇文章主要介绍了MySQL 8 新特性之Invisible Indexes 的相关资料,需要的朋友可以参考下


背景

索引是把双刃剑,在提升查询速度的同时会减慢DML的操作。毕竟,索引的维护需要一定的成本。
所以,对于索引,要加上该加的,删除无用的。前者是加法,后者是减法。但在实际工作中,
大家似乎更热衷于前者,而很少进行后者。究其原因,在于后者,难。难的不是操作本身,
而是如何确认一个索引是无用的。

如何确认无用索引

在不可见索引出现之前,大家可以通过sys.schema_unused_indexes来确定无用索引。
在MySQL 5.6中,即使没有sys库,也可通过该视图的基表来进行查询。

  1. mysql> show create table sys.schema_unused_indexes\G
  2. *************************** 1. row ***************************
  3.         View: schema_unused_indexes
  4.     Create View: CREATE ALGORITHM=MERGE DEFINER=`mysql.sys`@`localhost` SQL
  5. SECURITY INVOKER VIEW `sys`.`schema_unused_indexes` (
  6. `object_schema`,`object_name`,`index_name`) AS select `t`.`OBJECT_SCHEMA` AS `object_
  7. schema`,`t`.`OBJECT_NAME` AS `object_name`,`t`.`INDEX_NAME` AS `index_name` from (`perfor
  8. mance_schema`.`table_io_waits_summary_by_index_usage` `t` join `information_schema`.`STATIS
  9. TICS` `s` on(((`t`.`OBJECT_SCHEMA` = convert(`s`.`TABLE_SCHEMA` using utf8mb4)) and (`t`.`OBJE
  10. CT_NAME` = convert(`s`.`TABLE_NAME` using utf8mb4)) and (convert(`t`.`INDEX_NAME` using utf8) = `s`
  11. .`INDEX_NAME`)))) where ((`t`.`INDEX_NAME` is not null) and (`t`.`COUNT_STAR` = 0) and (`t`.`OBJEC
  12. T_SCHEMA` <> 'mysql') and (`t`.`INDEX_NAME` <> 'PRIMARY') and (`s`.`NON_UNIQUE` = 1) and
  13. (`s`.`SEQ_IN_INDEX` = 1)) order by `t`.`OBJECT_SCHEMA`,`t`.`OBJECT_NAME`character_set_client: utf8mb4
  14. collation_connection: utf8mb4_0900_ai_ci
  15. 1 row in set, 1 warning (0.00 sec)
复制代码


但这种方式也有不足,

1. 如果实例发生重启,performance_schema中的数据就会清零。

2. 如果基于上面的查询删除了索引,查询性能突然变差,怎么办?

不可见索引的出现,可有效弥补上述不足。将index设置为invisible,
会导致优化器在选择执行计划时,自动忽略该索引,即便使用了FORCE INDEX。

当然,这个是由optimizer_switch变量中use_invisible_indexes选项决定的,默认为off。
如果想看一个查询在索引调整前后执行计划的差别,
可在会话级别调整use_invisible_indexes的值,如,

  1. mysql> show create table slowtech.t1\G
  2. *************************** 1. row ***************************
  3.    Table: t1
  4. Create Table: CREATE TABLE `t1` (
  5. `id` int(11) NOT NULL,
  6. `name` varchar(10) DEFAULT NULL,
  7. PRIMARY KEY (`id`),
  8. KEY `idx_name` (`name`) /*!80000 INVISIBLE */
  9. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
  10. 1 row in set (0.00 sec)
  11. mysql> explain select * from slowtech.t1 where name='a';
  12. +----+-------------+-------+------------+------+---------------+------
  13. +---------+------+------+----------+-------------+
  14. | id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra   |
  15. +----+-------------+-------+------------+------+---------------+------
  16. +---------+------+------+----------+-------------+
  17. | 1 | SIMPLE   | t1  | NULL   | ALL | NULL     | NULL | NULL  | NULL |  6 |  16.67 | Using where |
  18. +----+-------------+-------+------------+------+---------------+------+
  19. ---------+------+------+----------+-------------+
  20. 1 row in set, 1 warning (0.00 sec)
  21. mysql> set session optimizer_switch="use_invisible_indexes=on";
  22. Query OK, 0 rows affected (0.00 sec)

  23. mysql> explain select * from slowtech.t1 where name='a';
  24. +----+-------------+-------+------------+------+---------------+------
  25. ----+---------+-------+------+----------+-------------+
  26. | id | select_type | table | partitions | type | possible_keys | key   | key_l
  27. en | ref | rows | filtered | Extra   |
  28. +----+-------------+-------+------------+------+---------------+----------
  29. +---------+-------+------+----------+-------------+
  30. | 1 | SIMPLE   | t1  | NULL   | ref | idx_name   | idx_name | 43   | const |  1 | 100.00 | Using index |
  31. +----+-------------+-------+------------+------+---------------+----------
  32. +---------+-------+------+----------+-------------+
  33. 1 row in set, 1 warning (0.00 sec)
复制代码


不可见索引的常见操作

  1. create table t1(id int primary key,name varchar(10),index idx_name (name) invisible);
  2. alter table t1 alter index idx_name visible;
  3. alter table t1 alter index idx_name invisible;
复制代码


如何查看哪些索引不可见

  1. mysql> select table_schema,table_name,index_name,column_name,is_visible
  2. from information_schema.statistics where is_visible='no';
  3. +--------------+------------+------------+-------------+------------+
  4. | TABLE_SCHEMA | TABLE_NAME | INDEX_NAME | COLUMN_NAME | IS_VISIBLE |
  5. +--------------+------------+------------+-------------+------------+
  6. | slowtech  | t1    | idx_name | name    | NO    |
  7. +--------------+------------+------------+-------------+------------+
  8. 1 row in set (0.00 sec)
复制代码


注意

1. 主键索引不可被设置为invisible。

总结

以上所述是小编给大家介绍的MySQL 8 新特性之Invisible Indexes ,希望对大家有所帮助!




您需要登录后才可以回帖 登录 | 注册

本版积分规则

1

关注

0

粉丝

9021

主题
精彩推荐
热门资讯
网友晒图
图文推荐

Powered by Pcgho! X3.4

© 2008-2022 Pcgho Inc.