0
0
MongoDBquery~5 mins

Normalization vs denormalization default in MongoDB - Performance Comparison

Choose your learning style9 modes available
Time Complexity: Normalization vs denormalization default
O(n)
Understanding Time Complexity

When working with databases, how data is organized affects how fast queries run.

We want to see how time to get data changes when using normalized or denormalized data in MongoDB.

Scenario Under Consideration

Analyze the time complexity of fetching user orders in two ways: normalized and denormalized.


// Normalized: separate collections
const user = db.users.findOne({ _id: userId });
const orders = db.orders.find({ userId: user._id }).toArray();

// Denormalized: embedded orders
const userWithOrders = db.users.findOne({ _id: userId });
const orders = userWithOrders.orders;
    

This code shows fetching orders separately (normalized) versus embedded inside user (denormalized).

Identify Repeating Operations
  • Normalized primary operation: Querying orders collection for matching userId.
  • Normalized how many times: Once per user, but scanning orders that belong to user.
  • Denormalized primary operation: Single query to users collection, then access embedded orders array.
  • Denormalized how many times: One query, no extra scans.
How Execution Grows With Input

As the number of orders grows, how does query time change?

Input Size (orders per user)Normalized Approx. OperationsDenormalized Approx. Operations
10Scan 10 orders in orders collectionAccess 10 embedded orders
100Scan 100 orders in orders collectionAccess 100 embedded orders
1000Scan 1000 orders in orders collectionAccess 1000 embedded orders

Pattern observation: Both grow roughly linearly with number of orders, but normalized requires separate query scanning orders collection.

Final Time Complexity

Time Complexity: O(n)

This means the time to fetch orders grows linearly with how many orders a user has, whether normalized or denormalized.

Common Mistake

[X] Wrong: "Denormalized data always makes queries faster regardless of data size."

[OK] Correct: Large embedded arrays can slow down queries and updates, so time can still grow with data size.

Interview Connect

Understanding how data layout affects query time helps you design better databases and answer real questions about performance.

Self-Check

"What if we added an index on userId in the orders collection? How would that change the time complexity for normalized queries?"