File organisation and blocking factor
How records are stored on disk: blocks, records per block (the blocking factor), the number of blocks of a file for fixed-length records (unspanned and spanned), the cost of a block access, and the three basic file organisations (heap, sorted, hashed) with the number of block accesses for search, insertion, deletion and range queries; the textbook binary-search formula ceil(log2 b) against the exact count floor(log2 b) + 1, checked by measuring searches on generated files.
Why it exists
A database lives on disk, and a disk is read and written in blocks. The time of a query is dominated by the number of blocks it touches, so the basic questions of file organisation are numerical: how many records fit in a block, how many blocks does the file need, how many blocks must be read to find a record. These calculations appear in almost every GATE paper.
Prerequisites
You need the structure of a disk: surfaces, tracks, sectors and blocks, and the three delays of an access (secondary storage and disks). The essentials are repeated below.
Precise definitions
- Block (page): the unit of transfer between disk and memory; size bytes (typical B to KB).
- Block access time = seek time (move the head to the track) + rotational latency (wait for the sector; on average half a rotation) + transfer time ( divided by the transfer rate). Example: 7200 rpm gives ms per rotation and ms average rotational latency; with an average seek of ms and a transfer of a KB block at MB/s ( ms) a block access takes about ms. Reading consecutive blocks avoids repeated seeks and is much cheaper per block.
- Record: a row, stored as a sequence of fields of fixed length bytes (variable-length records need length fields or separators and are not used in the calculations below).
- Blocking factor : the number of records that fit completely in one block. The unused space per block is bytes.
- Number of blocks for records (unspanned): .
- Spanned organisation: a record may be split over two blocks (with a pointer to the continuation). No space is wasted, so the file needs blocks. Unspanned: every record lies completely in one block (the usual case; it requires ).
- A record pointer is a block address plus the offset inside the block.
Three basic file organisations: heap (unordered): records in insertion order; sorted (ordered): records ordered by a key field; hashed: the block of a record is computed from the key by a hash function.
How it works
Records, blocks and searches
The sizes for four parameter sets:
Records, blocks and search cost: start
Computed by a script, not typed| parameters | records per block (bf) | blocks b (unspanned) | bytes wasted per block | blocks if spanned | average blocks, unordered file | worst case, unordered file | binary search, textbook ⌈log2 b⌉ | binary search, exact worst case |
|---|---|---|---|---|---|---|---|---|
| B = 1024, R = 100, r = 30 000 | ||||||||
| B = 4096, R = 200, r = 1 000 000 | ||||||||
| B = 512, R = 120, r = 5 000 | ||||||||
| B = 1024, R = 300, r = 10 000 |
Step 0
bf = ⌊B/R⌋, b = ⌈r/bf⌉. For an unordered (heap) file a search for an existing key reads (b + 1)/2 blocks on average. For an ordered file binary search needs ⌈log2 b⌉ block accesses by the usual formula; counting probes exactly gives ⌊log2 b⌋ + 1, which is larger by one when b is a power of 2.
For the first row: , blocks, bytes lost per block; with spanning blocks.
Heap file. Insertion is cheap: append to the last block (read it, write it: 2 accesses, or 1 if the last block is kept in memory). A search on any field must scan the file: for a key that exists, on average blocks, at most ; for a missing key always . Deleting needs the search and a rewrite of the block (the record is marked deleted; space is reclaimed by reorganising periodically).
Sorted file. The records are ordered by a key. A search on the ordering key uses binary search over the blocks: the textbooks (and GATE) give block accesses. The exact worst case is , one more when is a power of two; measured on real sorted files:
Block accesses of a binary search, formula against measurement: start
Computed by a script, not typed| file size | textbook ⌈log2 b⌉ | exact ⌊log2 b⌋ + 1 | measured worst case |
|---|---|---|---|
| b = 1 | |||
| b = 2 | |||
| b = 3 | |||
| b = 4 | |||
| b = 5 | |||
| b = 8 | |||
| b = 9 | |||
| b = 16 | |||
| b = 17 | |||
| b = 100 | |||
| b = 1024 |
Step 0
A sorted file is searched by binary search over its blocks, reading one block per probe. The measured worst case over all keys of the file equals ⌊log2 b⌋ + 1. The textbook formula ⌈log2 b⌉ is the same except when b is a power of 2, where it is one smaller. Exam answers follow the textbook formula.
For both formulas give . A search on any other field is a linear scan. A range query on the ordering key finds the first record by binary search and then reads consecutive blocks. Insertion is expensive: the record must go to its place and the following records shift; with all blocks full and no spare room about half the file is read and rewritten: block reads and as many writes on average (measured). Practical files keep free space in each block or use an overflow area that is merged during reorganisation. Deletion uses a deletion marker.
Hashed file. The block of a record is ; an exact-match search reads one block (plus overflow blocks) but range queries are impossible (hash-based file organisation).
| operation | heap | sorted (on the ordering key) | hashed |
|---|---|---|---|
| equality search | average, worst | about | |
| range search | + consecutive blocks | ||
| insert | to | about (shift) | about to |
| search on a non-key field |
Why indexes are needed
If a table has blocks, even a perfectly sorted file needs block accesses per search, and only for ONE field. An index is a small separate file that finds the block of a record with fewer accesses and works for other fields too (ordered indexes, B+ trees).
Worked examples
EX 1Blocks of a file
Easy, , . ; blocks. No wasted space (). Linear search for an existing key: blocks on average, at worst.
EX 2Spanned or not
Easy, , . Unspanned: , blocks, bytes wasted per block. Spanned: blocks. Unspanned wastes about of the space here.
EX 3Binary search on a sorted file
Mediumrecords of bytes, : blocks. Binary search: block accesses. A linear search of the same file would need on average. For the textbook formula gives and the exact count ; use the textbook formula unless told otherwise.
EX 4Cost of inserting into a sorted file
MediumA sorted file of completely full blocks. Inserting a record at a random position: read and write all blocks from the insertion point to the end to shift the records: on average blocks read and as many written, about accesses. An insertion into a heap file: accesses. This is why sorted files are used for relatively static data.
EX 5Typical questions
GATE level(a) "How many records fit in a block of 1024 bytes if a record has 100 bytes?" . (b) "How many blocks for 30 000 such records?" . (c) "Average blocks searched in an unsorted file of blocks (key present):" (strictly ). (d) "Binary search on a sorted file of blocks:" . (e) "Which organisation gives the fastest equality search on the key?" Hashing. (f) "Which supports range queries on the key efficiently?" Sorted file (and tree indexes), not hashing.
Cheat box
Cheat box
- ; ; spanned: .
- Block access = seek + average rotational latency (half a rotation) + transfer.
- Heap: insert ~–; search average, worst. Sorted: search , insert ~. Hashed: equality ~, no ranges.
- Exact binary-search worst case is ; the textbook gives .
- Cost is counted in block accesses, not in records.
Traps and GATE patterns
Connections
Builds on
Unlocks
Across subjects
Within this subject
Indexes reduce the number of accesses of a search (ordered indexes); tree indexes keep that cost low under insertions (B trees, B+ trees); hashing gives direct addressing (hash files).
PYQs
7 previous year questions from your book. I re-solved each one myself (3 by running or simulating, 4 by reasoning only); the book's key is shown next to my answer. All PYQs
Consider a join (relation algebra) between relations r(R) and s(S) using the nested loop method. There are 3 buffers each of size equal to disk block size, out of which one buffer is reserved for intermediate results. Assuming size(r(R)) < size(s(S)), the join will have fewer number of disk block accesses if:
- (a)relation r(R) is in the outer loop.
- (b)relation s(S) is in the outer loop.
- (c)join selection factor between r(R) and s(S) is more than 0.5.
- (d)join selection factor between r(R) and s(S) is less than 0.5.
Consider the relation enrolled(student, course) in which (student, course) is the primary key, and the relation paid(student, amount), where student is the primary key. Assume no null values and no foreign keys or integrity constraints. Assume that amounts 6000, 7000, 8000, 9000 and 10000 were each paid by 20% of the students. Consider these query plans (Plan 1 on left, Plan 2 on right) to "list all courses taken by students who have paid more than x". A disk seek takes 4ms, disk data transfer bandwidth is 300 MB/s and checking a tuple to see if amount is greater than x takes 10 micro-seconds. Which of the following statements is correct?
- (a)Plan 1 and Plan 2 will not output identical row sets for all databases.
- (b)A course may be listed more than once in the output of Plan 1 for some databases
- (c)For x = 5000, Plan 1 executes faster than Plan 2 for all databases.
- (d)For x = 9000, Plan I executes slower than Plan 2 for all databases.
Consider a database with three relation instances shown below. The primary keys for the Drivers and Cars relation are did and cidrespectively and the records are stored in ascending order of these primary keys as given in the tables. No indexing is available in the database. D: Drivers relation did dname rating age 22 Karthikeyan 7 25 20 Salman 1 33 31 Boris 8 55 32 Amoldt 8 25 58 Schumacher 10 35 64 Sachin 7 35 71 Senna 10 16 74 Sachin 9 35 83 Rahul 3 25 95 Ralph 3 53 R: Reserves relation did cid day 22 101 10/10/06 22 102 10/10/06 22 103 08/10/06 22 104 07/10/06 31 102 10/11/06 31 103 06/11/06 31 104 12/11/06 64 101 05/09/06 64 102 08/09/06 74 103 08/09/06 C: cars celation cid cname colour 101 Renault blue 102 Renault red 103 Ferrari green 104 Jaguar red select D.dname from Drivers D where D.didin ( DATABASE MANAGEMENT SYSTEM select R.did from Cars C, Reserves R where R.cid = C.cid and C.colour = 'red' intersect select R.did from Cars C, Reserves R where R.cid =C.cid and C.colour = 'green' Let n be the number of comparisons performed when the above SQL query is optimally executed. If linear search is used to locate a tuple in a relation using primary key, then n lies in the range.
- (a)36-40
- (b)44-48
- (c)60-64
- (d)100-104
In the context of DBMS, consider the two sets T and S given below. T S I:Logical Schema Ii:Physical Schema III: External Schema L: Views M: File organization and indexes N: Relations Which one of the following is the correct match from T to S?
- (a)I – L, II – M, III – N
- (b)I – M, II – L, III – N
- (c)I – N, II – M, III – L
- (d)I – N, II – L, III – M
Which of the following file organizations is/are I/O efficient for the scan operation in DBMS?
- (a)Sorted
- (b)Heap
- (c)Unclustered tree index
- (d)Unclustered hash index DATABASE MANAGEMENT SYSTEM
A database table T1 has 2000 records and occupies 80 disk blocks. Another table T2 has 400 records and occupies 20 disk blocks. These two tables have to be joined as per a specified join condition that needs to be evaluated for every pair of records from these two tables. The memory buffer space available can hold exactly one block of records for T1 and one block of records for T2 simultaneously at any point in time. No index is available on either table. If Nested-loop join algorithm is employed to perform the join, with the most appropriate choice of table to be used in outer loop, the number of block accesses required for reading the data are .
- (a)800000
- (b)40080
- (c)32020
- (d)100
A database table T1 has 2000 records and occupies 80 disk blocks. Another table T2 has 400 records and occupies 20 disk blocks. These two tables have to be joined as per a specified join condition that needs to be evaluated for every pair of records from these two tables. The memory buffer space available can hold exactly one block of records for T1 and one block of records for T2 simultaneously at any point in time. No index is available on either table. If, instead of Nested-loop join, Block nested-loop join is used, again with the most appropriate choice of table in the outer loop, the reduction in number of block accesses required for reading the data will be
- (a)0
- (b)30400
- (c)38400
- (d)798400
Self-check
Question 1
, , . How many blocks (unspanned)?
Show answerHide
, .
Question 2
A sorted file has 1024 blocks. How many block accesses does a binary search need by the textbook formula? By exact counting?
Show answerHide
; exactly .
Question 3
Why is inserting into a sorted file expensive?
Show answerHide
The records after the insertion point must be shifted, so about half the blocks are read and rewritten.
Question 4
What is the average rotational latency of a 6000 rpm disk?
Show answerHide
One rotation is ms, so the average latency is ms.
Revision card
One-screen revision
- , ; spanned .
- Access time = seek + half rotation + transfer.
- Heap search; sorted ; hash ~1.
- Sorted insertion ~ block accesses.
References
- ·Silberschatz, Korth, Sudarshan, Database System Concepts (7th edition), Chapter 13 (data storage structures), Sections 13.1 to 13.3
- ·Elmasri, Navathe, Fundamentals of Database Systems (7th edition), Chapter 16 (disk storage, basic file structures and hashing), Sections 16.1 to 16.7
Verification
scripts/enum-checks/db_index_check.py: for 400 random parameter sets the number of blocks obtained by packing records into blocks one by one equals ceil(r / floor(B / R)) (unspanned) and ceil(r R / B) (spanned); on generated sorted files of 1 to 100 blocks the worst-case number of blocks read by a binary search over the blocks, measured over every key of the file, equals floor(log2 b) + 1, which is one more than the textbook ceil(log2 b) exactly when b is a power of two; a linear search of an unordered file with full blocks reads exactly (b + 1) / 2 blocks on average over the existing keys and b blocks at worst; inserting into a sorted file with full blocks and no spare room rewrites (b + 1) / 2 blocks on average; the disk-time arithmetic (7200 rpm: 8.33 ms per rotation, 4.17 ms average rotational delay) was recomputed. The textbook formula ceil(log2 b) is what exam answers use. Disk parameters are typical numbers, not measurements. Textbook chapter references are from memory. SECOND PASS (2026-11-02, done by the assistant, not a human check): every Self-check question was solved before the printed answer was read and all agreed; the definitions, worked examples, cheat box and traps were read claim by claim and re-derived against the textbook definitions and the sample database. No defect found. Status raised to self-checked; verified needs a textbook or human check.
last verified: never
My notes (saved in this browser)