Indexes in Database
Indexes in Database
DATABASE
Presented by: Rafia Latif SP21-BSE-018
Raiha Mehdi SP21-BSE-028
Sobia Batool SP21-BSE-031
Agenda
⬥ What Are Indexes?
⬥ Index Structure
⬥ Why We Use Indexes In Database?
⬥ How To Create Indexes In Oracle?
⬥ Types Of Indexes
⬥ Benefits Of Indexes
2
What Are Indexes?
⬥ Indexing is a data structure technique which allows you to
quickly retrieve records from a database file.
3
Index Structure
⬥ Indexes can be created using some database columns.
4
Example
⬥ Suppose we have an employee table with thousands of
record and each of which is 10 bytes long.
5
Example (Continue)
⬥ In the case of a database with no index, we have to search
the disk block from starting till it reaches 543. The DBMS
will read the record after reading 543*10=5430 bytes.
6
Representation
7
Representation
8
Why we use indexes in database?
9
Why Required?
⬥ An index is a copy of selected columns of data, from a table,
that is designed to enable very efficient search.
⬥ The users cannot see the indexes, they are just used to speed
up searches/queries.
10
3 Indexes in Oracle
Introduction
⬥ An index is a performance-tuning method of
allowing faster retrieval of records.
12
B-tree Indexes
⬥ Referred to as balanced tree.
14
Structure (Continue)
A binary tree index has two types of blocks
⬥ Branch Block
For searching a particular index
⬥ Leaf Block
For storing the data
15
Create an Index
⬥ Syntax
The syntax for creating an index in Oracle is:
16
Create an Index (Continue)
Syntax Explaination
UNIQUE
It indicates that the combination of values in the indexed columns must be
unique.
index_name
The name to assign to the index.
table_name
The name of the table in which to create the index.
column1, column2, ... column_n
The columns to use in the index.
COMPUTE STATISTICS
It tells Oracle to collect statistics during the creation of the index. The
statistics are then used by the optimizer to choose a "plan of execution" when 17
SQL statements are executed.
Example
Let's look at an example of how to create an index in Oracle.
For example:
⬥ CREATE INDEX supplier_idx
ON supplier (supplier_name);
⬥ We could also create an index with more than one field as in the
example below:
CREATE INDEX supplier_idx
ON supplier (supplier_name, city);
18
Example (Continue)
We could also choose to collect statistics upon creation of the
index as follows:
19
Rename An Index
⬥ Syntax
The syntax for renaming an index in Oracle is:
20
Rename An Index
⬥ Syntax Explaination
index_name
The name of the index that you wish to rename.
new_index_name
The new name to assign to the index.
21
Example
Let's look at an example of how to rename an index in Oracle.
For example:
22
Drop An Index
⬥ Syntax
The syntax for dropping an index in Oracle is:
index_name
The name of the index to drop.
23
4 Types of Indexes
25
Primary Index
⬥ If the index is created on the basis of the primary key of the
table, then it is known as primary indexing. These primary
keys are unique to each record and contain 1:1 relation
between the records.
27
28
Sparse Index
In the data file, index record appears only for a few items.
Each item points to a block.
29
30
Clustering Index
A clustered index can be defined as an ordered data file.
Sometimes the index is created on non-primary key columns
which may not be unique for each record.
32
33
5 Benefits of Indexes
Benefits
⬥ Indexing helps in faster query results or quick data retrieval.
35
Benefits (Continue)
⬥ Since CPU speed and secondary memory speed have a large
difference, the CPU uses this main memory index table to
bridge the gap of speeds.
36
ANY
QUESTIONS?
37
THANKS!
38