RDBMS Views and Indexes
Use views for reusable queries and indexes for faster lookups.
Views and Indexes in RDBMS
As databases grow larger, managing and searching data becomes more challenging. Imagine a database with millions of customer records. Retrieving the required information quickly and showing only relevant data becomes very important.
This is where views and indexes help. A view lets you display specific data from one or more tables without storing a separate copy of that data. An index helps the database find records much faster, improving query performance.
Although they serve different purposes, both views and indexes make databases easier to use and more efficient.
What is a View?
A view is a virtual table created from the result of an SQL query.
Unlike a regular table, a view does not usually store data itself. Instead, it displays data taken from one or more existing tables whenever the view is queried.
Think of a view as a saved SQL query. Instead of writing the same query repeatedly, you can create a view and use it whenever needed.
Why Do We Use Views?
Views provide several advantages:
- Simplify complex SQL queries.
- Display only the required columns.
- Improve data security by hiding sensitive information.
- Make reports easier to create.
- Reduce repeated SQL code.
For example, an employee table may contain salary information, phone numbers, and addresses. A view can display only employee names and departments for users who do not need access to confidential data.
Creating a View
Suppose you have an Employees table.
| EmployeeID | Name | Department | Salary |
|---|---|---|---|
| 101 | Rahul | Sales | 45000 |
| 102 | Priya | HR | 50000 |
| 103 | Aman | IT | 60000 |
To create a view that shows only employee names and departments:
CREATE VIEW EmployeeDetails AS
SELECT Name, Department
FROM Employees;Now you can retrieve data from the view just like a table.
SELECT *
FROM EmployeeDetails;| Name | Department |
|---|---|
| Rahul | Sales |
| Priya | HR |
| Aman | IT |
The original table remains unchanged, while the view provides a simpler way to access selected information.
What is an Index?
An index is a special database object that helps SQL locate records more quickly.
Without an index, the database may need to examine every row in a table to find the required data. This process becomes slower as the table grows.
An index works much like the index at the back of a book. Instead of reading every page, you look up a keyword in the index and go directly to the correct page.
Why Do We Use Indexes?
Indexes improve database performance by reducing the time required to search for records.
- Faster data retrieval.
- Improved query performance.
- Better performance for large tables.
- Faster sorting and filtering.
- More efficient searching using frequently queried columns.
Indexes are especially useful for columns that are searched often, such as customer IDs, email addresses, or product codes.
Creating an Index
Suppose you have a Customers table.
| CustomerID | Name | |
|---|---|---|
| 1 | Rahul | rahul@email.com |
| 2 | Priya | priya@email.com |
| 3 | Aman | aman@email.com |
To create an index on the Email column:
CREATE INDEX idx_email
ON Customers (Email);After the index is created, searches using the Email column become faster.
SELECT *
FROM Customers
WHERE Email = 'rahul@email.com';The database can quickly locate the matching record instead of scanning the entire table.
Views vs Indexes
Although both improve database usability, they serve different purposes.
| View | Index |
|---|---|
| Displays selected data | Speeds up data retrieval |
| Acts like a virtual table | Acts like a lookup structure |
| Simplifies complex queries | Improves query performance |
| Helps improve security | Helps improve search speed |
In simple terms, a view controls how data is presented, while an index controls how efficiently data is found.
Best Practices
When using views and indexes, follow these guidelines:
- Create views to simplify frequently used queries.
- Use views to hide sensitive columns from certain users.
- Create indexes on columns that are searched or filtered often.
- Avoid creating too many indexes, because they can slow down data insertion and updates.
- Review database performance regularly and add indexes only where they provide a clear benefit.