DEV Community

Vincent Tommi
Vincent Tommi

Posted on

Denormalization: Making Your Database Faster (and Why It’s a Trade-Off) day 35 of system design basics

Whether you’re just starting out or you’ve been building apps for years, you’ve probably heard of databases and how they store all the data that powers your apps. But have you ever wondered how to make your database faster when fetching data? That’s where denormalization comes in! In this article, we’ll break down what denormalization is, why it matters, and when you should (or shouldn’t) use it—all in a beginner-friendly way with some cool flowcharts to guide you. We’ll use a hotel booking system as our example to keep things relatable. Let’s dive in!

What’s Normalization, Anyway? 🤔

Before we talk about denormalization, let’s quickly cover normalization. Imagine you’re running a hotel booking system like Booking.com. You’ve got guests, their reservations, and the rooms they book. To keep things organized, a relational database (like MySQL or PostgreSQL) uses normalization to split data into separate tables to avoid duplication.

Here’s what that might look like:

  • Guests table: Stores guest details like name and email.

  • Reservations table: Tracks bookings with details like reservation ID and room.

  • Rooms table: Holds room info like room number and type (e.g., single, suite).

This setup is super efficient for storing data because it avoids repeating the same info (like a guest’s name) across multiple tables. Here’s a flowchart to show how these tables connect:

This structure is great, but there’s a catch: when you want to fetch data, you need to join these tables together using something called a JOIN operation. For example, to get a guest’s reservation details, you might write a SQL query like this:

SELECT r.reservation_id, g.name, g.email, r.room, r.check_in_date
FROM reservations r
JOIN guests g ON r.guest_id = g.guest_id;
Enter fullscreen mode Exit fullscreen mode

Joins are awesome, but as your database grows (think thousands of guests and reservations), these operations can slow things down. That’s where denormalization swoops in to save the day! .

Enter Denormalization: Speed Things Up!

Denormalization is like the opposite of normalization. Instead of splitting data into separate tables, you combine related data into a single table, even if it means duplicating some info. Why? To make your queries faster by avoiding those pesky joins.

Let’s go back to our hotel booking example. Instead of having separate Guests and Reservations tables, you could create a single GuestReservations table that stores guest details and their reservation info together. Here’s what that looks like in a flowchart:

Now, fetching reservation details is as simple as:

SELECT reservation_id, guest_name AS name, guest_email AS email, room, check_in_date
FROM guest_reservations;
Enter fullscreen mode Exit fullscreen mode

No joins needed! This means your queries run faster, especially in read-heavy apps where users are constantly pulling data (like viewing their booking history).

Why Denormalization Rocks (and When It Doesn’t)

So, why would you use denormalization? Here are the big wins:

  • Faster Reads: No joins = quicker queries. Perfect for apps where speed is critical, like hotel booking dashboards or check-in systems.

  • Simpler Queries: Your SQL code is easier to write and understand, which is a win for both beginners and pros.

But, like everything in tech, there’s a trade-off. Denormalization isn’t perfect:

  • More Storage: Duplicating data (like storing a guest’s name in every reservation) takes up more space.

  • Complex Updates: If a guest changes their email, you have to update it in every row of the Guest Reservations table, which can get messy and slow.

Here’s a quick comparison to help you decide when to use denormalization:

Aspect Normalization Denormalization
Storage Less (no duplication) More (data is duplicated)
Read Speed Slower (requires joins) Faster (no joins needed)
Write/Update Easier (update one table) Harder (update multiple rows)
Best For Write-heavy apps (e.g., check-ins) Read-heavy apps (e.g., booking dashboards)

*When Should You Denormalize? *
Denormalization is like adding a turbo boost to your database, but it’s not always the right choice. Here are some scenarios where it shines:

  • Read-Heavy Apps: If your app is all about fetching data quickly (like showing guest bookings or room availability), denormalization can be a game-changer.

  • Reporting Systems: Dashboards or analytics tools that need to pull data fast for reports (like occupancy rates) love denormalized tables.

  • Caching: Sometimes, you denormalize data into a temporary table (or use tools like Redis) to speed up specific queries.

But if your app involves lots of data updates (like guests frequently updating their contact info), stick with normalization to keep things clean and manageable.

Wrapping Up

Denormalization is a powerful tool in your database toolkit. It sacrifices some storage and update simplicity to give you lightning-fast reads, which is perfect for apps where speed is king, like hotel booking systems. By understanding both normalization and denormalization, you can make smart choices about how to structure your database based on your app’s needs.

If you’re just starting out, play around with a small database in MySQL or SQLite to see how normalization and denormalization work in practice. For the pros, consider denormalization for performance bottlenecks in read-heavy systems, but always weigh the trade-offs.

Top comments (0)