0
0
MySQLquery~10 mins

Index maintenance in MySQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Index maintenance
Create or Alter Table
Add/Modify Index
Index Structure Updated
Data Insert/Update/Delete
Index Entries Updated
Query Uses Index for Faster Access
This flow shows how indexes are created or modified, then maintained automatically as data changes, helping queries run faster.
Execution Sample
MySQL
CREATE TABLE employees (
  id INT PRIMARY KEY,
  name VARCHAR(50),
  dept_id INT,
  INDEX idx_dept (dept_id)
);

INSERT INTO employees VALUES (1, 'Alice', 10);
Creates a table with an index on dept_id, then inserts a row, updating the index.
Execution Table
StepActionIndex StateData StateNotes
1Create table with index idx_dept on dept_idEmpty index structure createdNo dataIndex ready but no entries
2Insert row (1, 'Alice', 10)Index updated with dept_id=10 pointing to row 1One row insertedIndex entry added for dept_id=10
3Insert row (2, 'Bob', 20)Index updated with dept_id=20 pointing to row 2Two rows insertedIndex entry added for dept_id=20
4Update row 1 dept_id to 30Index entry for dept_id=10 removed, dept_id=30 addedRow 1 updatedIndex reflects new dept_id
5Delete row 2Index entry for dept_id=20 removedRow 2 deletedIndex entry removed
6Query: SELECT * FROM employees WHERE dept_id=30Index used to find row 1 quicklyData accessed via indexFaster query due to index
7EndIndex consistent with dataData consistentMaintenance complete
💡 All data changes reflected in index; index ready for fast queries
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5Final
Index entries{}{10: [row1]}{10: [row1], 20: [row2]}{30: [row1], 20: [row2]}{30: [row1]}{30: [row1]}
Table rows{}{1: ('Alice',10)}{1: ('Alice',10), 2: ('Bob',20)}{1: ('Alice',30), 2: ('Bob',20)}{1: ('Alice',30)}{1: ('Alice',30)}
Key Moments - 3 Insights
Why does the index entry for dept_id=10 disappear after step 4?
Because the dept_id value in row 1 changed from 10 to 30, the index must remove the old entry for 10 and add a new entry for 30, as shown in execution_table row 4.
Does deleting a row automatically remove its index entries?
Yes, when a row is deleted, all its index entries are removed to keep the index consistent, as seen in execution_table row 5.
How does the index help the query in step 6?
The index allows the database to quickly find rows with dept_id=30 without scanning the whole table, improving query speed as shown in execution_table row 6.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3. What index entries exist?
AOnly dept_id=10 pointing to row 1
Bdept_id=10 pointing to row 1 and dept_id=20 pointing to row 2
Cdept_id=30 pointing to row 1 only
DNo index entries yet
💡 Hint
Check the 'Index State' column in execution_table row 3
At which step does the index remove an entry due to a row deletion?
AStep 2
BStep 4
CStep 5
DStep 6
💡 Hint
Look for 'Delete row' action in execution_table
If we insert a new row with dept_id=30 after step 5, what happens to the index entries?
AA new entry for dept_id=30 is added alongside existing one
BThe index removes all entries for dept_id=30
CNo change to index entries
DThe index is rebuilt from scratch
💡 Hint
Refer to how index entries are updated on insert in execution_table rows 2 and 3
Concept Snapshot
Index maintenance in MySQL:
- Indexes are created with CREATE TABLE or ALTER TABLE
- Index entries update automatically on INSERT, UPDATE, DELETE
- Updates remove old index entries and add new ones
- Deletions remove index entries
- Queries use indexes to find data faster
- Indexes must always reflect current table data
Full Transcript
Index maintenance in MySQL means keeping the index data structures up to date as table data changes. When you create a table with an index, the index starts empty. When you insert rows, the index adds entries pointing to those rows. If you update a row's indexed column, the index removes the old entry and adds a new one with the updated value. When you delete a row, the index removes its entries. This maintenance ensures queries using the index run faster by quickly locating rows. The execution table shows each step: creating the table, inserting rows, updating a row, deleting a row, and querying using the index. The variable tracker shows how index entries and table rows change after each step. Key moments clarify why index entries change on updates and deletions. The quiz tests understanding of index state at different steps and effects of data changes on the index.