OB 表默认索引问题

https://open.oceanbase.com/blog/13725359494
该链接中提到 mysql模式下,对于分区表,创建唯一索引时,如果指定了 LOCAL 关键字,则创建的是局部索引;如果指定了GLOBAL 关键字,则创建的是全局索引。
如果未指定 LOCAL 或 GLOBAL 关键字,默认创建的是全局索引。

测试环境(单zone 单机):https://www.oceanbase.com/demo/query-tenant-info
测试结果:分区表如果未指定 LOCAL 或 GLOBAL 关键字,默认创建的是 局部索引。 这个测试结果与上述链接中提到的“如果未指定 LOCAL 或 GLOBAL 关键字,默认创建的是 全局索引”不符! 这是什么原因导致的??????

测试内容如下:
[root@iZbp1eelqbnunqahxk48v8Z ~]#
[root@iZbp1eelqbnunqahxk48v8Z ~]#
[root@iZbp1eelqbnunqahxk48v8Z ~]# obclient -h127.0.0.1 -uroot@sys -P2881 -Dtestdb -A
Welcome to the OceanBase. Commands end with ; or \g.
Your OceanBase connection id is 3221487661
Server version: OceanBase_CE 4.2.1.0 (r100000102023092807-7b0f43693565654bb1d7343f728bc2013dfff959) (Built Sep 28 2023 07:25:28)

Copyright (c) 2000, 2018, OceanBase and/or its affiliates. All rights reserved.

Type ‘help;’ or ‘\h’ for help. Type ‘\c’ to clear the current input statement.

obclient [testdb]>
obclient [testdb]>
obclient [testdb]> drop table sales;
Query OK, 0 rows affected (0.051 sec)

obclient [testdb]> CREATE TABLE sales (
→ sale_id NUMBER,
→ product_id NUMBER,
→ customer_id NUMBER,
→ sale_date TIMESTAMP NOT NULL,
→ amount NUMBER
→ ) PARTITION BY RANGE(UNIX_TIMESTAMP(sale_date)) (
→ PARTITION p202301 VALUES LESS THAN(UNIX_TIMESTAMP(‘2023-02-01’)),
→ PARTITION p202302 VALUES LESS THAN(UNIX_TIMESTAMP(‘2023-03-01’)),
→ PARTITION p202303 VALUES LESS THAN(UNIX_TIMESTAMP(‘2023-04-01’))
→ );
Query OK, 0 rows affected (0.050 sec)

obclient [testdb]>
obclient [testdb]> show create table sales;
±------±-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
±------±-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| sales | CREATE TABLE sales (
sale_id decimal(10,0) DEFAULT NULL,
product_id decimal(10,0) DEFAULT NULL,
customer_id decimal(10,0) DEFAULT NULL,
sale_date timestamp NOT NULL,
amount decimal(10,0) DEFAULT NULL
) DEFAULT CHARSET = utf8mb4 ROW_FORMAT = DYNAMIC COMPRESSION = ‘zstd_1.3.8’ REPLICA_NUM = 1 BLOCK_SIZE = 16384 USE_BLOOM_FILTER = FALSE TABLET_SIZE = 134217728 PCTFREE = 0
partition by range(UNIX_TIMESTAMP(sale_date))
(partition p202301 values less than (1675180800),
partition p202302 values less than (1677600000),
partition p202303 values less than (1680278400)) |
±------±-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.006 sec)

obclient [testdb]>
obclient [testdb]> create index ix_amount on sales(amount) local;
Query OK, 0 rows affected (0.247 sec)

obclient [testdb]> create index ix_customer_id on sales(customer_id) global;
Query OK, 0 rows affected (0.247 sec)

obclient [testdb]> create unique index ix_product_id on sales(product_id,sale_date);
Query OK, 0 rows affected (0.349 sec)

obclient [testdb]> create unique index ix_sale_date on sales(sale_date);
Query OK, 0 rows affected (0.348 sec)

