Practical 5 Implement Indexes
Practical 5 Implement Indexes
Objective:
The objective of this practical is to understand and implement indexes in a database. An index
improves the speed of data retrieval operations on a database table at the cost of additional space
and maintenance overhead. By implementing indexes, you will explore how they enhance
performance, particularly in SELECT queries.
Introduction:
An index is an ordered list of values (usually sorted) from one or more columns of a table. It helps
speed up retrieval operations by reducing the number of rows to search through. Indexes are
particularly useful when dealing with large datasets, allowing the database management system
(DBMS) to quickly locate rows.
Types of indexes:
Key Concepts:
Theory:
Syntax:
2. Index on Multiple Columns: When creating an index on more than one column.
Example:
CREATE INDEX idx_name_age ON employees (last_name, first_name);
3. Unique Index: A type of index that ensures no two rows in the table have the same value
for the indexed columns.
Example:
CREATE UNIQUE INDEX idx_unique_email ON employees (email);
4. Performance Considerations:
○ Faster Query Performance: Indexes speed up SELECT queries, especially with
WHERE, JOIN, and ORDER BY clauses.
○ Slower Insert/Update/Delete: Indexes can slow down DML (Data Manipulation
Language) operations because the index must also be updated.
○ Space Consumption: Indexes consume additional disk space, as they store a sorted
list of column values.
Practical Steps:
Ensure your database is set up and you have a table on which you can implement indexes. If
necessary, create a table.
Example:
create table employee(eid int primary key, name varchar(20), salary int, dept varchar(20));
Query OK, 0 rows affected (0.05 sec)
Step 2: Inserting Data
Insert sample data into the table. This will allow you to test the performance before and after index
implementation.
Example:
Test the performance by running SELECT queries before and after the index is created.
Example of SELECT without index:
If you need to remove the index later, use the DROP INDEX statement.
mysql> alter table employee drop index sal;
Query OK, 0 rows affected (0.03 sec)
Records: 0 Duplicates: 0 Warnings: 0
Compare the performance of queries before and after creating the indexes. For large tables, the
improvement in query performance should be more noticeable.
Expected Results:
1. You should see faster query execution times with indexes, especially when querying large
datasets.
2. Performance improvements will be more apparent with larger data volumes.
Conclusion: By completing this practical, you will understand how indexes improve query
performance in databases and learn how to implement them effectively. You will also observe the
trade-offs involved, such as the increased time required for data modification operations and the
additional storage overhead.
Additional Exercises:
1. Create a unique index on the email column and observe the behavior when inserting
duplicate data.
2. Experiment with the performance impact of indexes on larger datasets by inserting more
rows into the table.