A Non-Unique Index in Oracle Database is an index that allows duplicate values in the indexed column or combination of columns.
In simple words, multiple rows can have the same value in a Non-Unique Index.
It is mainly used to improve SQL query performance by helping Oracle locate records efficiently.
1. Example of a Non-Unique Index
Suppose we have an EMPLOYEE table containing employee details.
Employee Table:
| EMP_ID | EMP_NAME | DEPARTMENT_ID |
|---|---|---|
| 101 | Ravi | 10 |
| 102 | Kumar | 20 |
| 103 | Priya | 10 |
| 104 | Arun | 30 |
| 105 | Raj | 10 |
Here, DEPARTMENT_ID contains duplicate values.
For example, department ID 10 appears three times.
Let's create a Non-Unique Index on the DEPARTMENT_ID column.
SQL Query:
CREATE INDEX idx_emp_dept
ON employee(department_id);
Explanation:
CREATE INDEXcreates a normal B-tree index by default.idx_emp_deptis the index name.employeeis the table name.department_idis the indexed column.
This creates a Non-Unique Index because we have not specified the UNIQUE keyword.
2. What Happens When We Insert Duplicate Values?
Let's insert another employee with department ID 10.
SQL Query:
INSERT INTO employee
VALUES (106, 'Suresh', 10);
Now the EMPLOYEE table contains another employee belonging to department 10.
Updated Employee Records:
| EMP_ID | EMP_NAME | DEPARTMENT_ID |
|---|---|---|
| 101 | Ravi | 10 |
| 102 | Kumar | 20 |
| 103 | Priya | 10 |
| 104 | Arun | 30 |
| 105 | Raj | 10 |
| 106 | Suresh | 10 |
Notice that department ID 10 appears four times. Oracle allows these duplicate values because the index is Non-Unique.
3. How Does a Non-Unique Index Improve Performance?
Consider the following SQL query:
SELECT *
FROM employee
WHERE department_id = 10;
Without an Index
- Oracle may perform a Full Table Scan.
- It checks table rows to identify employees belonging to department 10.
- This can require reading many table blocks for large tables.
With an Index
- Oracle can search the Non-Unique Index for department ID 10.
- The index contains entries for the matching rows.
- Oracle can use the corresponding ROWIDs to retrieve table records.
4. How to Check Non-Unique Indexes in Oracle?
We can use the USER_INDEXES data dictionary view to check whether an index is UNIQUE or NONUNIQUE.
SQL Query:
SELECT index_name,
table_name,
uniqueness
FROM user_indexes
WHERE table_name = 'EMPLOYEE';
Example Output:
| INDEX_NAME | TABLE_NAME | UNIQUENESS |
|---|---|---|
| IDX_EMP_DEPT | EMPLOYEE | NONUNIQUE |
The UNIQUENESS column displays the following values:
UNIQUE– Unique Index.NONUNIQUE– Non-Unique Index.
5. Difference Between Unique and Non-Unique Index
Oracle supports both Unique and Non-Unique Indexes. Let's understand their differences.
| Unique Index | Non-Unique Index |
|---|---|
| Does not allow duplicate indexed keys. | Allows duplicate indexed keys. |
| Used to enforce uniqueness. | Used mainly for query performance. |
| Created using CREATE UNIQUE INDEX. | Created using CREATE INDEX. |
| Example: Email ID. | Example: Department ID. |
| Useful for unique lookups. | Useful for searching duplicate values. |
| Can contain NULL values. | Can contain NULL values. |
6. How to Create a Non-Unique Index on Multiple Columns?
We can also create a Non-Unique Index on two or more columns. This is called a Composite Non-Unique Index.
SQL Query:
CREATE INDEX idx_dept_salary
ON employee(department_id, salary);
Explanation:
department_idis the first indexed column.salaryis the second indexed column.- Duplicate combinations of these values are allowed.
For example, multiple employees can have the same department ID and salary.
7. Important Oracle Interview Questions
Q1. Can We Insert Duplicate Values into a Non-Unique Index?
Answer: Yes. Oracle allows duplicate values in a Non-Unique Index.
Q2. Can We Create a Non-Unique Index on Multiple Columns?
Answer: Yes. It is called a Composite Non-Unique Index.
Example:
CREATE INDEX idx_dept_salary
ON employee(department_id, salary);
Q3. Does a Non-Unique Index Improve SELECT Query Performance?
Answer: Yes. It can improve SELECT query performance when Oracle determines that using the index is more efficient than other available access paths.
Q4. Does a Non-Unique Index Allow NULL Values?
Answer: Yes. Oracle allows NULL values in Non-Unique Indexes. Unlike a conventional single-column B-tree index, which omits entries where the indexed column is entirely NULL, a Non-Unique composite B-tree index can contain entries with NULL components when at least one indexed column is non-NULL.
Q5. What Is the Default Index Type in Oracle?
Answer: B-tree is the default index type in Oracle when we create a normal index using CREATE INDEX.
Q6. Can We Create Multiple Non-Unique Indexes on a Single Table?
Answer: Yes. Oracle allows multiple Non-Unique Indexes on a single table, depending on the application and query requirements.
Q7. What Is the Difference Between a Unique and Non-Unique Index?
Answer: A Unique Index prevents duplicate indexed key values, whereas a Non-Unique Index allows duplicate indexed key values.
8. Easy Way to Remember
Unique Index
Duplicate indexed key values are not allowed.
Example: Employee Email ID.
Non-Unique Index
Duplicate indexed key values are allowed.
Example: Department ID, Gender, and Job ID.
A Non-Unique Index in Oracle is an index that allows duplicate values in the indexed column or combination of columns. It is mainly created to improve SQL query performance and speed up data retrieval.
Conclusion
Non-Unique Indexes are commonly used Oracle Database objects that help improve SQL query performance.
They allow duplicate indexed key values and are useful when multiple records contain the same value, such as Department ID, Gender, and Job ID.
We can create a Non-Unique Index on a single column or multiple columns depending on the query requirements.
Understanding the difference between Unique and Non-Unique Indexes is important for Oracle SQL Developers and Oracle DBAs.
Keep Learning Oracle SQL and Database Performance Tuning!
No comments:
Post a Comment