100% found this document useful (1 vote)
91 views38 pages

Indexes in Database

The document discusses indexing in databases. It begins by defining indexes as data structures that allow quick retrieval of records from database files. The document then covers index structure using B-trees, why indexes are used, how to create indexes in Oracle including the syntax, and different types of indexes like primary, clustered, and secondary indexes. It concludes by listing benefits of indexes such as faster query results, sorting, and retrieval enabled by indexes.

Uploaded by

Sobia Batool
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
91 views38 pages

Indexes in Database

The document discusses indexing in databases. It begins by defining indexes as data structures that allow quick retrieval of records from database files. The document then covers index structure using B-trees, why indexes are used, how to create indexes in Oracle including the syntax, and different types of indexes like primary, clustered, and secondary indexes. It concludes by listing benefits of indexes such as faster query results, sorting, and retrieval enabled by indexes.

Uploaded by

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

INDEXING IN

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.

⬥ Basically improves the speed of data retrieval operations on


a database table.

3
Index Structure
⬥ Indexes can be created using some database columns.

⬥ The first column of the database is the search key that


contains a copy of the primary key or candidate key of the
table.

⬥ The second column of the database is the data reference. It


contains a set of pointers holding the address of the disk
block where the value of the particular key can be found.

4
Example
⬥ Suppose we have an employee table with thousands of
record and each of which is 10 bytes long.

⬥ If their IDs start with 1, 2, 3....and so on and we have to


search student with ID-543.

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.

⬥ In the case of an index, we will search using indexes and the


DBMS will read the record after reading 542*2= 1084 bytes
which are very less compared to the previous case.

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.

⬥ An index normally includes a "key" or direct link to the


original row of data from which it was copied, to allow the
complete row to be retrieved efficiently.

⬥ 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.

⬥ Index creates an entry for each value that appears


in the indexed columns.

⬥ Oracle creates B-tree indexes.

12
B-tree Indexes
⬥ Referred to as balanced tree.

⬥ A B-tree index is an ordered list of values divided


into ranges.

⬥ By associating a key with a row or range of rows,


B-trees provide excellent retrieval performance for
a wide range of queries.
13
Structure

14
Structure (Continue)
A binary tree index has two types of blocks
⬥ Branch Block
For searching a particular index

The upper-level branch blocks of a B-tree index contain


index data that points to lower-level index blocks.

⬥ Leaf Block
For storing the data

15
Create an Index
⬥ Syntax
The syntax for creating an index in Oracle is:

CREATE [UNIQUE] INDEX index_name


ON table_name (column1, column2, ... column_n)
[ COMPUTE STATISTICS ];

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);

In this example, we've created an index on the supplier table called


supplier_idx. It consists of only one field - the supplier_name field.

⬥ 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:

CREATE INDEX supplier_idx


ON supplier (supplier_name, city)
COMPUTE STATISTICS;

19
Rename An Index
⬥ Syntax
The syntax for renaming an index in Oracle is:

ALTER INDEX index_name


RENAME TO new_index_name;

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:

ALTER INDEX supplier_idx


RENAME TO supplier_index_name;

In this example, we're renaming the index called supplier_idx to


supplier_index_name.

22
Drop An Index
⬥ Syntax
The syntax for dropping an index in Oracle is:

DROP INDEX index_name;

 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.

⬥ As primary keys are stored in sorted order, the performance


of the searching operation is quite efficient.

⬥ The primary index can be classified into two types: Dense


index and Sparse index.
26
Dense Index
 The dense index contains an index record for every search
key value in the data file. It makes searching faster.

 In this, the number of records in the index table is same as


the number of records in the main table.

 It needs more space to store index record itself. The index


records have the search key and a pointer to the actual record
on the disk.

27
28
Sparse Index
 In the data file, index record appears only for a few items.
Each item points to a block.

 In this, instead of pointing to each record in the main table,


the index points to the records in the main table in a gap.

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.

 In this case, to identify the record faster, we will group two


or more columns to get the unique value and create index out
of them. This method is called a clustering index.

 The records which have similar characteristics are grouped,


and indexes are created for these group.
31
Secondary Index
 It is a two-level indexing technique used to reduce the
mapping size of the primary index.

 The secondary index points to a certain location where the


data is to be found but the actual data is not sorted like in the
primary indexing.

 Secondary Indexing is also known as non-clustered


Indexing.

32
33
5 Benefits of Indexes
Benefits
⬥ Indexing helps in faster query results or quick data retrieval.

⬥ Indexing helps in faster sorting and grouping of records

⬥ Some Indexing uses sorted and unique keys which helps to


retrieve sorted queries even faster.

⬥ Index tables are smaller in size so require lesser memory.

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.

⬥ Indexing helps in better CPU utilization and better


performance.

⬥ As Index tables are smaller in size, they are stored in the


main memory.

36
ANY
QUESTIONS?

37
THANKS!

38

You might also like