Table relationships in Supabase - Time & Space Complexity
When working with table relationships in Supabase, it's important to understand how the number of related records affects the time it takes to fetch data.
We want to know: how does the time to get related data grow as the amount of related data grows?
Analyze the time complexity of fetching a user and their related orders.
const { data, error } = await supabase
.from('users')
.select('id, name, orders(*)')
.eq('id', userId)
This code fetches one user by ID and includes all their related orders in the result.
Here are the key operations that repeat:
- Primary operation: Fetching each related order record from the orders table.
- How many times: Once for each order linked to the user.
As the number of orders for a user grows, the time to fetch all orders grows roughly the same way.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 orders | Fetching 1 user + 10 orders |
| 100 orders | Fetching 1 user + 100 orders |
| 1000 orders | Fetching 1 user + 1000 orders |
Pattern observation: The time grows linearly with the number of related orders.
Time Complexity: O(n)
This means the time to fetch data grows directly in proportion to the number of related records.
[X] Wrong: "Fetching related data is always a single fast operation regardless of size."
[OK] Correct: Each related record adds to the total data fetched, so more related records mean more time.
Understanding how related data fetching scales helps you design efficient queries and explain your reasoning clearly in real-world situations.
"What if we only fetched the count of related orders instead of all order details? How would the time complexity change?"
Practice
Solution
Step 1: Understand table relationships
Table relationships link data in different tables using keys to keep data connected.Step 2: Identify the purpose
This linking helps to find related data easily, like joining customer info with orders.Final Answer:
To connect data across tables using keys -> Option BQuick Check:
Table relationships = Connect data [OK]
- Thinking relationships store files
- Believing relationships increase database size
- Confusing relationships with UI themes
Solution
Step 1: Identify the keyword for foreign keys
In SQL, the REFERENCES keyword defines a foreign key that links one table's column to another's primary key.Step 2: Match with Supabase usage
Supabase uses standard SQL, so REFERENCES is used to create safe links between tables.Final Answer:
REFERENCES -> Option AQuick Check:
Foreign key = REFERENCES [OK]
- Using JOIN to create relationships (JOIN reads data, doesn't create keys)
- Confusing SELECT or INSERT with key creation
CREATE TABLE authors (id SERIAL PRIMARY KEY, name TEXT);CREATE TABLE books (id SERIAL PRIMARY KEY, title TEXT, author_id INT REFERENCES authors(id));What will this query return?
SELECT books.title, authors.name FROM books JOIN authors ON books.author_id = authors.id;Solution
Step 1: Understand the table relationship
The books table has a foreign key author_id linking to authors.id, connecting each book to its author.Step 2: Analyze the JOIN query
The query joins books and authors on matching author_id and id, selecting book titles and author names together.Final Answer:
A list of book titles with their author's names -> Option DQuick Check:
JOIN on foreign key returns related data [OK]
- Expecting authors without books from this JOIN
- Thinking query causes error due to foreign key
- Assuming books without author names appear
CREATE TABLE orders (id SERIAL PRIMARY KEY, customer_id INT, FOREIGN KEY customer_id REFERENCES customers(id));Why does this cause an error in Supabase?
Solution
Step 1: Check FOREIGN KEY syntax
The FOREIGN KEY clause requires parentheses around the column name, like FOREIGN KEY (customer_id).Step 2: Confirm other parts
customer_id as INT is valid, SERIAL primary key is valid, and REFERENCES is supported in Supabase.Final Answer:
FOREIGN KEY syntax is incorrect; it needs parentheses around the column -> Option AQuick Check:
FOREIGN KEY syntax = parentheses needed [OK]
- Omitting parentheses around foreign key column
- Changing data type unnecessarily
- Thinking REFERENCES is unsupported
students and courses tables in Supabase. Which setup is best?Solution
Step 1: Understand many-to-many relationships
Many-to-many means each student can take many courses and each course can have many students.Step 2: Choose the correct design
Neither students nor courses alone can hold all links. A join table with student_id and course_id foreign keys connects them properly.Final Answer:
Create a join table with student_id and course_id as foreign keys -> Option CQuick Check:
Many-to-many = join table with two foreign keys [OK]
- Trying to store many-to-many in one table column
- Merging unrelated tables
- Using one foreign key only
Ready to prove it?
Pass the test and this pattern moves unknown
