Why INSERT matters in SQL - Performance Analysis
When we add new data to a database using INSERT, it's important to know how the time it takes grows as we add more rows.
We want to understand how the cost of inserting changes when the table gets bigger.
Analyze the time complexity of the following SQL INSERT statement.
INSERT INTO employees (name, position, salary)
VALUES ('Alice', 'Developer', 70000);
This code adds one new employee record to the employees table.
Look for repeated steps that happen when inserting data.
- Primary operation: Writing the new row to the table storage.
- How many times: Once per INSERT statement.
Adding one row takes about the same time no matter how many rows are already in the table.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 insert operation |
| 100 | 1 insert operation |
| 1000 | 1 insert operation |
Pattern observation: The time to insert one row stays about the same as the table grows.
Time Complexity: O(1)
This means inserting one row takes roughly the same time no matter how many rows are already in the table.
[X] Wrong: "INSERT gets slower as the table gets bigger because it has to look at all existing rows."
[OK] Correct: The database usually adds new rows directly without scanning all existing rows, so the time stays steady.
Understanding how INSERT works helps you explain how databases handle adding data efficiently, a useful skill in many real-world projects.
"What if we added an index on the salary column? How would that affect the time complexity of INSERT?"