You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

请求解析Oracle中三种CREATE INDEX语句的差异

Breaking Down the Three Oracle CREATE INDEX Statements

Alright, let's dive into these three index creation commands—they all target the invoice_date column on the invoices table, but their behavior (especially with partitioned tables) is night and day.

1. CREATE INDEX invoices_idx ON invoices (invoice_date);

This is a global non-partitioned index. Think of it as a single, monolithic index structure that covers every row in the invoices table, regardless of whether the table itself is partitioned.

  • If your invoices table is partitioned, any partition maintenance operations (like dropping or truncating a table partition) will mark this entire index as UNUSABLE—you'll have to rebuild the whole index to get it working again.
  • Best for queries that frequently span multiple table partitions (e.g., "get all invoices from 2023 and 2024"), but it comes with higher maintenance overhead.

2. CREATE INDEX invoices_idx ON invoices (invoice_date) LOCAL;

This is a local partitioned index, and it only makes sense if your invoices table is already partitioned. Here's how it works:

  • The index automatically mirrors the table's partitioning scheme. Every partition in the invoices table gets a corresponding index partition. You don't have to define any partition details here—Oracle handles it for you.
  • When you perform partition maintenance (like dropping a Q1 2024 table partition), only the matching index partition is affected (it gets dropped too, no need to rebuild the whole index). This makes it way easier to maintain for partitioned tables.
  • Ideal for queries that target specific table partitions (e.g., "get all Q1 2024 invoices"), as Oracle can quickly narrow down to the relevant index partition.

3. 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 u...

This is still a local partitioned index, but with explicit partition definitions. It's essentially the same as the second statement, but you're overriding the default behavior:

  • Instead of letting Oracle inherit partition names and tablespaces from the invoices table, you're manually naming each index partition (e.g., invoices_q1 for Q1) and specifying which tablespace each lives in.
  • Use this if you need custom partition naming for clarity, or if you want to store certain index partitions in different tablespaces (e.g., putting frequently accessed index partitions on faster storage).
  • The core benefit of local indexes (partition maintenance doesn't break the whole index) still applies here—you're just adding customization to the index's structure.

Quick Comparison

Statement TypeKey BehaviorMaintenance OverheadBest For
Global non-partitioned indexSingle index for entire tableHighCross-partition queries
Default local indexMirrors table partitions automaticallyLowSingle-partition queries, easy maintenance
Explicit local indexMirrors table partitions with custom settingsLow (plus customization)Custom partition naming/storage needs

内容的提问来源于stack exchange,提问作者Ele

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:38:18