Query optimization with EXPLAIN in Supabase - Time & Space Complexity
When we run database queries, some take longer than others. Using EXPLAIN helps us see how the database plans to run a query.
We want to understand how the time to run a query grows as the data size grows.
Analyze the time complexity of a SELECT query with EXPLAIN.
const { data, error } = await supabase.rpc('explain_query', {
query: 'EXPLAIN SELECT * FROM users WHERE age > 30'
});
This runs EXPLAIN on a query to see how the database will execute it.
Look for repeated actions that affect time.
- Primary operation: The database scans or indexes rows to find matches.
- How many times: Depends on number of rows in the table scanned or indexed.
As the number of rows grows, the database work grows too.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 | About 10 row checks |
| 100 | About 100 row checks |
| 1000 | About 1000 row checks |
Pattern observation: The work grows roughly in direct proportion to the number of rows scanned.
Time Complexity: O(n)
This means the time to run the query grows roughly in a straight line with the number of rows checked.
[X] Wrong: "EXPLAIN makes the query run faster by itself."
[OK] Correct: EXPLAIN only shows the plan; it does not speed up the query execution.
Understanding how query time grows helps you design better databases and write faster queries, a useful skill in many cloud roles.
"What if we add an index on the age column? How would the time complexity change?"
Practice
EXPLAIN in Supabase queries?Solution
Step 1: Understand the role of EXPLAIN
EXPLAIN shows the query plan, which is how the database intends to run the query.Step 2: Differentiate EXPLAIN from other commands
It does not execute or change data; it only provides information about query execution.Final Answer:
To see how the database plans to run your query -> Option BQuick Check:
EXPLAIN = query plan info [OK]
- Thinking EXPLAIN runs the query faster
- Confusing EXPLAIN with data modification commands
- Assuming EXPLAIN creates or deletes tables
Solution
Step 1: Recall the syntax for detailed query execution
To get real execution details, useEXPLAIN ANALYZEbefore the query.Step 2: Check each option's syntax
EXPLAIN ANALYZE SELECT * FROM users; correctly placesEXPLAIN ANALYZEbefore the SELECT statement; others are incorrect or invalid.Final Answer:
EXPLAIN ANALYZE SELECT * FROM users; -> Option AQuick Check:
EXPLAIN ANALYZE = detailed execution [OK]
- Swapping EXPLAIN and ANALYZE order
- Placing EXPLAIN inside SELECT
- Using ANALYZE without EXPLAIN
Solution
Step 1: Understand sequential scan meaning
A sequential scan reads every row in the table, which can be slow for large tables.Step 2: Compare with index scan
Index scans read only relevant rows, making queries faster; sequential scans do not use indexes.Final Answer:
The query might be slow because it reads the whole table -> Option DQuick Check:
Seq scan = full table read, slower [OK]
- Assuming sequential scan uses indexes
- Thinking sequential scan reads only part of table
- Confusing failure with slow performance
EXPLAIN ANALYZE SELECT * FROM orders WHERE customer_id = 123; but notice the query is slow. What is a likely cause and fix?Solution
Step 1: Analyze slow query cause
Slow queries filtering by a column often mean missing indexes on that column.Step 2: Suggest fix based on EXPLAIN ANALYZE
Adding an index oncustomer_idhelps the database find rows faster, improving speed.Final Answer:
No index on customer_id; add an index on that column -> Option AQuick Check:
Missing index = slow filter; add index [OK]
- Assuming syntax error causes slowness
- Restarting server without checking query plan
- Thinking missing table causes slow query
Solution
Step 1: Understand nested loops impact
Nested loops join can be slow on large tables without proper indexes on join columns.Step 2: Apply optimization techniques
Adding indexes on join columns helps the database find matching rows faster. Using explicit JOIN syntax clarifies the query for optimization.Final Answer:
Add indexes on join columns and rewrite query to use explicit JOIN syntax -> Option CQuick Check:
Indexes + clear JOIN = faster joins [OK]
- Removing indexes causing slower scans
- Splitting queries losing join benefits
- Relying only on hardware upgrades
Ready to prove it?
Pass the test and this pattern moves unknown
