0
0
SQLquery~10 mins

Index impact on INSERT and UPDATE in SQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Index impact on INSERT and UPDATE
Start INSERT/UPDATE
Check for Indexes on Table
If Index Exists?
NoInsert/Update Data Directly
Yes
Insert/Update Data in Table
Update All Relevant Indexes
Finish Operation
When inserting or updating data, the database checks if indexes exist. If yes, it updates those indexes after changing the data, which adds extra steps and time.
Execution Sample
SQL
INSERT INTO employees (id, name) VALUES (1, 'Alice');
UPDATE employees SET name = 'Alicia' WHERE id = 1;
Insert a new employee and then update the employee's name, showing how indexes affect these operations.
Execution Table
StepOperationIndex Present?ActionResult
1Start INSERTYesInsert row into tableRow inserted
2Update IndexesYesUpdate index entries for new rowIndexes updated
3Finish INSERTYesComplete operationInsert done
4Start UPDATEYesFind row by indexRow found
5Update row dataYesChange name fieldData updated
6Update IndexesYesUpdate index entries for changed dataIndexes updated
7Finish UPDATEYesComplete operationUpdate done
8Start INSERTNoInsert row into tableRow inserted
9Finish INSERTNoComplete operationInsert done
10Start UPDATENoFind row by scanRow found
11Update row dataNoChange name fieldData updated
12Finish UPDATENoComplete operationUpdate done
💡 Operations finish after data and indexes are updated (if indexes exist), or just data if no indexes.
Variable Tracker
VariableStartAfter InsertAfter UpdateFinal
Table Rows0111
Index Entries0111
Data Field 'name'N/A'Alice''Alicia''Alicia'
Key Moments - 3 Insights
Why does updating a row take longer when indexes exist?
Because after changing the data, the database must also update all related index entries, as shown in execution_table rows 5 and 6.
What happens if there are no indexes during an INSERT?
The database inserts the row directly without extra steps for indexes, as seen in execution_table rows 8 and 9.
How does the database find the row to update when indexes exist?
It uses the index to quickly locate the row, shown in execution_table row 4, instead of scanning the whole table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what action happens at step 6?
AUpdate index entries for changed data
BInsert row into table
CComplete operation
DFind row by scan
💡 Hint
Check the 'Action' column at step 6 in execution_table.
At which step does the database finish the UPDATE operation when indexes exist?
AStep 5
BStep 6
CStep 7
DStep 8
💡 Hint
Look for 'Finish UPDATE' in the 'Operation' column with 'Yes' in 'Index Present?'
If the table has no indexes, how does the database find the row to update?
AUsing the index
BBy scanning the whole table
CIt cannot find the row
DUsing a cache
💡 Hint
See execution_table row 10 under 'Action' for no index case.
Concept Snapshot
Index impact on INSERT and UPDATE:
- When inserting or updating, indexes must be updated if present.
- This adds extra steps and time to the operation.
- Without indexes, data changes happen directly.
- Indexes speed up finding rows but slow down writes.
- Balance indexes for read vs write performance.
Full Transcript
When you insert or update data in a table, the database first checks if there are any indexes on that table. If indexes exist, after inserting or updating the data, the database must also update the index entries to keep them accurate. This means more steps and more time are needed compared to tables without indexes. For example, when inserting a new employee, the row is added to the table, then the indexes are updated. When updating the employee's name, the database uses the index to find the row quickly, updates the data, then updates the indexes again. Without indexes, the database inserts or updates data directly and finds rows by scanning the table, which can be slower for large tables but faster for writes. This shows the trade-off: indexes speed up reading but slow down inserting and updating.