DEV Community

Cover image for My experience for Normalizing Database: A Funny Story from the Trenches
dana
dana

Posted on

My experience for Normalizing Database: A Funny Story from the Trenches

As a young database engineer, I once found myself in a bit of a pickle at my company. Our application was running painfully slow, and no one could figure out why. That is, until I took a deep dive into the database design.

It all started when my boss called me into her office, her brow furrowed with concern. "Hey, kiddo," she said (she always called me that, even though I was a grown adult). "The sales team is up in arms about how slow the app is running. Think you can work your magic and fix this?"

Challenge accepted! I rolled up my sleeves and got to work, determined to get to the bottom of this database dilemma.

First, I took a look at the schema. Oh boy, was it a mess. Tables were all over the place, with data redundancy galore. It was like a tangled web of information, just waiting to trip someone up.

"Alright," I thought to myself, "time to put my database normalization skills to the test."

I started by identifying the various entities - customers, orders, products, you name it. Then, I broke each one down into its most basic attributes, carefully avoiding any unnecessary duplication.

Let me give you an example. Originally, the "customers" table had fields for the customer's name, address, phone number, and email. But it also had fields for the salesperson's name and contact info. Yikes! That's a classic case of data redundancy.

So, I created a separate "salespeople" table to store all that info, and linked the customers back to their assigned salesperson using a foreign key. Boom! Normalized.

Next, I tackled the orders table. It was a mess of product details, shipping info, and payment data. I split that sucker up into three separate tables - one for the order header, one for the line items, and one for the shipping and payment details.

"This is starting to shape up nicely," I thought, patting myself on the back.

After a few more rounds of table splitting and relationship building, the database was looking lean, mean, and ready to rumble. I couldn't wait to see the results.

Sure enough, when the sales team fired up the app the next day, it was like night and day. The pages were zipping along, and the users were practically dancing with joy (okay, maybe not dancing, but they were definitely a lot less grumpy).

My boss gave me a big ol' high five and said, "Nicely done, kiddo. I knew you had it in you!"

From that day on, I made database normalization my middle name. Okay, not really, but I did become the go-to guy for all things data-related. And you know what? I kind of enjoyed the challenge. It's like solving a puzzle, but with way more spreadsheets involved.

So, if you ever find yourself in a similar situation - a slow-running app, a tangled web of a database - don't panic. Just grab a cup of coffee, roll up your sleeves, and get to work on that normalization magic. Trust me, your users (and your boss) will thank you.

Step by step for normalize the database design

Here's the step-by-step process I used to normalize the database and solve the performance issues, i used online database design tool for visualize this design. Here is my table.

Original Customers Table:

CustomerID CustomerName CustomerAddress CustomerPhone CustomerEmail SalespersonName SalespersonPhone

Step 1: Separate the Salespeople information into a new table:
Salespeople Table:

SalespersonID SalespersonName SalespersonPhone

Customers Table (updated):

CustomerID CustomerName CustomerAddress CustomerPhone CustomerEmail SalespersonID

Step 2: Separate the Orders information into a new table:
Orders Table:

OrderID CustomerID OrderDate ShippingAddress ShippingPhone PaymentMethod PaymentDetails

Step 3: Separate the Order Line Items into a new table:
OrderLineItems Table:

OrderLineItemID OrderID ProductID Quantity UnitPrice

Step 4: Create a separate table for Products:
Products Table:

ProductID ProductName ProductDescription UnitPrice

After these normalization steps, the database structure looks much cleaner and more efficient. Here's how the relationships between the tables would look:

Customers --< Orders >-- OrderLineItems
            |
            v
        Salespeople
            |
            v
         Products

Enter fullscreen mode Exit fullscreen mode

By separating the data into these normalized tables, we've eliminated data redundancy, improved data integrity, and made the database more scalable. The application's performance should now be much faster, as the database can efficiently retrieve and process the data it needs.

This step-by-step approach to database normalization is a tried and true method for solving performance issues and maintaining a healthy, well-structured database. It may take some time and effort upfront, but the long-term benefits are well worth it.

Conclusion for normalize database design

Here's the conclusion to the story about normalizing the database and solving the performance issues:

After spending a good chunk of my week buried in spreadsheets and SQL queries, the database normalization project was finally complete. I leaned back in my chair, took a deep breath, and admired my handiwork.

The once-tangled web of data had been transformed into a sleek, efficient database structure. Gone were the days of redundant information and sluggish performance. This database was ready to take on the world (or at least, our company's growing sales and customer data).

I couldn't wait to show my boss the results. As I walked into her office, she looked up from her computer, a hopeful gleam in her eye.

"Well, kiddo," she said, "the sales team is breathing down my neck. Any luck with that database issue?"

I grinned. "You bet. Let me walk you through it."

I pulled up the new database schema on the screen, explaining each step of the normalization process. Her eyes grew wider with each table and relationship I described.

"Wow, I had no idea database design could be so... intricate," she said, shaking her head in amazement.

When I finished, she leaned back in her chair, a satisfied smile spreading across her face.

"Nice work, kid. I knew you were the right person for the job." She paused, then added, "You know, I think this calls for a celebratory lunch. My treat. What do you say?"

I didn't need to be asked twice. As we headed out the door, I felt a sense of pride and accomplishment wash over me. Sure, it had been a lot of hard work, but the payoff was worth it. Not only had I solved a critical problem for the business, but I'd also solidified my reputation as the go-to database guru.

From that day on, whenever performance issues or data management challenges arose, my boss would come knocking. And you know what? I didn't mind one bit. It was the perfect opportunity to flex my normalization muscles and keep that database running like a well-oiled machine.

So, if you ever find yourself in a similar situation – a slow app, a tangled database, and a boss breathing down your neck – just remember: normalization is your friend. Embrace the challenge, dive into the data, and watch as your application transforms into a lean, mean, performance-boosting machine.

Oh, and don't forget to ask your boss out for lunch. You've earned it!

Top comments (0)