Why databases over files in SQL - Performance Analysis
We want to understand why using databases is often faster than using simple files for storing and retrieving data.
How does the time to find or update data grow as the amount of data grows?
Analyze the time complexity of searching data in a file versus a database.
-- Searching a record in a file (conceptual)
-- This represents scanning a file line by line
-- Not actual SQL syntax for file search
-- Searching a record in a database table with an index
SELECT * FROM users WHERE id = 123;
The first is like scanning a file line by line. The second uses a database index to find data quickly.
Look at what repeats when searching data.
- Primary operation: Checking each record one by one in the file search.
- How many times: As many as the number of records (n) in the file.
In the database with an index, the search jumps directly to the record without checking all.
As the file grows, searching takes longer because it checks more records.
| Input Size (n) | Approx. Operations (File Search) |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The time grows directly with the number of records.
For the database with an index, the search time grows much slower, jumping quickly to the result.
Time Complexity: O(n) for file search, O(log n) for database index search
This means searching in a file takes longer as data grows, but a database can find data much faster even when data is large.
[X] Wrong: "Searching a file is always just as fast as a database because both store data."
[OK] Correct: Files usually require checking each record one by one, while databases use smart ways like indexes to jump directly to the data.
Understanding how data search time grows helps you explain why databases are preferred for large data, showing you know practical efficiency.
"What if the file was sorted? How would that change the time complexity of searching in the file?"