0% found this document useful (0 votes)
4 views7 pages

UNIT1 Notes ABDA

Performance tuning is the process of optimizing SQL queries to enhance database speed and efficiency, involving techniques like indexing. Indexes improve data retrieval speed and reduce resource usage, with various types such as single-column, composite, unique, and bitmap indexes. The document also covers the use of ROWID and ROWNUM functions, views, clustered indexes, and storage methods like indexed and hash clusters in SQL databases.

Uploaded by

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

UNIT1 Notes ABDA

Performance tuning is the process of optimizing SQL queries to enhance database speed and efficiency, involving techniques like indexing. Indexes improve data retrieval speed and reduce resource usage, with various types such as single-column, composite, unique, and bitmap indexes. The document also covers the use of ROWID and ROWNUM functions, views, clustered indexes, and storage methods like indexed and hash clusters in SQL databases.

Uploaded by

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

UNIT -1 NOTES

Performance Tuning

Performance Tuning - is the process of optimizing SQL queries to improve


the speed and efficiency of database operations. It involves various techniques to optimize
the execution of queries, manage system resources more effectively, and ensure that the
database responds quickly to user requests.

Optimizing SQL performance is crucial because poorly optimized queries can severely affect
the speed of the database, increase CPU usage, and lead to system downtime. By improving
query execution times and resource utilization, performance tuning enhances the overall
performance of the SQL database.

Indexes - Indexes in SQL are used to improve the speed of data retrieval operations on a
database table. They are similar to the index in a book, where instead of reading through the
entire content, you can quickly jump to the specific page you're looking for. In the case of
SQL, indexes help speed up query performance by allowing the database engine to quickly
locate the rows that satisfy a query condition.

Benefits of Indexes:

 Faster Queries: Speeds up SELECT and JOIN operations.

 Lower Disk I/O: Reduces the load on your database by limiting the amount of data
scanned.

 Better Performance on Large Tables: Essential when working with millions of


records.

Syntax

CREATE INDEX index_name


ON table_name (column name);

Example - CREATE INDEX idx_product_id


ON Sales (product_id);

Types of Index –
1. Single-Column Index/simple index : A single-column index is an index created on one
column of a table. This is the most basic type of index, and it speeds up queries that filter or
sort data based on a single column

Eg: CREATE INDEX idx_last_name ON employees (last_name);

2. Composite Index (Multi-Column Index): A composite index is an index created on


multiple columns of a table. It is helpful when queries filter based on more than one column.

Eg: Suppose you have a students table with first_name and last_name columns, and you
often query students by both names:

CREATE INDEX idx_name ON students (first_name, last_name);

3. Unique Index: A unique index ensures that the values in the indexed column(s) are
unique. It is automatically created when you add a UNIQUE constraint to a column or a set
of columns.

For the users table, where the email column must have unique values, you can create a
unique index:

CREATE UNIQUE INDEX idx_email ON users (email);

4. Reverse key index : In a Reverse Key Index, the bytes of the index key are reversed before
being stored in the B-tree index structure. A Reverse Key Index is a special type of index
used primarily to distribute data more evenly across index blocks, improving performance
in specific high-insertion use cases—especially in Oracle Database.

CREATE INDEX idx_reverse_id ON employees(id) REVERSE;

5.A Bitmap Index is a specialized type of SQL index that uses bitmaps (bit arrays) instead of
traditional tree structures (like B-trees) to represent index entries. It is especially efficient for
queries on low-cardinality columns—columns with a small number of distinct values (e.g.,
gender, status, region).

CREATE BITMAP INDEX idx_gender ON employees(gender);

DROPPING INDEX:

If an index is no longer needed, it can be removed to improve write performance or


save storage space. As indexes can slow down operations like INSERT, UPDATE,
and DELETE due to the overhead of maintaining them, dropping unnecessary indexes can
improve overall database efficiency. The DROP INDEX command is used for this purpose.

DROP INDEX indexname ON table_name;


ROWID Function and ROWNUM function
The ROWID is a unique identifier for rows in a table, used by the database management
system (DBMS) to track the physical location of each row in a table. It is typically used in
Oracle and other relational databases to quickly access specific rows.

ROWID is a pseudo-column that exists in every table and returns a unique address for each
row.

The ROWID is not a physical column that you can modify. It’s a system-generated identifier.

Selecting ROWID:

SELECT ROWID, employee_name FROM employees WHERE department = 'Sales';

Using ROWID to Delete a Row

DELETE FROM employees WHERE ROWID = 'AAAF0tAABAAAG2fAAA';

Use Cases of ROWID

1. Fast Retrieval:

o ROWID can be used to quickly locate rows, especially in Oracle, where it


