How is data stored and retrieved inside a disk in a computer?
Introduction
Have you ever wondered how the data is stored and retrieved inside a disk in a computer. Learning about this basic concept would help you to understand about the flow of execution of a program in Operating System. If you are working with Databases, for example SQL database you could understand to a deeper level about why do we want indexes and how they make data retrieval faster. In the following section we will go over the following topics.
- Disk Structure.
- Program Execution Flow
- How data is organized on the disk in form of a database.
If you like the following article I would suggest you to subscribe to my blogs.
Disk Structure
The diagram of computer disk is shown below. It consists of multiple concentric circles(logical not actually physical) which are called tracks. The disk is also partitioned from center to the end of disk, which are called sectors. The intersection between a track and a sector is called a block.
- Track number
- Sector number
- Offset
Program Execution Flow
When any program is run on a computer the operating system reads the data from the disk and brings it to the main memory(ram). It needs to load it inside the main memory for running the logic of the program. We need to learn about the Data structures to handle the data in the main memory and learn about DBMS (Database Management System) for organizing data on the disk in an efficient way.
How is data organized on the disk in form of database
Suppose we have a table for storing information about students. It has the following columns in the table.
- Id (10 bytes)
- Name (50 bytes)
- Department (10 bytes)
- Section ( 8 bytes)
- Address (50 bytes)
So in total every record in the table will take 128 bytes. So every block will contain at most 512/128 = 4 records. Suppose we have 100 records in our database we will need 100/4 = 25 blocks to store the data.
If we need to access any record in the table we will need to scan through 25 blocks to find the record. The access time will be slower. We can make it faster by creating an index in the table.