In this lesson, you will learn how to evaluate the performance trade-offs between strictly normalized relational schemas and denormalized flat data models when working with embedded databases like SQLite.
The Relational Approach (Normalized)
A normalized design organizes data into separate tables to eliminate redundancy. For example, in an e-commerce app, you might have a Users table and an Orders table linked by user_id.
- Pros: Data integrity is easier to maintain; updates to a user's name only happen in one place.
- Cons: Retrieving a complete "Order Summary" requires a
JOINoperation. While manageable, frequent joins can introduce latency, especially when dealing with complex queries in resource-constrained embedded environments.
The Flat Approach (Denormalized)
A flat model collapses related data into a single table. You might store the user’s name and email directly inside the Orders table alongside the order details.
-- Flat model example: Everything in one place
CREATE TABLE Orders_Flat (
order_id INTEGER PRIMARY KEY,
customer_name TEXT,
customer_email TEXT,
item_name TEXT,
price REAL
);
- Pros: Lightning-fast read performance. Since there are no joins, the database engine performs a simple sequential scan or index lookup on a single file. This is often ideal for mobile apps where UI responsiveness is the priority.
- Cons: Massive data redundancy. If a customer changes their email, you must update every single order row associated with them. This increases the risk of "update anomalies" and significantly bloats your database file size, which can degrade performance over time.
The Trade-off
Choosing between these models depends on your read-to-write ratio. If your application primarily displays information (high-read) and rarely updates core entities, a flat model can provide a smoother user experience by minimizing CPU overhead. If your data is highly transactional or requires strict consistency (high-write), the relational model is safer and more efficient for managing state. In embedded databases, favor the flat model only when the performance gains from avoiding joins outweigh the complexity of managing data duplication.
Key Takeaway: Choose normalized relational models for data integrity and storage efficiency, or flat denormalized models for high-speed read performance at the cost of redundancy and maintenance complexity.