Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

College of Technology – Kandy - 2021

ICT – Level 5 (Weekend) : Database Systems II


Lesson 4 : Indexing

We know that data is stored in the form of records. Every record has a key field, which helps it to be recognized
uniquely. Indexing is a data structure technique to efficiently retrieve records from the database files based on
some attributes on which the indexing has been done. Indexing in database systems is similar to what we see in
books. Indexing is defined based on its indexing attributes. Indexing can be of the following types:

Primary Index : Primary index is defined on an ordered data file. The data file is ordered on a key field. The key
field is generally the primary key of the relation. In the primary Index, there is always one to one relationship
between the entries in the index table. The primary Indexing is also further divided into two types.
➢ Dense Index
➢ Sparse Index
Dense Index : In dense index, there is an index record for every search key value in the database. This makes
searching faster but requires more space to store index records itself. Index records contain search key value and a
pointer to the actual record on the disk.
Examples :

Sparse Index : In sparse index, index records are not created for every
search key. Only index record that appears for only some of the values in
the file. An index record here contains a search key and an actual pointer
to the data on the disk. To search a record, we first proceed by index
record and reach at the actual location of the data. If the data we are
looking for is not where we directly reach by following the index, then the
system starts sequential search until the desired data is found.
Sparse Index helps
you to resolve the
issues of dense Indexing. However, sparse Index stores index
records for only some search-key values. It needs less space,
less maintenance overhead for insertion, and deletions but It
is slower compared to the dense Index for locating records.

@ Sameera Withanachchi
M.Sc in Computer Science, B.Sc., ACS
Secondary Index : The secondary Index can be generated by a field which has a unique value for each record, and
it should be a candidate key. It is also known as a non-clustering index.
This two-level database indexing technique is used to reduce the mapping size of the first level. For the first level, a
large range of numbers is selected because of this; the mapping size always remains small.

@ Sameera Withanachchi
M.Sc in Computer Science, B.Sc., ACS
Clustering Index: In a clustered index, records themselves are stored in the Index and not pointers. Sometimes
the Index is created on non-primary key columns which might not be unique for each record. This also helps you to
identify the record faster. By using the cluster indexing we can reduce the cost of searching reason being multiple
records related to the same thing are stored at one place. Clustering index is defined on an ordered data file. The
data file is ordered on a non-key field. In some cases, the index is created on non-primary key columns which may
not be unique for each record. In such cases, in order to identify the records faster, we will group two or more
columns together to get the unique values and create index out of them. This method is known as the clustering
index. Basically, records with similar characteristics are grouped together and indexes are created for these groups.
In such a situation, you can group two or more columns to get the unique values and create an index which is called
clustered Index.

Example 1:
Let's assume that a company recruited many employees in various departments. In this case, clustering indexing
should be created for all employees who belong to the same dept.
Here, Department _no is a non-unique key.

Example 2:
For example, students studying in each semester are grouped together. i.e. 1st Semester students, 2nd semester
students, 3rd semester students etc are grouped.

@ Sameera Withanachchi
M.Sc in Computer Science, B.Sc., ACS
Multilevel Index : With the growth of the size of the database, indices also grow. As the index is stored in the main
memory, a single-level index might become too large a size to store with multiple disk accesses. The multilevel
indexing segregates the main block into various smaller blocks so that the same can stored in a single block. The
outer blocks are divided into inner blocks which in turn are pointed to the data blocks. This can be easily stored in
the main memory with fewer overheads.

Index records comprise search-key values and data pointers. Multilevel index is stored on the disk along with the actual
database files.

Multi-level Index helps in breaking


down the index into several smaller
indices in order to make the outermost
level so small that it can be saved in a
single disk block, which can easily be
accommodated anywhere in the main
memory.

@ Sameera Withanachchi
M.Sc in Computer Science, B.Sc., ACS
Creating MySQL Indexes
Indexes are used to retrieve data from the database more quickly than otherwise. The users cannot see the indexes,
they are just used to speed up searches/queries.

1. Creating a MySQL Index - New Table


If you are creating a new MySQL table you can specify a column to index by using the INDEX term as it has
below. It has created two fields: name and employeeID (index).

MySQL Code:
CREATE TABLE employee_records ( name VARCHAR(50), employeeID INT, INDEX (employeeID) )

2. Creating a MySQL Index - Existing Table


You can also add an index to an older table that you think would benefit from some indexing. The syntax is very
similar to creating an index in a new table.
MySQL Code:
CREATE INDEX id_index ON tbl_name(column_name)

3. Displaying INDEX Information:


You can use SHOW INDEX command to list out all the indexes associated with a table. Vertical-format output
(specified by \G) often is useful with this statement, to avoid long line wraparound:
Ex :
SHOW INDEX FROM table_name\G
SHOW INDEX FROM mytable FROM mydb;
SHOW INDEX FROM mydb.mytable;

4. Using ALTER command to add and drop INDEX


➢ ALTER TABLE tbl_name ADD INDEX index_name (column_list)
➢ ALTER TABLE tbl_name DROP INDEX index_name;

5. Creates an index on a table. Duplicate values are allowed:


CREATE INDEX index_name
ON table_name (column1, column2, ...);

6. CREATE UNIQUE INDEX Syntax


Creates a unique index on a table. Duplicate values are not allowed:

CREATE UNIQUE INDEX index_name


ON table_name (column1, column2, ...);

@ Sameera Withanachchi
M.Sc in Computer Science, B.Sc., ACS
Advantages of Indexing
✓ It helps to reduce the total number of I/O operations needed to retrieve that data.
✓ Offers faster search and retrieval of data.
✓ Indexing also helps to reduce table space as don't need to link to a row in a table, as there is no need to store
the ROWID in the Index. Thus, it will able to reduce the table space.
✓ It can't sort data in the lead nodes as the value of the primary key classifies it.

Disadvantages of Indexing
✓ To perform the non-clustering indexing, it needs a primary key on the table with a unique value.
✓ It can't perform any other indexes on the Indexed data.
✓ It is not allowed to partition an index-organized table.
✓ SQL Indexing decrease performance in INSERT, DELETE, and UPDATE query.
✓ Additional Disk space: The Clustered index in SQL Server does not require any additional disk space. But each
and every non clustered index requires additional disk space as it is stored separately from the table.

Summary:
✓ Indexing is a small table which is consist of two columns.
✓ Two main types of indexing methods are 1)Primary Indexing 2) Secondary Indexing.
✓ Primary Index is an ordered file which is fixed length size with two fields.
✓ The primary Indexing is also further divided into two types 1)Dense Index 2)Sparse Index.
✓ In a dense index, a record is created for every search key valued in the database.
✓ A sparse indexing method helps you to resolve the issues of dense Indexing.
✓ The secondary Index is an indexing method whose search key specifies an order different from the sequential order
of the file.
✓ Clustering index is defined as an order data file.
✓ Multilevel Indexing is created when a primary index does not fit in memory.
✓ The biggest benefit of Indexing is that it helps you to reduce the total number of I/O operations needed to retrieve
that data.
✓ The biggest drawback to performing the indexing database management system, you need a primary key on the
table with a unique value.

@ Sameera Withanachchi
M.Sc in Computer Science, B.Sc., ACS

You might also like