Database indexes in Supabase - Time & Space Complexity
When we use database indexes, we want to know how they affect the speed of finding data.
We ask: How does the time to find data change as the database grows?
Analyze the time complexity of querying a table with and without an index.
// Query without index
const { data, error } = await supabase
.from('users')
.select('*')
.eq('email', 'example@mail.com')
// Query with index on 'email' column
// (Index created separately in database)
This code fetches user data by email, once without an index and once assuming an index exists.
Look at what happens when the query runs multiple times as data grows.
- Primary operation: Searching rows in the database for matching email.
- How many times: Once per query, but the cost depends on how many rows must be checked.
Without an index, the database checks each row one by one.
| Input Size (n) | Approx. Rows Checked |
|---|---|
| 10 | 10 |
| 100 | 100 |
| 1000 | 1000 |
With an index, the database uses a shortcut to find the row quickly, checking far fewer rows.
| Input Size (n) | Approx. Rows Checked |
|---|---|
| 10 | About 3 |
| 100 | About 7 |
| 1000 | About 10 |
Pattern observation: Without index, work grows directly with data size; with index, work grows slowly as data grows.
Time Complexity: O(log n)
This means finding data with an index takes a small, slowly growing amount of time even as the database gets much bigger.
[X] Wrong: "Adding an index makes every query instant no matter what."
[OK] Correct: Indexes speed up searches but still take some time that grows slowly with data size. Also, indexes add some cost when adding or changing data.
Understanding how indexes affect query speed shows you know how to make databases work well as they grow. This skill helps you design systems that stay fast and reliable.
"What if we added multiple indexes on different columns? How would the time complexity of queries change?"
Practice
Solution
Step 1: Understand what an index does
An index creates a quick lookup structure to find data faster.Step 2: Compare options with index purpose
Only speeding up searches matches the index function.Final Answer:
To speed up data searches and queries -> Option AQuick Check:
Index = Faster search [OK]
- Thinking indexes store more data
- Confusing indexes with backups
- Assuming indexes encrypt data
email in a Supabase table named users?Solution
Step 1: Recall correct SQL index syntax
The standard syntax is: CREATE INDEX index_name ON table_name (column_name); but index_name is optional.Step 2: Match syntax with options
CREATE INDEX ON users (email); uses correct syntax without specifying index name, which is allowed.Final Answer:
CREATE INDEX ON users (email); -> Option CQuick Check:
CREATE INDEX ON table (column) [OK]
- Placing column before table incorrectly
- Using wrong keyword order
- Omitting ON keyword
orders with an index on customer_id, what will be the effect of this query?SELECT * FROM orders WHERE customer_id = 123;
Solution
Step 1: Understand index usage in queries
Indexes help the database find matching rows quickly when filtering by indexed columns.Step 2: Analyze the query filter
The query filters by customer_id, which has an index, so the index will be used.Final Answer:
The query will run faster by using the index on customer_id -> Option DQuick Check:
Indexed column filter = faster query [OK]
- Thinking index causes query failure
- Assuming index filters out data
- Believing index is ignored automatically
Solution
Step 1: Recall how indexes affect data changes
Each index must be updated when data changes, so many indexes slow inserts and updates.Step 2: Match symptoms with cause
Slow insertions match the effect of too many indexes.Final Answer:
Too many indexes slow down data changes -> Option AQuick Check:
Many indexes = slower inserts [OK]
- Thinking indexes cause data loss
- Assuming indexes block inserts
- Believing indexes delete data automatically
products that is frequently searched by category and price. Which index strategy is best to improve query speed without slowing inserts too much?Solution
Step 1: Understand composite vs separate indexes
A composite index on (category, price) helps queries filtering both columns efficiently.Step 2: Consider insert performance
One composite index is faster to update than two separate indexes, balancing speed and insert cost.Step 3: Evaluate options
Create a composite index on (category, price) optimizes queries on both columns with minimal insert slowdown.Final Answer:
Create a composite index on (category, price) -> Option BQuick Check:
Composite index = faster multi-column queries [OK]
- Creating too many separate indexes
- Indexing only one column when both are searched
- Avoiding indexes and hurting query speed
Ready to prove it?
Pass the test and this pattern moves unknown
