You are on page 1of 6

7/31/2018

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

Strategies for storing records into block


1. Spanned Strategy
It allows, partial parts of record 100 Bytes
R=30 Bytes
can be stored in a block.
R1 R2 R3 R4 B1

Advantage: No waste of memory.


Disadvantage: Block access R4 R5 R6 R7 B2
increases.
Suitable: It is suitable for variable
R7 ……. B3
length records.

1
7/31/2018

Strategies for storing records into block


2. Un-Spanned Strategy
No records can be stored in 100 Bytes
R=30 Bytes
more than one blocks.
R1 R2 R3 == B1

Advantage: Block access reduced. B2


R4 R5 R6 ==
Disadvantage: Wastage of memory.
Suitable: It is suitable for fixed length
records.

Organization of Records in a File


1. Ordered File Organization: All the records are ordered
based on some search key value.
Searching : Binary Search.
To access a record, the average no. of block access=log2 B blocks
B= Data Blocks
Advantage: Searching is efficient.
Disadvantage: Insertion is expensive due to reorganization of entire file.
2. Un-Ordered File Organization: All the records are inserted
where ever the place is available, usually at the end of the file.
Searching : Linear Search.
To access a record, the average no. of block access= B/2 blocks
B= Data Blocks
Advantage: Insertion is efficient.
Disadvantage: Searching is inefficient compared to ordered file organization.

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)

B. Multi Level Index:


1. B Tree
2. B+ Tree
Another Classification of Index
A. Dense Index : If an index entry is created for every search key
value that is called dense index.

B. Sparse Index : If an index entry is created only for some search


key values it is called sparse index.

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

You might also like