0% found this document useful (0 votes)
7 views8 pages

Lab 06 (1) (1)

The document is a lab manual for a Database Administration & Management course, focusing on creating and managing indexes in SQL Server. It outlines the objectives, types of indexes, their purposes, and provides examples and lab tasks for students to practice. The lab aims to enhance understanding of how indexes improve query performance and data retrieval efficiency.

Uploaded by

jannatimtiaz288
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)
7 views8 pages

Lab 06 (1) (1)

The document is a lab manual for a Database Administration & Management course, focusing on creating and managing indexes in SQL Server. It outlines the objectives, types of indexes, their purposes, and provides examples and lab tasks for students to practice. The lab aims to enhance understanding of how indexes improve query performance and data retrieval efficiency.

Uploaded by

jannatimtiaz288
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/ 8

Database Administration & Management Lab

Lab Manual 06

Session: Fall-2024
Instructor: SHEZA SHABIR

Department of Informatics & Systems


School of System & Technology
University of Management & Technology Lahore Pakistan
Objective: (CLO1, CLO2, CLO3)

In today’s lab, we will:

1. Learn how to create and manage indexes in SQL Server.


2. Understand the difference between clustered and non-clustered indexes.
3. Explore how indexes improve query performance and reduce retrieval time.

Index in SQL:

An index is a database object that speeds up data retrieval operations on a table. It works like an
index in a book, allowing the database to locate data quickly without scanning the entire table.

Why Do We Use Indexes?

 Faster Query Performance: Queries that use WHERE, JOIN, or sorting (ORDER BY)
can execute much faster with an index.
 Efficient Data Access: Reduces the time it takes to locate and retrieve data from a table.
 Improved Sorting: Indexes are helpful when ordering data.

Example:

Transaction Table:

What Happens When You Create an Index?

Values from the Indexed Column Are Stored:

 The transaction_date values are extracted and organized in a way that makes
searching faster.
 These values are stored in a sorted order, depending on the index type (e.g., B-tree).
Pointers to the Original Table Are Kept:

 Alongside each transaction_date value, the index stores a "pointer" that refers to the
actual row in the original table. This pointer uses the primary key (like transaction_id)
to locate the row.

Types of Indexes:

1. Single-column Index

 Definition: An index created on a single column of a table.


 Purpose: Speeds up lookups on one specific column.
 Example:

CREATE INDEX idx_employee_name ON employees(name);

This index improves searches like:

SELECT * FROM employees WHERE name = 'John';


2. Composite Index

 Definition: An index created on two or more columns.


 Purpose: Used to optimize queries that filter or sort using multiple columns.
 Example:

CREATE INDEX idx_employee_name_dept ON employees(name, dept_id);

This index speeds up queries like:

SELECT * FROM employees WHERE name = 'John' AND dept_id = 3;

3. Unique Index

 Definition: Ensures all values in the indexed column are unique.


 Purpose: Used to enforce uniqueness in a column (like a primary key).
 Example:

CREATE UNIQUE INDEX idx_employee_email ON employees(email);

Ensures no two employees have the same email.

INSERT INTO employees (name, email) VALUES ('John', '[email protected]');


If '[email protected]' already exists, this will fail.

4. Full-text Index

 Definition: Used for fast searching of text within large text or document fields.
 Purpose: Optimized for searching words/phrases in large text fields (like product
descriptions).
 Example:

CREATE FULLTEXT INDEX ON employees(name) KEY INDEX idx_employee_id;

Speeds up searches for text in large fields, like:

SELECT * FROM employees WHERE CONTAINS (name, 'John');


5. Clustered Index

 Definition: Physically sorts the rows in the table based on a key.


 Purpose: There can be only one clustered index per table since the rows are physically
ordered.
 Example:

CREATE CLUSTERED INDEX idx_employee_id ON employees(id);

Rows are physically stored in the table by the id column.

SELECT * FROM employees WHERE id = 10;

6. Non-clustered Index

 Definition: Creates a separate lookup structure for faster data access.


 Purpose: Unlike clustered indexes, data is not physically sorted, and you can create
multiple non-clustered indexes on a table.
 Example:

CREATE NONCLUSTERED INDEX idx_employee_dept ON employees(dept_id);

It speeds up lookups like:

SELECT * FROM employees WHERE dept_id = 2;

Steps to Check Query Performance

1. Enable Statistics (for I/O and TIME):

SET STATISTICS IO ON; -- Shows I/O details like logical reads, physical reads, etc.
SET STATISTICS TIME ON; -- Shows CPU time and elapsed execution time

2. Run Your Query:

SELECT * FROM employees WHERE department = 'IT';

3. Turn Off Statistics (optional, but recommended):

SET STATISTICS IO OFF;


SET STATISTICS TIME OFF;
Lab Task

1. Create an index idx_emp_name on the emp_name column in the employees table.


2. Write a query to fetch employee details where the emp_name starts with 'A'. Measure the
query execution time before and after creating the index.
3. Create a composite index idx_dept_salary on the department and salary columns in the
employees table.
4. Write a query to fetch employees in the 'HR' department with salaries greater than $50,000.
5. Create a unique index idx_product_name on the product_name column in the inventory
table.
6. Try inserting a duplicate product name in the inventory table and observe the result.
7. Drop the idx_emp_name index from the employees table.
8. Create an index idx_order_date on the order_date column in the orders table.
 Write a query to retrieve the last 10 orders placed, sorted by order_date in descending
order.
9. Create a full-text index idx_supplier_name on the supplier_name column in the inventory
table.
 Write a query to find all suppliers whose name contains the word "Tech".
10. Write a query to fetch all orders with an amount greater than $500. Measure execution time
with and without an index on the amount column in the orders table.
11. Create an index idx_product_id on the product_id column in the inventory table.
 Write a query to find products in the inventory table that have been ordered (join with
the orders table on product_name).
12. Create an index idx_stock on the stock column in the inventory table.
 Write a query to find all products with stock less than 50.
13. Create an index idx_salary on the salary column in the employees table.
 Write a query to calculate the average salary of employees in each department.
14. Create a composite index idx_order_date_amount on the order_date and amount columns
in the orders table.
 Write a query to find the total order amount for each month in 2024.
15. Use the idx_supplier_name full-text index from Task 9.
 Write a query to search for suppliers whose name contains any of the following words:
'Global', 'Tech', or 'Solutions'.

You might also like