---
title: OceanBase 数据库支持的不可见索引介绍-OceanBase数据库使用指南
description: 了解OceanBase数据库在实际应用中关于OceanBase 数据库支持的不可见索引介绍相关的常见问题和使用技巧，帮助您快速解决OceanBase 数据库支持的不可见索引介绍的难题。
---
切换语言

- 中文站 - 简体中文
- International - English
- 日本站 - 日本語

划线反馈

# OceanBase 数据库支持的不可见索引介绍

更新时间：2026-05-22 08:56

适用版本： V2.2.x、V3.1.x、V3.2.x 内容类型：TechNote  

本文介绍 OceanBase 数据库 MySQL 模式中不可见索引的使用方法。

## 适用版本

OceanBase 数据库所有版本。

## 不可见索引

INVISIBLE 是索引的一种状态。当索引是 INVISIBLE 状态时，SQL 优化器将不会选择该索引。创建索引时，索引默认是可见的。主键不可修改为 INVISIBLE 状态。

OceanBase 数据库中 INVISIBLE 索引与 MySQL 8.0 版本类似，不同点是 OceanBase 数据库没有 MySQL 中的 `use_invisible_indexes` 参数用于开启或关闭不可见索引。在 OceanBase 数据库中，可以通过 ALTER TABLE 语句修改索引是否可见。

修改索引属性的语法如下所示。

```unknow
ALTER TABLE <table_name> ALTER INDEX <index_name> [ VISIBLE | INVISIBLE ];

```

可以通过 MySQL 的方式查询 `information_schema.statistics` 查询索引状态是否为 INVISIBLE。

```unknow
obclient> SELECT * FROM information_schema.statistics WHERE is_visible='NO';
+---------------+--------------+------------+------------+--------------+------------+--------------+-------------+-----------+-------------+----------+--------+----------+------------+-----------+---------------+------------+
| TABLE_CATALOG | TABLE_SCHEMA | TABLE_NAME | NON_UNIQUE | INDEX_SCHEMA | INDEX_NAME | SEQ_IN_INDEX | COLUMN_NAME | COLLATION | CARDINALITY | SUB_PART | PACKED | NULLABLE | INDEX_TYPE | COMMENT   | INDEX_COMMENT | IS_VISIBLE |
+---------------+--------------+------------+------------+--------------+------------+--------------+-------------+-----------+-------------+----------+--------+----------+------------+-----------+---------------+------------+
| def           | test         | fruit      |          1 | test         | fruit_ind  |            1 | id2         | A         |        NULL | NULL     | NULL   | YES      | BTREE      | available |               | NO         |
+---------------+--------------+------------+------------+--------------+------------+--------------+-------------+-----------+-------------+----------+--------+----------+------------+-----------+---------------+------------+
1 row in set (0.03 sec)

```

也可以查询内部表 `oceanbase.__tenant_virtual_table_index` 来获得该统计信息。

```unknow
obclient> SELECT * FROM oceanbase.__tenant_virtual_table_index WHERE is_visible='NO';
+------------------+-----------+--------------+--------------+-------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+-----------+---------------+------------+
| table_id         | key_name  | seq_in_index | table_schema | table | non_unique | index_schema | column_name | collation | cardinality | sub_part | packed | null | index_type | comment   | index_comment | is_visible |
+------------------+-----------+--------------+--------------+-------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+-----------+---------------+------------+
| 1102810162709354 | fruit_ind |            1 | test         | fruit |          1 | test         | id2         | A         |        NULL | NULL     | NULL   | YES  | BTREE      | available |               | NO         |
+------------------+-----------+--------------+--------------+-------+------------+--------------+-------------+-----------+-------------+----------+--------+------+------------+-----------+---------------+------------+
1 row in set (0.00 sec)

```

## 测试步骤

1. 创建测试表，在 id 列创建主键，在 id2 列创建索引 fruit_ind，并向表中插入测试数据。

   ```unknow
   obclient> CREATE TABLE fruit (id int,id2 int,name varchar(20), PRIMARY KEY (id), index fruit_ind(id2));
   Query OK, 0 rows affected (0.09 sec)

   obclient> INSERT INTO fruit VALUES (1,1,'apple'),(2,3,'banana'),(3,9,'peach');
   Query OK, 3 rows affected (0.01 sec)
   Records: 3  Duplicates: 0  Warnings: 0));

   ```
 2. 通过 WHERE 条件查询 id2 列，并显示执行计划。

   此时查询计划的 NAME 列为 `fruit(fruit_ind)`，说明查询使用索引。

   ```unknow
   obclient> EXPLAIN SELECT id2 FROM fruit WHERE id2>2\G
   *************************** 1. row ***************************
   Query Plan: ===============================================
   |ID|OPERATOR  |NAME            |EST. ROWS|COST|
   -----------------------------------------------
   |0 |TABLE SCAN|fruit(fruit_ind)|2        |37  |
   ===============================================

   Outputs & filters:
   -------------------------------------
     0 - output([fruit.id2]), filter(nil),
         access([fruit.id2]), partitions(p0)

   1 row in set (0.00 sec)

   ```

   #### 注意

   由于数据量较小，因此只有查询索引所在的列，并且查询中具有过滤条件，才能保证查询使用索引。
 3. 将索引 fruit_ind 设置为 INVISIBLE，并再次执行步骤 2 中的查询。

   此时查询计划的 NAME 列为 `fruit`，说明查询未使用索引，而是在 fruit 表上进行了表扫描。

   ```unknow
   obclient> ALTER TABLE fruit ALTER INDEX fruit_ind INVISIBLE;
   Query OK, 0 rows affected (0.05 sec)

   obclient> EXPLAIN SELECT id2 FROM fruit WHERE id2>2\G
   *************************** 1. row ***************************
   Query Plan: ====================================
   |ID|OPERATOR  |NAME |EST. ROWS|COST|
   ------------------------------------
   |0 |TABLE SCAN|fruit|1        |37  |
   ====================================

   Outputs & filters:
   -------------------------------------
     0 - output([fruit.id2]), filter([fruit.id2 > 2]),
         access([fruit.id2]), partitions(p0)

   ```

## 注意事项

- OceanBase 数据库 V1.4.7x 版本新建索引后，需要集群合并后才使得索引生效。如果使用唯一索引，需要集群合并 2 次后索引才能生效。
 - OceanBase 数据库 V2.X 及后续版本的索引会直接在静态的 SSTable 中构建，直接生效不需要等待合并。

上一篇

[分区表全局索引回表显示 scan 所有 partition 问题](https://www.oceanbase.com/knowledge-base/oceanbase-database-1000000000451215)

下一篇

[如何导出所有索引的定义信息](https://www.oceanbase.com/knowledge-base/oceanbase-database-1000000000207720) ![有帮助](https://gw.alipayobjects.com/mdn/ob_asset/afts/img/A*y6ocSqN8cqsAAAAAAAAAAAAAARQnAQ)![无帮助](https://gw.alipayobjects.com/mdn/ob_asset/afts/img/A*BG9IQJyLHF8AAAAAAAAAAAAAARQnAQ)![反馈](https://gw.alipayobjects.com/mdn/ob_asset/afts/img/A*eTWdQKCRKHwAAAAAAAAAAAAAARQnAQ)[AI](https://www.oceanbase.com/obi) 咨询热线
