Update mutation pattern in GraphQL - Time & Space Complexity
When we update data using a GraphQL mutation, it's important to know how the time it takes changes as the data grows.
We want to understand how the update operation's cost changes when there are more records or fields involved.
Analyze the time complexity of the following update mutation.
mutation UpdateUser($id: ID!, $input: UserUpdateInput!) {
updateUser(id: $id, input: $input) {
id
name
email
}
}
This mutation updates a single user's data by their ID and returns some fields.
Look for repeated work inside the update process.
- Primary operation: Searching for the user by ID in the database.
- How many times: Once per update request, no loops inside the mutation itself.
The time to update depends mostly on how fast the database finds the user by ID.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 users | About 10 steps to find the user if no index |
| 100 users | About 100 steps to find the user if no index |
| 1000 users | About 1000 steps to find the user if no index |
Pattern observation: Without an index, the search grows linearly with the number of users.
Time Complexity: O(n)
This means the update takes longer as the number of users grows, because it searches through them one by one.
[X] Wrong: "Updating a single user always takes the same time no matter how many users exist."
[OK] Correct: If the database does not use an index, it must check many users to find the right one, so time grows with the number of users.
Understanding how update operations scale helps you explain real-world database behavior clearly and confidently.
"What if the database uses an index on the user ID? How would the time complexity change?"