To explain the difference between local prefixed indexes and local non-prefixed indexes in Oracle, let's break it down step by step in simple terms with examples and syntax.
Layman Explanation:
Think of your table as a giant filing cabinet with drawers (partitions) organized by years. You create indexes (like lists) to quickly find the documents in each drawer.
Local Prefixed Index:
- This is like labeling each drawer with a specific label (e.g., "Year 2022"), and then inside each drawer, you create an index based on that label. If you want to find a document from 2022, you know exactly which drawer to open and look at that index.
- Prefixing means that the index is directly tied to how the drawers are organized. If the drawers are organized by year, the index is built using the "year" as the main guide.
Local Non-Prefixed Index:
- This is like creating an index inside each drawer, but the index isn't necessarily based on the label on the drawer. For example, the drawers are still organized by year, but the index inside could be based on a different label, like "Customer Name" instead of the "Year."
- The non-prefixed part means that the index is not directly tied to how the drawers are organized. It can be based on a different column or set of columns.
Examples with Syntax:
Let’s say you have a Sales
table partitioned by Sale_Date
(year). The data is divided into partitions based on the year of the sale.
Step 1: Create the Partitioned Table
sqlCREATE TABLE Sales (
Sale_ID NUMBER,
Customer_Name VARCHAR2(100),
Sale_Amount NUMBER,
Sale_Date DATE
)
PARTITION BY RANGE (Sale_Date) (
PARTITION sales_2022 VALUES LESS THAN (TO_DATE('01-JAN-2023', 'DD-MON-YYYY')),
PARTITION sales_2023 VALUES LESS THAN (TO_DATE('01-JAN-2024', 'DD-MON-YYYY'))
);
This table has two partitions based on the Sale_Date
column (2022 and 2023).
Local Prefixed Index
A local prefixed index is based on the same column that is used to create the partitions (Sale_Date
in this case).
Syntax for Local Prefixed Index:
sqlCREATE INDEX sales_local_prefixed_idx
ON Sales (Sale_Date)
LOCAL;
In this case:
- Local: The index is partitioned along with the table, meaning each partition (
sales_2022
,sales_2023
) has its own index. - Prefixed: The index is built on the
Sale_Date
column, which is the same column used to partition the table.
When you query the table based on Sale_Date
, Oracle knows exactly which partition and index to use, making it efficient.
Local Non-Prefixed Index
A local non-prefixed index is based on a different column that is not used to create the partitions. For example, the table is partitioned by Sale_Date
, but the index is created on Customer_Name
.
Syntax for Local Non-Prefixed Index:
sqlCREATE INDEX sales_local_non_prefixed_idx
ON Sales (Customer_Name)
LOCAL;
In this case:
- Local: The index is still partitioned along with the table, meaning each partition has its own index. But...
- Non-Prefixed: The index is based on the
Customer_Name
column, which is different from theSale_Date
column used to partition the table.
Key Differences:
Local Prefixed Index:
- Index is built on the same column(s) used to create the partitions.
- Queries that involve the partitioning column (e.g.,
Sale_Date
) are more efficient because Oracle can directly locate the partition and use the index.
Local Non-Prefixed Index:
- Index is built on a different column(s) than the one used for partitioning.
- Useful for queries that don’t directly involve the partitioning column (e.g., searching by
Customer_Name
), but it may not be as efficient as a prefixed index for partition-based queries.
Example Queries:
Local Prefixed Index: If you search for sales data in 2023:
sqlSELECT * FROM Sales WHERE Sale_Date = TO_DATE('2023-05-15', 'YYYY-MM-DD');
Oracle will directly go to the
sales_2023
partition and use thesales_local_prefixed_idx
index.Local Non-Prefixed Index: If you search for sales by a customer:
sqlSELECT * FROM Sales WHERE Customer_Name = 'John Doe';
Oracle will still use the index on
Customer_Name
within each partition, but it won’t be as direct as using the partitioning column (Sale_Date
).
When to Use:
- Local Prefixed Index: Use when your queries often involve the partitioning column (e.g., dates, regions).
- Local Non-Prefixed Index: Use when you need to search based on other columns (e.g., customer names, product types) that are not used to partition the table.
Let’s dive into Global Non-Partitioned Indexes and Global Partitioned Indexes in Oracle, explain them in simple terms, and go over examples including syntax.
Layman Explanation:
Imagine you have a giant warehouse storing products, and you organize the warehouse by year—each year has its own section (partition). You want to create indexes (like lists) to help you quickly find specific products.
- Global Index: Unlike local indexes, which are divided and tied to specific sections of the warehouse (partitions), a global index covers the entire warehouse. Think of it as one big master list that spans all sections. There are two types:
- Global Non-Partitioned Index: This is like having one big list that covers everything in the warehouse. There are no smaller lists per section; it’s just one giant index that helps you find any product, no matter what section it's in.
- Global Partitioned Index: This is a global index, but it’s broken into smaller pieces (partitions) based on some criteria. It’s still a global index covering the entire warehouse, but the index itself is split into partitions.
Example with Syntax:
Let’s say you have a Sales table partitioned by Sale_Date
(year). The data is divided into partitions based on the year of the sale.
Step 1: Create the Partitioned Table
sqlCREATE TABLE Sales (
Sale_ID NUMBER,
Customer_Name VARCHAR2(100),
Sale_Amount NUMBER,
Sale_Date DATE
)
PARTITION BY RANGE (Sale_Date) (
PARTITION sales_2022 VALUES LESS THAN (TO_DATE('01-JAN-2023', 'DD-MON-YYYY')),
PARTITION sales_2023 VALUES LESS THAN (TO_DATE('01-JAN-2024', 'DD-MON-YYYY'))
);
This table is partitioned by the Sale_Date
column (2022 and 2023 partitions).
Global Non-Partitioned Index
A global non-partitioned index is a single index that covers the entire table, regardless of the partitions. It’s one big list that can be used to search the whole table.
Syntax for Global Non-Partitioned Index:
sqlCREATE INDEX sales_global_idx
ON Sales (Customer_Name);
In this case:
- The index is global, meaning it covers the entire
Sales
table across all partitions (2022 and 2023). - It’s non-partitioned, so there’s only one index, not separate indexes for each partition.
How It Works:
- If you search for a customer (e.g.,
Customer_Name = 'John Doe'
), Oracle will use this global index to look through the entire table, regardless of the partition. - The index spans the entire table, so if the partition structure changes (e.g., you drop a partition), this index might need to be rebuilt.
Global Partitioned Index
A global partitioned index is still an index that covers the entire table, but the index itself is split into smaller partitions based on some criteria. It’s like breaking the master list into smaller pieces.
Syntax for Global Partitioned Index:
sqlCREATE INDEX sales_global_part_idx
ON Sales (Customer_Name)
GLOBAL PARTITION BY HASH (Customer_Name)
PARTITIONS 4;
In this case:
- The index is global, meaning it covers the entire table.
- It’s partitioned by a hashing method on the
Customer_Name
column, which splits the index into 4 partitions.
How It Works:
- When you search for a customer (e.g.,
Customer_Name = 'John Doe'
), Oracle will use the partitioned index, but it can narrow down the search to a specific partition of the index, making the search faster than if it had to scan through one giant non-partitioned index. - The index partitions don’t align with the table partitions. Instead, they are split based on the index's criteria (e.g., a hash of
Customer_Name
).
Key Differences:
Global Non-Partitioned Index:
- One big index for the entire table, covering all partitions.
- Easier to manage in some cases but can become inefficient if the table grows large.
- Example use case: If you have queries that don’t rely on the partitioning column (e.g., searching by
Customer_Name
), but you don’t need a partitioned index.
Global Partitioned Index:
- The index covers the entire table but is split into smaller partitions based on the index's criteria.
- More scalable and can offer better performance for larger tables.
- Example use case: If you have large datasets and want to improve performance by partitioning the index itself, making it more efficient for queries that need to search across the entire table.
Example Queries:
Global Non-Partitioned Index: If you search for a customer:
sqlSELECT * FROM Sales WHERE Customer_Name = 'John Doe';
Oracle will use the
sales_global_idx
to search the entire table, regardless of partitions.Global Partitioned Index: If you search for a customer:
sqlSELECT * FROM Sales WHERE Customer_Name = 'John Doe';
Oracle will use the
sales_global_part_idx
index but can focus on a specific partition of the index based on the hash ofCustomer_Name
.
When to Use:
- Global Non-Partitioned Index: Use when you want a simple, single index that covers the entire table, especially when the table isn’t too large or complex.
- Global Partitioned Index: Use when you have a large table and want to split the index itself into partitions for better performance and scalability, even when queries need to search across the entire table.
No comments:
Post a Comment