Oracle本地索引与带分区关键字的本地索引的区别

Oracle differences of local index and local index with partition keyword

我在 Oracle 中找到了 this 关于不同索引的教程。 我想知道,是否有人可以解释这三个创建索引语句之间的区别:

CREATE INDEX invoices_idx ON invoices (invoice_date);

CREATE INDEX invoices_idx ON invoices (invoice_date) LOCAL;

CREATE INDEX invoices_idx ON invoices (invoice_date) LOCAL
 (PARTITION invoices_q1 TABLESPACE users,
  PARTITION invoices_q2 TABLESPACE users,
  PARTITION invoices_q3 TABLESPACE users,
  PARTITION invoices_q4 TABLESPACE users);

我本以为,所有分区都会在每个分区上创建相同类型的本地索引。

//编辑: 另一个问题。 假设我有以下 table

CREATE TABLE invoices
(invoice_no    NUMBER NOT NULL,
 invoice_date  DATE   NOT NULL,
 comments      VARCHAR2(500))
PARTITION BY RANGE (invoice_date)
(PARTITION invoices_q1 VALUES LESS THAN (TO_DATE('01/04/2001', 'DD/MM/YYYY')) TABLESPACE users,
 PARTITION invoices_q2 VALUES LESS THAN (TO_DATE('01/07/2001', 'DD/MM/YYYY')) TABLESPACE users,
 PARTITION invoices_q3 VALUES LESS THAN (TO_DATE('01/09/2001', 'DD/MM/YYYY')) TABLESPACE users,
 PARTITION invoices_q4 VALUES LESS THAN (TO_DATE('01/01/2002', 'DD/MM/YYYY')) TABLESPACE users);

我创建了一个索引:

CREATE INDEX invoices_idx ON invoices (invoice_date);

那是什么类型的索引,如果我也查询例如,我需要什么类型的索引仅invoice_no?

感谢您的澄清。 E.

CREATE INDEX invoices_idx ON invoices (invoice_date);

您正在创建全局索引。文档说 "global index can be partitioned by the range or hash method, and it can be defined on any type of partitioned, or non-partitioned, table"。

CREATE INDEX invoices_idx ON invoices (invoice_date) LOCAL;

您正在创建本地索引。本地索引是索引分区和 table 分区之间的 one-to-one 映射。

CREATE INDEX invoices_idx ON invoices (invoice_date) LOCAL
 (PARTITION invoices_q1 TABLESPACE users,
  PARTITION invoices_q2 TABLESPACE users,
  PARTITION invoices_q3 TABLESPACE users,
  PARTITION invoices_q4 TABLESPACE users);

您正在创建本地分区索引。本地分区索引允许我们在不影响 table.

中的其他分区和索引的情况下,使 table 的各个分区和索引脱机进行维护(或重组)

如果您创建索引 LOCAL,则每个分区都有其 "own" 索引。否则你有一种 "one big index" 跨越整个 table 称为 GLOBAL 索引。

甚至还有第三种选择,您也可以对索引进行分区。 LOCAL 表示,索引分区与基本分区相同table。但是,可以对不同于 table 的索引进行分区 - 但我必须承认,我不知道任何有意义的用例。

通常 LOCAL 索引更好,因为

  • 与一个大索引相比,索引分区更小(因此更快)扫描
  • 当您 drop/truncate 分区时索引不会失效
  • 您可以只在某些分区而不是整个 table 上建立索引。

当然也有缺点

  • 如果您需要一个 UNIQUE 索引并且分区 key-column 不是您索引的一部分,那么它必须是一个 GLOBAL 索引
  • 如果您有很多分区并且您的查询没有指定分区,那么 LOCAL 索引可能会更慢,因为扫描许多本地索引分区就像扫描许多不同的索引。