MySQL 8.0 Invisible Indexes 和 RDS 5.6 Invisible Indexes介绍

本文涉及的产品
云数据库 RDS MySQL Serverless,0.5-2RCU 50GB
简介: mysql 在8.0的时候支持了不可见索引,称为隐式索引 索引默认是可以的,控制索引的可见性可以使用Invisible,visible关键字作为create table,create index,alter table 来进行定义。

mysql 在8.0的时候支持了不可见索引,称为隐式索引
索引默认是可以的,控制索引的可见性可以使用Invisible,visible关键字作为create table,create index,alter table 来进行定义。

RDS 5.6 Invisible Indexes

也是最近刚刚上线的功能。新购买实例目前已经支持,老版本实例,需要进行升级。
那么接下来测试一下。

查看数据库版本:

mysql>select version();
+-----------+
| version() |
+-----------+
| 5.6.16-log    |
+-----------+
1 row in set (0.00 sec)

查看表结构以及数据量

  CREATE TABLE `employees` (
    `emp_no` int(11) NOT NULL,
    `birth_date` date NOT NULL,
    `first_name` varchar(14) NOT NULL,
    `last_name` varchar(16) NOT NULL,
    `gender` enum('M','F') NOT NULL,
    `hire_date` date NOT NULL,
    PRIMARY KEY (`emp_no`),
    KEY `idx_a1` (`birth_date`)
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8

  mysql>select count(*) from employees;
  +----------+
  | count(*) |
  +----------+
  |   300024 |
  +----------+
  1 row in set (0.33 sec)

新增索引first_name

mysql>alter table employees add index idx_firstname(first_name);执行成功,花费 1034 ms.

测试:

a.显式索引测试:
image
b.将first_name 设置成隐藏索引
mysql>alter table employees alter index idx_firstname INVISIBLE;
执行成功,花费 6 ms.
image
c.将隐藏索引修改为显示索引
mysql>alter table employees alter index idx_firstname VISIBLE;
执行成功,花费 6 ms.
image

MySQL 8.0 Invisible Indexes

查看版本号:

root@my3308.sock-8.0.11>[employees]>select version();
+-----------+
| version() |
+-----------+
| 8.0.11    |
+-----------+
1 row in set (0.00 sec)

查看表结构以及使用create index alter index 创建INVISIBLE index

root@my3308.sock-8.0.11>[employees]>create index  idx_first_name on  employees(first_name) INVISIBLE;
Query OK, 0 rows affected (1.75 sec)
Records: 0  Duplicates: 0  Warnings: 0

 root@my3308.sock-8.0.11>[employees]>alter table employees add index idx_last_name(last_name) INVISIBLE;
Query OK, 0 rows affected (1.51 sec)
Records: 0  Duplicates: 0  Warnings: 0

使用alter index ... 命令修改 索引的可见性:

  root@my3308.sock-8.0.11>[employees]>explain  select * from employees where first_name='Shahab';
  +----+-------------+-----------+------------+------+---------------+------+---------+------+--------+----------+-------------+
  | id | select_type | table     | partitions | type | possible_keys | key  | key_len | ref  | rows   | filtered | Extra       |
  +----+-------------+-----------+------------+------+---------------+------+---------+------+--------+----------+-------------+
  |  1 | SIMPLE      | employees | NULL       | ALL  | NULL          | NULL | NULL    | NULL | 299290 |    10.00 | Using where |
  +----+-------------+-----------+------------+------+---------------+------+---------+------+--------+----------+-------------+
  1 row in set, 1 warning (0.00 sec)

  root@my3308.sock-8.0.11>[employees]>alter table employees alter index idx_first_name VISIBLE;
  Query OK, 0 rows affected (0.02 sec)
  Records: 0  Duplicates: 0  Warnings: 0

  root@my3308.sock-8.0.11>[employees]>explain  select * from employees where first_name='Shahab';
  +----+-------------+-----------+------------+------+----------------+----------------+---------+-------+------+----------+-------+
  | id | select_type | table     | partitions | type | possible_keys  | key            | key_len | ref   | rows | filtered | Extra |
  +----+-------------+-----------+------------+------+----------------+----------------+---------+-------+------+----------+-------+
  |  1 | SIMPLE      | employees | NULL       | ref  | idx_first_name | idx_first_name | 44      | const |  295 |   100.00 | NULL  |
  +----+-------------+-----------+------------+------+----------------+----------------+---------+-------+------+----------+-------+
  1 row in set, 1 warning (0.00 sec)
  
  root@my3308.sock-8.0.11>[employees]>alter table employees alter index idx_first_name INVISIBLE;
  Query OK, 0 rows affected (0.02 sec)
  Records: 0  Duplicates: 0  Warnings: 0

  root@my3308.sock-8.0.11>[employees]>explain  select * from employees where first_name='Shahab';
  +----+-------------+-----------+------------+------+---------------+------+---------+------+--------+----------+-------------+
  | id | select_type | table     | partitions | type | possible_keys | key  | key_len | ref  | rows   | filtered | Extra       |
  +----+-------------+-----------+------------+------+---------------+------+---------+------+--------+----------+-------------+
  |  1 | SIMPLE      | employees | NULL       | ALL  | NULL          | NULL | NULL    | NULL | 299290 |    10.00 | Using where |
  +----+-------------+-----------+------------+------+---------------+------+---------+------+--------+----------+-------------+
  1 row in set, 1 warning (0.00 sec)

可以从INFORMATION_SCHEMA.STATISTICS表中获取索引的属性

root@my3308.sock-8.0.11>[employees]>SELECT INDEX_NAME, IS_VISIBLE        
    FROM INFORMATION_SCHEMA.STATISTICS        
    WHERE TABLE_SCHEMA = 'employees' and TABLE_NAME='employees';
+----------------+------------+
| INDEX_NAME     | IS_VISIBLE |
+----------------+------------+
| idx_first_name | NO         |
| idx_last_name  | NO         |
| PRIMARY        | YES        |
+----------------+------------+
3 rows in set (0.00 sec)

对于NOT NULL UNIQUE 的约束索引没有显示的主键时,是不可以直接设置成INVISIBLE 属性,

root@my3308.sock-8.0.11>[employees]>CREATE TABLE t2 (
    ->   i INT NOT NULL,
    ->   j INT NOT NULL,
    ->   UNIQUE j_idx (j)
    -> ) ENGINE = InnoDB;
Query OK, 0 rows affected (0.03 sec)

root@my3308.sock-8.0.11>[employees]>ALTER TABLE t2 ALTER INDEX j_idx INVISIBLE;
ERROR 3522 (HY000): A primary key index cannot be invisible

root@my3308.sock-8.0.11>[employees]>ALTER TABLE t2 ADD PRIMARY KEY (i);
Query OK, 0 rows affected (0.10 sec)
Records: 0  Duplicates: 0  Warnings: 0

root@my3308.sock-8.0.11>[employees]>ALTER TABLE t2 ALTER INDEX j_idx INVISIBLE;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0
相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
1月前
|
存储 关系型数据库 MySQL
RDS MySQL 数据库运维简述
从运维的视角,汇总云数据库RDS MySQL使用的避坑指南。文章初版,维护更新,欢迎指点。
761 3
|
1月前
|
SQL 存储 关系型数据库
【MySQL 数据库】11、学习 MySQL 中的【锁】
【MySQL 数据库】11、学习 MySQL 中的【锁】
76 0
|
21天前
|
SQL 关系型数据库 MySQL
【MySQL技术专题】「问题实战系列」深入探索和分析MySQL数据库的数据备份和恢复实战开发指南(8.0版本升级篇)
【MySQL技术专题】「问题实战系列」深入探索和分析MySQL数据库的数据备份和恢复实战开发指南(8.0版本升级篇)
94 0
|
1月前
|
SQL 关系型数据库 MySQL
【MySQL 数据库】4、MySQL 事务学习
【MySQL 数据库】4、MySQL 事务学习
44 0
|
2天前
|
SQL 关系型数据库 MySQL
MySQL环境搭建——“MySQL数据库”
MySQL环境搭建——“MySQL数据库”
|
2天前
|
SQL NoSQL 关系型数据库
初识MySQL数据库——“MySQL数据库”
初识MySQL数据库——“MySQL数据库”
|
21天前
|
SQL 关系型数据库 MySQL
【MySQL技术专题】「问题实战系列」深入探索和分析MySQL数据库的数据备份和恢复实战开发指南(数据恢复补充篇)(一)
【MySQL技术专题】「问题实战系列」深入探索和分析MySQL数据库的数据备份和恢复实战开发指南(数据恢复补充篇)
29 0
|
29天前
|
关系型数据库 MySQL Serverless
RDS MySQL Serverless
阿里云新推出RDS MySQL Serverless,提供实时弹性资源,按需设置范围,自动适应负载变化,实现资源优化与成本降低。用户可通过控制台或API轻松创建实例,无缝应对低负载至高负载场景,实现自动弹性扩缩容。该服务适合各种云数据库应用场景,兼具成本优化和高灵活性。【2月更文挑战第29天】
32 1
|
1月前
|
存储 SQL 关系型数据库
【MySQL 数据库】10、MySQL 的触发器
【MySQL 数据库】10、MySQL 的触发器
20 0
|
11天前
|
关系型数据库 MySQL 数据库
mysql卸载、下载、安装(window版本)
mysql卸载、下载、安装(window版本)