Cosmos DB overview and use cases in Azure - Time & Space Complexity
We want to understand how the time to perform operations in Cosmos DB changes as we work with more data or requests.
Specifically, how does the number of operations or API calls grow when using Cosmos DB for common tasks?
Analyze the time complexity of inserting multiple documents into a Cosmos DB container.
// Insert multiple items into Cosmos DB container
for (int i = 0; i < itemCount; i++) {
await container.CreateItemAsync(items[i]);
}
This code inserts each item one by one into the Cosmos DB container.
Look at what repeats as we insert items:
- Primary operation: The CreateItemAsync API call to add one document.
- How many times: Once for each item in the input list.
As the number of items grows, the number of insert calls grows the same way.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 | 10 calls |
| 100 | 100 calls |
| 1000 | 1000 calls |
Pattern observation: The number of calls grows directly with the number of items.
Time Complexity: O(n)
This means the time to insert items grows linearly with how many items you add.
[X] Wrong: "Inserting multiple items at once will take the same time as inserting one item."
[OK] Correct: Each item requires a separate API call, so more items mean more calls and more time.
Understanding how Cosmos DB operations scale helps you design efficient data solutions and shows you can think about performance in cloud services.
"What if we used batch operations to insert multiple items at once? How would the time complexity change?"