请求解析Oracle中三种CREATE INDEX语句的差异
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
invoicestable is partitioned, any partition maintenance operations (like dropping or truncating a table partition) will mark this entire index asUNUSABLE—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
invoicestable 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
invoicestable, you're manually naming each index partition (e.g.,invoices_q1for 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 Type | Key Behavior | Maintenance Overhead | Best For |
|---|---|---|---|
| Global non-partitioned index | Single index for entire table | High | Cross-partition queries |
| Default local index | Mirrors table partitions automatically | Low | Single-partition queries, easy maintenance |
| Explicit local index | Mirrors table partitions with custom settings | Low (plus customization) | Custom partition naming/storage needs |
内容的提问来源于stack exchange,提问作者Ele

