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.

EmployeeIDNameDepartmentSalary
101RahulSales45000
102PriyaHR50000
103AmanIT60000

To create a view that shows only employee names and departments:

SQL
CREATE VIEW EmployeeDetails AS
SELECT Name, Department
FROM Employees;

Now you can retrieve data from the view just like a table.

SQL
SELECT *
FROM EmployeeDetails;
NameDepartment
RahulSales
PriyaHR
AmanIT

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.

CustomerIDNameEmail
1Rahulrahul@email.com
2Priyapriya@email.com
3Amanaman@email.com

To create an index on the Email column:

SQL
CREATE INDEX idx_email
ON Customers (Email);

After the index is created, searches using the Email column become faster.

SQL
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.

ViewIndex
Displays selected dataSpeeds up data retrieval
Acts like a virtual tableActs like a lookup structure
Simplifies complex queriesImproves query performance
Helps improve securityHelps 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.