File Organization
•Database is a collection of file.
•Each file is a collection of Records.
•Each Record is a sequence of Fields.
Database
Files Blocks
Records
Blocking Factor: average no. of
records per block.
Fields
1
7/31/2018
2
7/31/2018
Index
Indexes are used to improve searching efficiency.
Index is a record consist of two fields:
Key Block Pointer
• Block Pointer : points to a block where ‘Key’ is available.
• Index is an ordered file.
• Searching : Binary Search.
• To access a record using index, the average no. of block access =
log 2 Bi + 1 (Here Bi = No. of Blocks in the index file.)
Types of Index
A. Single Level Index:
1. Primary Index (PK + Ordered)
2. Clustered Index (Non-Key + Ordered)
3. Secondary Index (Non-Key/Cand. Key + Un-Ordered)
3
7/31/2018
Primary Index
1. It is created on primary key of table. Hence there should
be only one primary index per table.
2. Both data files and index files are ordered.
3. No. of index records=No. of data pages.
4. It is an example of sparse index.
5. It consists two attributes:
a. Key Value
b. Block Pointer
6. First record in each data page is called anchor record
Primary Index
Assume total no. of records=30,000 Case A:
Record size = 100 bytes Searches required without
Page size = 1024 bytes Indexes:
No. of records per page = 1024/100 = 10 (Binary Search) log 2 3000 = 12
records
No of pages required = 30,000/10 = 3,000
pages
Case B:
Assume Key = 9 bytes Searches required with Indexes:
Ptr = 6 bytes (Binary Search) log 2 45 = 6 (from
Length of index record = 9+6 =15 here moving to data page we
No. of Index records per page =1024/15 =64 need one more search)
We have Total Search = 6+1 = 7
No. of index records=No. of data pages =
3000
No of index pages = 3000/64 = 45
4
7/31/2018
Clustered Index
• Primary index is created on unique value
columns where as clustered index is created on
cluster value column
Eg: branch, year etc.
• It is also example for sparse index
• We can have only one clustered index per table.
• Both data pages and index pages are ordered.
Secondary Index
1. It is created on columns other than
primary key and clustered columns.
2. Data pages are not ordered but index
pages are ordered.
3. It is an example for dense index.
4. We can have multiple secondary indices
per table.
5
7/31/2018
Secondary Index
Assume total no. of records=30,000 Case A:
Record size = 100 bytes Searches required without Index:
Page size = 1024 bytes As data pages are not ordered -
No. of records per page = 1024/100 = 10 Linear search
records on average = 3000/2 = 1500
No of pages required = 30,000/10 = 3,000 searches
pages
Case B:
Assume Key = 9 bytes Searches required with Index:
Ptr = 6 bytes Log 2 450 = 9
Length of index record = 9+6 =15 Total = 9+1 = 10 Searched
No. of Index records per page =1024/15 =64
We have
No. of index records=No. of data pages =
3000
No of index pages = 3000/64 = 450