Why SELECT is the most important command in SQL - Performance Analysis
We want to understand how the time it takes to run a SELECT query changes as the data grows.
How does the size of the data affect the work the database must do to get results?
Analyze the time complexity of the following SQL SELECT query.
SELECT *
FROM employees
WHERE department = 'Sales';
This query finds all employees who work in the Sales department.
Look for repeated actions that take time.
- Primary operation: Checking each employee's department value.
- How many times: Once for every employee in the table.
As the number of employees grows, the database checks more rows.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The work grows directly with the number of employees.
Time Complexity: O(n)
This means the time to run the query grows in a straight line with the number of rows.
[X] Wrong: "SELECT queries always run instantly no matter the data size."
[OK] Correct: The database must check each row to find matches, so more data means more work and longer time.
Understanding how SELECT queries scale helps you explain database performance clearly and shows you know how data size affects speed.
"What if we add an index on the department column? How would the time complexity change?"