Database Design & Tuning
ER Modelling, Normalization & Speed
A database that works for one store will fail for 10,000 stores if it isn't designed correctly. How do we structure data so that it remains fast as it grows to billions of rows? How do we eliminate redundant data that leads to errors? This chapter covers the Art of Database Design.
We will explore ER Modelling to visualize our business, Normalization to keep our data clean, and Indexing to ensure our global retail powerhouse moves with lightning speed.
ER Modelling: The Business Map
Entity-Relationship (ER) Modelling is the process of defining exactly what "Things" (Entities) your business tracks and how they are "Connected" (Relationships).
- Entity: A Customer, a Product, a Store.
- Attribute: A Customer's name, a Product's price.
- Relationship: A Customer places an Order.
Getting the ER model right is 90% of the work in building a successful database system.
Normalization: The 3 Golden Rules
Normalization is a formal technique to organize data to reduce redundancy:
- 1st Normal Form (1NF): No repeating groups. Each cell must have one value.
- 2nd Normal Form (2NF): Everything in the row must depend on the Primary Key.
- 3rd Normal Form (3NF): No "hidden" dependencies (e.g., don't store the City if you already have the Zip Code table).
Normalization ensures that when a customer changes their address, you only have to update it in one place, not fifty.
Performance Tuning: Why is it slow?
As your data grows, queries get slower. Performance tuning is the science of making them fast again.
- Indexing: Like the index at the back of a book, it tells the database exactly where to look without reading every page.
- Execution Plans: Using
EXPLAINto see the exact steps the database takes. - Query Optimization: Avoiding
SELECT *and usingLIMITto fetch only what you need.
EXPLAIN SELECT * FROM transactions WHERE amount > 1000;Beyond the Single Database: The Age of Big Data
When a database grows to a "massive, massive" size—what we call Big Data—traditional single-server architectures reach their physical limits. In the modern **Age of AI**, where models require petabytes of data for training and real-time inference, we move into the realm of distributed systems.
To resolve these extreme scale challenges, engineers use a specialized toolkit:
- In-Memory Databases: Storing data in RAM (like Redis or SAP HANA) for microsecond response times.
- Distributed Systems & Sharding: Splitting a massive table across dozens of different servers so no single machine is overwhelmed.
- Massively Parallel Processing (MPP): Using many processors to execute a single query in parallel (e.g., Snowflake, BigQuery).
- Clustering & Load Balancing: Ensuring that millions of user requests are distributed evenly across a global network of database nodes.
This is a complete, separate field of study, but it is the ultimate destination for any SQL expert working with the massive datasets that power today's Artificial Intelligence.
Practice Questions
Question 1
What is the main purpose of Database Normalization?
Question 2
Which index type is the default and most common in databases like PostgreSQL for general comparisons?
Question 3
In an ER Diagram, what does a diamond shape usually represent?
Question 4
Which command shows the 'cost' and 'time' the database expects to spend on a query?
Question 5
Which Normal Form is violated if a table stores both 'Store Name' and 'Store Manager Name' alongside every single sale?