0% found this document useful (0 votes)
1 views

Practical 5 Implement Indexes

This practical focuses on understanding and implementing indexes in a database to enhance data retrieval speed while considering the associated space and maintenance costs. It covers various types of indexes, their creation and deletion using SQL commands, and the performance implications of using indexes in SELECT queries. By completing the practical, participants will learn how to effectively implement indexes and analyze their impact on query performance.

Uploaded by

moriyi8183
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Practical 5 Implement Indexes

This practical focuses on understanding and implementing indexes in a database to enhance data retrieval speed while considering the associated space and maintenance costs. It covers various types of indexes, their creation and deletion using SQL commands, and the performance implications of using indexes in SELECT queries. By completing the practical, participants will learn how to effectively implement indexes and analyze their impact on query performance.

Uploaded by

moriyi8183
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

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:

1. Single-Column Index: An index created on a single column.


2. Multi-Column Index: An index created on two or more columns.
3. Unique Index: Ensures that no two rows have the same value for the indexed column(s).

Key Concepts:

● Primary Key and Unique Indexes: Enforce uniqueness of data.


● Non-Unique Indexes: Speed up query performance without enforcing uniqueness.
● Composite Indexes: Indexes created on multiple columns to optimize queries that involve
multiple columns.

Theory:

CREATE INDEX: The SQL command used to create an index.

Syntax:

CREATE INDEX index_name ON table_name (column_name);


Example:

CREATE INDEX idx_name ON employees (last_name);

1. Drop Index: The SQL command used to drop an existing index.


Syntax:
DROP INDEX index_name;

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:

Step 1: Setting Up the Database

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:

mysql> insert into employee values(101,"akash",80000,"developer");


mysql> insert into employee values(201,"alisha",70000,"tester");
mysql> insert into employee values(111,"asish",90000,"developer");
mysql> insert into employee values(201,"alisha",70000,"tester");
mysql> insert into employee values(302,"akanksha",60000,"manager");
mysql> select*from employee;
+-----+----------+--------+----------+
| eid | name | salary | dept |
+-----+----------+--------+----------+
| 101 | akash | 80000 | developer |
| 111 | asish | 90000 | developer |
| 201 | alisha | 70000 | tester |
| 302 | akanksha | 60000 | manager |
+-----+----------+--------+----------+

Step 3: Create an Index


mysql> create index sal on employee(salary);
Query OK, 0 rows affected (0.04 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show index from employee;
+----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality |
Sub_part | Packed | Null | Index_type | Comment | Index_comment | Visible | Expression |
+----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------
| employee | 0 | PRIMARY | 1 | eid |A | 4| NULL | NULL | |
BTREE | | | YES | NULL |
| employee | 1 | sal | 1 | salary |A | 4| NULL | NULL | YES |
BTREE | | | YES | NULL |
+----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------
Step 4: Query with and without Index

Test the performance by running SELECT queries before and after the index is created.
Example of SELECT without index:

SELECT * FROM employees WHERE Salary = 70000;

Example of SELECT with index:

EXPLAIN SELECT * FROM employee WHERE salary=70000;

Step 5: Drop the 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

Step 6: Multi-Column Index


Example:
CREATE INDEX idx_name ON employee (name,salary);

Step 7: Query with Multi-Column Index

Run a query to check the effect of multi-column indexes.

EXPLAIN SELECT * FROM employees WHERE Name=”Alisha” AND salary=70000;

Step 8: Performance Analysis

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.

You might also like