A B-tree (Balanced Tree) index is the default index type in Oracle Database. It helps Oracle retrieve rows quickly without scanning the entire table.
Instead of searching every row in a table, Oracle uses a tree structure to find the required data efficiently.
Think of it like a book's index: rather than reading every page to find a topic, you use the index to jump directly to the relevant page.
1. Example: Create an Employee Table
Let's create an EMPLOYEES table to understand how a B-tree index works.
SQL Query:
CREATE TABLE employees (
emp_id NUMBER,
emp_name VARCHAR2(50),
salary NUMBER,
department_id NUMBER
);
Now insert some sample employee records.
INSERT INTO employees VALUES (101, 'John', 50000, 10);
INSERT INTO employees VALUES (102, 'Smith', 60000, 20);
INSERT INTO employees VALUES (103, 'David', 45000, 10);
INSERT INTO employees VALUES (104, 'Alice', 70000, 30);
INSERT INTO employees VALUES (105, 'Robert', 55000, 20);
COMMIT;
Let's check the inserted records.
SELECT *
FROM employees;
| EMP_ID | EMP_NAME | SALARY | DEPARTMENT_ID |
|---|---|---|---|
| 101 | John | 50000 | 10 |
| 102 | Smith | 60000 | 20 |
| 103 | David | 45000 | 10 |
| 104 | Alice | 70000 | 30 |
| 105 | Robert | 55000 | 20 |
2. Create a B-Tree Index in Oracle
In Oracle, when you create a normal index, Oracle creates a B-tree index by default.
SQL Query:
CREATE INDEX idx_emp_name
ON employees(emp_name);
Explanation:
idx_emp_nameis the index name.employeesis the table name.emp_nameis the indexed column.- Oracle creates a B-tree index automatically.
USING BTREE when creating a normal Oracle index. B-tree is the default index structure.
3. How Does a B-Tree Index Work?
Suppose we have the following employee names:
| EMP_NAME |
|---|
| John |
| Smith |
| David |
| Alice |
| Robert |
Oracle maintains B-tree index entries in sorted order, regardless of the order in which records were inserted into the table.
B-Tree Index Structure
Conceptual B-tree Index Structure
ROWID David
ROWID John
ROWID Robert
ROWID Smith
ROWID
Simplified conceptual illustration, not the actual block layout of this five-row index.
A B-tree index generally contains three types of index blocks:
- Root Block: The starting point of the index search.
- Branch Blocks: Help Oracle navigate to the correct leaf block.
- Leaf Blocks: Store indexed column values and corresponding ROWIDs.
1. Root Block
The Root Block is the top-level block of the B-tree index. Oracle starts its index search from this block.
2. Branch Blocks
Branch Blocks contain separator key values and pointers to lower-level index blocks. They help Oracle navigate to the correct leaf block.
3. Leaf Blocks
Leaf Blocks contain indexed column values and corresponding ROWIDs. Oracle uses these ROWIDs to locate the actual table records.
4. Query Using a B-Tree Index
Let's execute the following SQL query to find an employee named Smith.
SELECT *
FROM employees
WHERE emp_name = 'Smith';
If Oracle chooses the B-tree index, it can perform the following operations:
Step 1: Start from the Root Block
Oracle starts searching from the root block of the B-tree index.
Step 2: Navigate Through Branch Blocks
Oracle uses the key values in branch blocks to identify the appropriate leaf block.
Step 3: Find Smith in the Leaf Block
Oracle searches the leaf block and locates the index entry for Smith.
Step 4: Get the ROWID
The index entry provides the ROWID corresponding to the employee record.
Step 5: Retrieve the Actual Table Row
Oracle uses the ROWID to access the EMPLOYEES table and retrieve the actual employee details.
5. How to Check Whether Oracle Uses the Index
We can use EXPLAIN PLAN to see the execution plan selected for a SQL statement.
Step 1: Generate the Execution Plan
EXPLAIN PLAN FOR
SELECT *
FROM employees
WHERE emp_name = 'Smith';
Step 2: Display the Execution Plan
SELECT *
FROM TABLE(DBMS_XPLAN.DISPLAY);
Look for an operation such as:
INDEX RANGE SCAN IDX_EMP_NAME
For a normal non-unique index with an equality condition, Oracle commonly uses INDEX RANGE SCAN.
6. Different Types of B-Tree Index Scans
Oracle supports different index access operations depending on the SQL query and index structure.
| Scan Type | Meaning |
|---|---|
| INDEX UNIQUE SCAN | Finds a single matching index entry using a unique index key. |
| INDEX RANGE SCAN | Finds one or more matching index entries. |
| INDEX FULL SCAN | Reads all index entries in index order. |
| INDEX FAST FULL SCAN | Reads the entire index without preserving index order. |
| INDEX SKIP SCAN | Can use a composite index even when the leading column is not specified. |
| INDEX RANGE SCAN DESCENDING | Reads matching index entries in descending order. |
7. When Should You Use a B-Tree Index?
B-tree indexes are useful for many common SQL query conditions.
- Equality Conditions:
WHERE emp_id = 101 - Range Conditions:
WHERE salary > 50000 - Sorting:
ORDER BY emp_name - BETWEEN Conditions:
WHERE salary BETWEEN 40000 AND 60000 - JOIN Operations: Searching using indexed join columns.
Example: Create an Index on Salary
CREATE INDEX idx_emp_salary
ON employees(salary);
Example Query:
SELECT *
FROM employees
WHERE salary BETWEEN 40000 AND 60000;
Oracle may use the index to locate employee records whose salaries fall within the specified range.
8. Important Interview Questions
Q1. What Is the Default Index Type in Oracle?
Answer: B-tree index is the default index type in Oracle Database.
Q2. Does Oracle Store Actual Table Data Inside a B-Tree Index?
Answer: No. A conventional B-tree index stores indexed column values and ROWIDs pointing to the actual table rows.
Q3. Can a B-Tree Index Contain NULL Values?
Answer: A conventional single-column B-tree index does not store entries where the indexed column is entirely NULL. However, composite B-tree indexes can contain entries when at least one indexed column is non-NULL.
Q4. What Is the Difference Between B-Tree and Bitmap Indexes?
| B-Tree Index | Bitmap Index |
|---|---|
| Suitable for high-cardinality columns. | Often suitable for low-cardinality columns. |
| Common in OLTP systems. | Often used in data warehouses. |
| Stores key values and ROWIDs. | Stores bitmaps representing matching rows. |
| Generally better suited to frequent individual DML operations. | Can be expensive for frequent concurrent DML. |
Q5. What Is the Main Advantage of a B-Tree Index?
Answer: It helps Oracle locate records efficiently using a balanced tree structure without scanning every row in the table.
Remember this simple definition:
A B-tree index in Oracle is a balanced tree structure that stores sorted indexed column values along with ROWIDs, allowing Oracle to locate table rows efficiently without performing a Full Table Scan whenever index access is more efficient.
Conclusion
B-tree indexes are the default and most commonly used index type in Oracle Database. They help improve SQL query performance by storing indexed values in a balanced tree structure.
Understanding Root Blocks, Branch Blocks, Leaf Blocks, and ROWIDs is important for understanding how Oracle retrieves records using indexes.
B-tree indexes are useful for equality searches, range conditions, sorting, and other common SQL operations.
Keep Learning Oracle SQL and Database Performance Tuning!
No comments:
Post a Comment