Query execution plans in DBMS Theory - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When a database runs a query, it creates a plan to get the data efficiently.
We want to understand how the time to run the query grows as the data size increases.
Analyze the time complexity of the following SQL query execution plan snippet.
EXPLAIN SELECT * FROM Orders WHERE CustomerID = 123;
-- The plan shows a table scan or an index seek
-- If no index, full scan of Orders table
-- If index on CustomerID, index seek used
This query fetches all orders for a specific customer using a filter on CustomerID.
Look at how the database checks rows to find matches.
- Primary operation: Scanning rows in the Orders table.
- How many times: Once for each row in the table or index.
As the number of orders grows, the work to find matching rows changes.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 rows checked |
| 100 | 100 rows checked |
| 1000 | 1000 rows checked |
Pattern observation: Without an index, the database checks each row one by one, so work grows directly with data size.
Time Complexity: O(n)
This means the time to run the query grows in direct proportion to the number of rows in the table.
[X] Wrong: "The query always runs fast regardless of table size."
[OK] Correct: Without an index, the database must check every row, so bigger tables take more time.
Understanding how query plans affect time helps you explain how databases handle data efficiently in real projects.
"What if we add an index on CustomerID? How would the time complexity change?"
Practice
query execution plan in a database?Solution
Step 1: Understand what a query execution plan is
A query execution plan explains the steps the database takes to run a query.Step 2: Identify the main purpose
The plan helps users see how the database processes the query to optimize performance.Final Answer:
To show how the database will execute a query step-by-step -> Option BQuick Check:
Query execution plan = execution steps [OK]
- Confusing plans with data storage
- Thinking plans create tables
- Assuming plans backup data
Solution
Step 1: Recall the command to view execution plans
The SQL commandEXPLAINis used to display how a query will be executed.Step 2: Differentiate from other commands
RUNexecutes queries,SHOW PLANis not standard, andDESCRIBEshows table structure.Final Answer:
EXPLAIN -> Option CQuick Check:
View plan = EXPLAIN [OK]
- Using RUN to view plans
- Confusing DESCRIBE with EXPLAIN
- Assuming SHOW PLAN is standard
SELECT * FROM employees WHERE department_id = 5;, what does the execution plan likely show?Solution
Step 1: Analyze the query condition
The query filters employees bydepartment_id = 5. Ifdepartment_idhas an index, the database uses it.Step 2: Understand execution plan behavior
If indexed, the plan shows an index scan to quickly find matching rows instead of scanning the whole table.Final Answer:
An index scan on department_id if indexed -> Option DQuick Check:
Indexed filter = index scan [OK]
- Assuming full table scan always happens
- Expecting join when none exists
- Thinking sorting is automatic
Solution
Step 1: Understand why indexes may be ignored
If a query applies a function (like LOWER or CAST) on an indexed column, the index cannot be used directly.Step 2: Rule out other options
Corrupted indexes are rare and usually cause errors; databases do not always prefer full scans; empty tables do not cause full scans.Final Answer:
The query uses a function on the indexed column -> Option AQuick Check:
Functions on indexed columns block index use [OK]
- Assuming index corruption without error
- Believing full scans are default
- Thinking empty tables cause scans
Solution
Step 1: Identify the cause of slow join
Nested loop joins are slow on large tables without indexes on join columns.Step 2: Apply indexing to improve join method
Creating indexes on join columns allows the database to use faster join methods like hash or merge joins.Step 3: Evaluate other options
Removing indexes slows queries; rewriting to subqueries may not help; increasing cache alone may not fix join inefficiency.Final Answer:
Create indexes on the join columns to enable hash or merge joins -> Option AQuick Check:
Indexes on join keys speed joins [OK]
- Dropping indexes thinking it helps
- Assuming subqueries are always faster
- Relying only on cache size increase
