What is a database in SQL - Complexity Analysis
When we talk about databases, we want to know how fast they can find or save information as the amount of data grows.
We ask: How does the work needed change when the database gets bigger?
Analyze the time complexity of the following SQL query.
SELECT *
FROM customers
WHERE city = 'New York';
This query looks through the customers table to find all customers who live in New York.
Look at what repeats when the query runs.
- Primary operation: Checking each customer's city value.
- How many times: Once for every customer in the table.
As the number of customers grows, the work grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The work grows directly with the number of customers.
Time Complexity: O(n)
This means the time to run the query grows in a straight line as the table gets bigger.
[X] Wrong: "The query time stays the same no matter how many customers there are."
[OK] Correct: The database must check each row to see if the city matches, so more rows mean more work.
Understanding how query time grows helps you explain how databases handle more data, a useful skill in many jobs.
"What if we add an index on the city column? How would the time complexity change?"