Over-fetching and under-fetching problems in GraphQL - Time & Space Complexity
When using GraphQL, it's important to see how the amount of data requested affects performance.
We want to know how fetching too much or too little data changes the work the server does.
Analyze the time complexity of this GraphQL query fetching user data and their posts.
query GetUsersWithPosts($limit: Int) {
users(limit: $limit) {
id
name
posts {
id
title
}
}
}
This query gets a list of users and for each user, it fetches all their posts.
Look for repeated actions in the query.
- Primary operation: Fetching each user and then fetching all posts for that user.
- How many times: For each user (n times), fetch their posts (m times per user).
As the number of users and posts grows, the work grows too.
| Input Size (users n, posts per user m) | Approx. Operations |
|---|---|
| 10 users, 5 posts each | About 10 * 5 = 50 fetches |
| 100 users, 5 posts each | About 100 * 5 = 500 fetches |
| 100 users, 50 posts each | About 100 * 50 = 5000 fetches |
Pattern observation: The total work grows by multiplying users and posts per user.
Time Complexity: O(n * m)
This means the work grows with both the number of users and the number of posts each user has.
[X] Wrong: "Fetching more fields or nested data doesn't affect performance much."
[OK] Correct: Every extra nested list means more data to fetch and process, which can multiply the work and slow things down.
Understanding how data fetching scales helps you design better queries and APIs that are fast and efficient.
What if we added a filter to fetch only posts from the last month? How would that change the time complexity?
Practice
over-fetching mean in GraphQL?Solution
Step 1: Understand the term over-fetching
Over-fetching means the client receives more data than it requires, which can slow down the app.Step 2: Compare with other options
Under-fetching is the opposite (less data), exact fetching matches needs, and multiple sources is unrelated.Final Answer:
Getting more data than the client actually needs -> Option AQuick Check:
Over-fetching = More data than needed [OK]
- Confusing over-fetching with under-fetching
- Thinking over-fetching means exact data
- Mixing up data sources with data amount
Solution
Step 1: Identify requested fields
{ user { name email } }requests onlynameandemail, exactly what is needed.Step 2: Check other options for extra fields
{ user { id name email } }includesid(extra), B and D includepostsandcomments(extra data).Final Answer:
{ user { name email } } -> Option CQuick Check:
Request only needed fields ={ user { name email } }[OK]
- Including extra fields causing over-fetching
- Missing required fields causing under-fetching
- Confusing field names or nesting
{ user { name posts { title } } }What problem might this query cause if the client only needs the user's name?
Solution
Step 1: Analyze the query fields
The query requestsnameandposts { title }, which includes posts data.Step 2: Compare with client needs
The client only needsname, so fetching posts is extra data causing over-fetching.Final Answer:
Over-fetching, fetching unnecessary posts data -> Option DQuick Check:
Extra fields cause over-fetching = Over-fetching, fetching unnecessary posts data [OK]
- Thinking missing posts is under-fetching
- Assuming no problem if query runs
- Confusing syntax errors with data issues
{ user { name } }The client needs the user's name and email.
Solution
Step 1: Check requested fields
The query requests onlyname, missingemail.Step 2: Compare with client needs
The client needs bothnameandemail, so missingemailcauses under-fetching.Final Answer:
The query is missing theemailfield -> Option AQuick Check:
Missing needed fields = under-fetching [OK]
- Assuming syntax error when none exists
- Thinking too many fields cause under-fetching
- Ignoring missing fields as a problem
{ users { id name posts { title comments { text } } } }To avoid over-fetching when the client only needs user names and post titles, how should you modify the query?
Solution
Step 1: Identify needed fields
The client needs onlynameandpost titles, noidor comments.Step 2: Modify query to include only needed fields
{ users { name posts { title } } }requestsnameandposts { title }, exactly matching needs without extra data.Final Answer:
{ users { name posts { title } } } -> Option BQuick Check:
Remove unneeded fields to avoid over-fetching [OK]
- Leaving comments in query causing over-fetching
- Removing needed fields like user name
- Removing posts entirely causing under-fetching
Ready to prove it?
Pass the test and this pattern moves unknown
