Why DML operations modify data in MySQL - Performance Analysis
When we use commands that change data in a database, like adding or updating rows, it takes some time to do that work.
We want to understand how the time needed grows as we change more data.
Analyze the time complexity of the following code snippet.
INSERT INTO employees (name, position) VALUES ('Alice', 'Manager');
UPDATE employees SET position = 'Senior Manager' WHERE name = 'Alice';
DELETE FROM employees WHERE name = 'Alice';
This code adds a new employee, updates their position, then removes them from the table.
- Primary operation: Searching rows to find where to insert, update, or delete.
- How many times: Depends on how many rows the database checks to find the right place.
As the table gets bigger, the database may need to check more rows to find where to change data.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 checks |
| 100 | About 100 checks |
| 1000 | About 1000 checks |
Pattern observation: The work grows roughly in direct proportion to the number of rows.
Time Complexity: O(n)
This means the time to modify data grows roughly in step with the number of rows in the table.
[X] Wrong: "Modifying one row always takes the same time no matter how big the table is."
[OK] Correct: The database often needs to look through many rows to find the right one, so bigger tables usually take more time.
Understanding how data changes take longer with bigger tables helps you explain how databases work efficiently in real projects.
"What if we add an index on the column used in the WHERE clause? How would the time complexity change?"
Practice
Solution
Step 1: Understand the purpose of DML commands
DML commands modify data in tables by adding, changing, or removing rows.Step 2: Identify the command for adding data
INSERTadds new rows,UPDATEchanges existing rows,DELETEremoves rows, andSELECTonly reads data.Final Answer:
INSERT -> Option DQuick Check:
Add new rows = INSERT [OK]
- Confusing UPDATE with INSERT
- Thinking SELECT modifies data
- Using DELETE to add data
users?Solution
Step 1: Recognize the correct DML command for changing data
To change existing data,UPDATEis used withSETto assign new values.Step 2: Check syntax correctness
UPDATE users SET email = 'new@example.com' WHERE id = 5;correctly updates the email for user with id 5.Final Answer:
UPDATE users SET email = 'new@example.com' WHERE id = 5; -> Option CQuick Check:
Update data uses UPDATE + SET + WHERE [OK]
- Using INSERT instead of UPDATE to modify data
- Omitting WHERE clause causing all rows to update
- Using DELETE or SELECT incorrectly
products with columns id, name, and price, what will be the result of this query?UPDATE products SET price = price * 1.1 WHERE id = 3;
Solution
Step 1: Understand the UPDATE statement with WHERE clause
The query updates only rows whereid = 3, so only that product is affected.Step 2: Analyze the price update expression
The price is multiplied by 1.1, increasing it by 10% for the product with id 3.Final Answer:
The price of the product with id 3 will increase by 10%. -> Option AQuick Check:
UPDATE with WHERE affects specific rows [OK]
- Assuming all rows update without WHERE
- Confusing UPDATE with DELETE
- Thinking syntax error occurs without WHERE
DELETE users WHERE id = 10;
Solution
Step 1: Recall correct DELETE syntax
The correct syntax isDELETE FROM table_name WHERE condition;. TheFROMkeyword is required.Step 2: Identify the missing keyword
The statement missesFROMafterDELETE, causing a syntax error.Final Answer:
Missing FROM keyword after DELETE. -> Option AQuick Check:
DELETE requires FROM before table name [OK]
- Omitting FROM keyword
- Using DELETE without WHERE to remove specific rows
- Quoting table names unnecessarily
employees table who work in the 'Sales' department. Which query correctly performs this update?Solution
Step 1: Choose the correct DML command for modifying existing data
To change salaries,UPDATEis used, notINSERTorDELETE.Step 2: Verify correct syntax order
The correct syntax isUPDATE table SET column = value WHERE condition;. UPDATE employees SET salary = salary * 1.05 WHERE department = 'Sales'; follows this order correctly.Final Answer:
UPDATE employees SET salary = salary * 1.05 WHERE department = 'Sales'; -> Option BQuick Check:
UPDATE + SET + WHERE with correct order [OK]
- Using INSERT to modify data
- Placing WHERE before SET in UPDATE
- Deleting rows instead of updating
Ready to prove it?
Pass the test and this pattern moves unknown