directly maps to the physical location of the row on disk.

2. Efficient Deletion/Update:

o Using ROWID for DELETE or UPDATE ensures that you target the exact row
without ambiguity. This is often used in scenarios where the table doesn’t
have a unique identifier (like a PRIMARY KEY).

3. Optimizing Large Table Queries:

o In large tables, using ROWID can speed up queries, especially when dealing
with indexed or non-unique columns. Since ROWID is unique and fast to
access, it reduces the overhead of scanning the entire table.

ROWNUM Function in SQL

The ROWNUM function is used to assign a unique, sequential number to rows in the result
set of a query. It is most commonly used in Oracle databases.

In Oracle, ROWNUM is a pseudocolumn that is used to return a unique sequential integer


to each row starting from 1. The number is assigned in the order the rows are processed .

SELECT ROWNUM, column1, column2

FROM employees;
Example SELECT ROWNUM, employee_name, department

FROM employees;

ROWNUM employee_name department

1 John Sales

2 Jane HR

3 Bob Marketing

View in sql
Views in SQL are a type of virtual table that simplifies how users interact with data across
one or more tables. Unlike traditional tables, a view in SQL does not store data on disk;
instead, it dynamically retrieves data based on a pre-defined query each time it’s accessed.

SQL views are particularly useful for managing complex queries, enhancing security, and
presenting data in a simplified forma

Syntax CREATE VIEW view_name AS

SELECT column1, column2, ...

FROM table_name

WHERE condition;

Example CREATE VIEW DetailsView AS


SELECT NAME, ADDRESS
FROM StudentDetails
WHERE S_ID < 5;

Creating view on multiple tables :

CREATE TABLE Customers (

CustomerID INT PRIMARY KEY,

Name VARCHAR(100)

);

CREATE TABLE Orders (


OrderID INT PRIMARY KEY,

CustomerID INT,

OrderDate DATE,

TotalAmount DECIMAL(10, 2),

FOREIGN KEY (CustomerID) REFERENCES Customers(CustomerID)

);

You can create a view to join them:

CREATE VIEW CustomerOrders AS

SELECT

c.CustomerID,

c.Name,

o.OrderID,

o.OrderDate,

o.TotalAmount

FROM Customers c

JOIN Orders o ON c.CustomerID = o.CustomerID;

CLUSTER INDEX

A clustered index determines the physical order of data in a table. There can be only one
clustered index per table because the table rows themselves are stored in the order of the
clustered index.

Key Characteristics

 Physically reorders the table rows to match the index.

 Improves performance for range queries or queries involving sorting.

 Only one clustered index per table.

 Automatically created on primary key (in most RDBMSs, unless overridden).

Syntax - CREATE CLUSTERED INDEX idx_Employees_Department

ON Employees(Department);
Indexed Cluster :

An Indexed Cluster in Oracle is a way of storing related rows from different tables together
based on a common key — but instead of using a hash function (like a hash cluster), it uses a
cluster index (B-tree) to locate rows.

It’s a physical data organization technique that improves join performance between tables
sharing the same cluster key.

When to Use an Indexed Cluster

Use indexed clusters when:

 You frequently perform joins on a common key (e.g., CustomerID).

 You want to improve I/O performance by storing related rows together.

 You prefer B-tree indexed access over hash-based access.

Syntax

CREATE INDEX customer_cluster_idx

ON CLUSTER customer_cluster;

HASH CLUSTER :

A Hash Cluster is a special data storage method unique to Oracle Database. It allows rows to
be stored together based on the result of a hash function applied to a key value — improving
direct access performance for equality searches (=).

Instead of using a traditional index, Oracle uses a hash function to determine where to store
and find the data.

When to Use a Hash Cluster

Use a hash cluster when:

 You're frequently using equality-based lookups (e.g., WHERE id = 100).

 The dataset is static or rarely updated (hash collisions can cause performance issues
during inserts/updates).

 You need fast access without using a traditional index.

Syntax

CREATE CLUSTER emp_cluster (

EmployeeID NUMBER
)

HASHKEYS 100;

Indexed Cluster vs Hash Cluster vs Clustered Index

Indexed Cluster Clustered Index (SQL


Feature Hash Cluster (Oracle)
(Oracle) Server/MySQL)

Rows grouped by Rows hashed into Rows ordered by indexed


Storage
cluster key buckets column

Access
Cluster index (B-tree) Hash function Clustered B-tree index
method

Best for Multi-table joins Fast equality lookups Sorting, range scans

SQL Server, MySQL (InnoDB),


DB Support Oracle only Oracle only
etc.

Multiple
Yes No No
tables

You might also like