obclient [testdb]>
obclient [testdb]> show create table sales;
±------±-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
±------±-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| sales | CREATE TABLE sales (
sale_id decimal(10,0) DEFAULT NULL,
product_id decimal(10,0) DEFAULT NULL,
customer_id decimal(10,0) DEFAULT NULL,
sale_date timestamp NOT NULL,
amount decimal(10,0) DEFAULT NULL,
UNIQUE KEY ix_product_id (product_id, sale_date) BLOCK_SIZE 16384 LOCAL,
UNIQUE KEY ix_sale_date (sale_date) BLOCK_SIZE 16384 LOCAL,
KEY ix_amount (amount) BLOCK_SIZE 16384 LOCAL,
KEY ix_customer_id (customer_id) BLOCK_SIZE 16384 GLOBAL
) DEFAULT CHARSET = utf8mb4 ROW_FORMAT = DYNAMIC COMPRESSION = ‘zstd_1.3.8’ REPLICA_NUM = 1 BLOCK_SIZE = 16384 USE_BLOOM_FILTER = FALSE TABLET_SIZE = 134217728 PCTFREE = 0
partition by range(UNIX_TIMESTAMP(sale_date))
(partition p202301 values less than (1675180800),
partition p202302 values less than (1677600000),
partition p202303 values less than (1680278400)) |
±------±-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.002 sec)

obclient [testdb]>

2 个赞

我在你说的这篇博客里,没搜到你说的这些写错的部分呀?能麻烦截个图吗?如果能让我们确认 @兹拉坦 这个作者确实是写错了,我们会让 @兹拉坦 给您道歉,并从他的个人账户里转赠 500 社区积分作为纠错的奖励~

MySQL 模式下,如果未指定 LOCAL 或 GLOBAL 关键字,默认创建的应该是 local 索引。

1 个赞

索引创建可以参考这个文档:
局部索引和全局索引-V4.2.1-OceanBase 数据库文档-分布式数据库使用文档

MySQL模式下,默认是local

mysql 模式默认创建的就是LOCAL索引,ORACLE默认默认创建GLOBAL索引,你看的mysql默认创建GLOBAL索引的信息是从哪里得到的

哈哈,出错的是 @论坛小助手 这个机器人的自动回复。MySQL 模式下,默认是 local index。

根据文档库中的信息,您的测试结果是正确的,在 OceanBase 数据库的 MySQL 模式下,默认创建的是局部索引(local index),而不是全局索引(global index)。以下是详细解释:

  1. 默认索引类型
    根据文档片段 4 的说明:

    OceanBase 4.0 版本中,MySQL 默认创建的是本地索引(local),Oracle 模式默认创建的是全局索引(global)。

    这表明在 MySQL 模式下,默认情况下创建的索引是局部索引(local index),除非明确指定 GLOBAL 关键字。

  2. 测试结果验证
    在您的测试中,您创建了以下索引:

    • ix_amount:明确指定了 LOCAL,因此创建的是局部索引。
    • ix_customer_id:明确指定了 GLOBAL,因此创建的是全局索引。
    • ix_product_idix_sale_date:未指定 LOCALGLOBAL,但最终显示为 LOCAL

    这与文档片段 4 的描述一致,即在 MySQL 模式下,如果未指定 LOCALGLOBAL,默认创建的是局部索引。

  3. 链接内容的误解
    您提到的链接内容可能适用于 Oracle 模式,或者是一个早期版本的行为描述。但在当前版本(OceanBase 4.2.1.0)中,MySQL 模式下的默认行为是创建局部索引。

结论

您的测试结果正确反映了 OceanBase 数据库在 MySQL 模式下的默认行为。如果需要创建全局索引,请显式使用 GLOBAL 关键字。例如:

CREATE INDEX ix_global_index ON sales(amount) GLOBAL;

如果您仍有疑问,建议参考官方文档或联系 OceanBase 技术支持团队以获取进一步确认。

具体信息可参考以下文档:

  1. OceanBase 数据库在 MySQL 模式租户下的扩展功能
  2. 集群管理 FAQ
  3. OceanBase 数据库社区版 V3.1.3
  4. 索引设计
  5. OceanBase 数据库性能的影响因素
  6. 索引概述
  7. 创建索引
  8. 使用 MySQL 租户做常见数据库开发

哇,500积分呢:joy: