How to Denormalize Data in MariaDB

Galaxy Glossary

How do I denormalize data in MariaDB without causing stale records?

Denormalizing data in MariaDB combines related tables into one to accelerate read-heavy queries by trading storage for speed.

Sign up for the latest in SQL knowledge from the Galaxy Team!
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Description

Table of Contents

What does “denormalize data” mean in MariaDB?

Denormalization copies or aggregates columns from several normalized tables into a single table. The new table eliminates runtime JOINs, so dashboards and APIs respond faster.

When should you denormalize?

Apply denormalization for read-heavy workloads, slow JOINs on large tables, or repeated aggregations. Avoid it for write-heavy tables because every update must be reflected in two places.

How do you denormalize step-by-step?

Step 1 – Create a target table

Design a schema that stores all required columns and derived metrics. Include primary keys or unique constraints to prevent duplicate rows.

Step 2 – Populate the table

Use CREATE TABLE ... AS SELECT or INSERT INTO ... SELECT to copy data from source tables with JOINs or aggregations.

Step 3 – Keep data in sync

Attach AFTER INSERT/UPDATE/DELETE triggers or schedule EVENTs (cron-like jobs) to refresh the denormalized table so analytics stay accurate.

Practical example: Orders summary

The query below builds a fast lookup table for customer-level order totals, eliminating two JOINs for every dashboard load.

CREATE TABLE customer_order_totals AS
SELECT c.id AS customer_id,
c.name AS customer_name,
c.email,
COUNT(o.id) AS order_count,
SUM(o.total_amount) AS lifetime_value,
MAX(o.order_date) AS last_order_date
FROM Customers c
LEFT JOIN Orders o ON o.customer_id = c.id
GROUP BY c.id, c.name, c.email;

Best practices for safe denormalization

Start with a clear performance goal. Track query timings before and after. Document every trigger/event used for syncing. Expose the table as read-only to application code.

Common mistakes and fixes

Stale data: Missing triggers or jobs leave the table outdated. Fix by adding AFTER UPDATE triggers or nightly EVENTs.
Over-denormalization: Copying entire rows inflates storage and slows inserts. Include only columns the application truly needs.

Why How to Denormalize Data in MariaDB is important

How to Denormalize Data in MariaDB Example Usage


-- Build a product sales roll-up table for quick listing pages
CREATE TABLE product_sales_cache AS
SELECT  p.id            AS product_id,
        p.name,
        p.price,
        SUM(oi.quantity) AS units_sold,
        SUM(oi.quantity * p.price) AS gross_revenue,
        p.stock
FROM    Products p
LEFT JOIN OrderItems oi ON oi.product_id = p.id
GROUP BY p.id, p.name, p.price, p.stock;

How to Denormalize Data in MariaDB Syntax


-- One-off build
CREATE TABLE new_table AS
SELECT ...
FROM   source_table1
JOIN   source_table2 USING (key)
WHERE  conditions;

-- Ongoing refresh (event scheduler)
CREATE EVENT refresh_new_table
ON SCHEDULE EVERY 15 MINUTE
DO
  REPLACE INTO new_table
  SELECT ...
  FROM   source_table1 s1
  JOIN   source_table2 s2 ON s2.key = s1.key;

-- Trigger-based sync example
CREATE TRIGGER orders_ai AFTER INSERT ON Orders
FOR EACH ROW
  REPLACE INTO customer_order_totals (customer_id, order_count, lifetime_value)
  SELECT  o.customer_id,
          COUNT(*),
          SUM(total_amount)
  FROM    Orders o
  WHERE   o.customer_id = NEW.customer_id;

Common Mistakes

Frequently Asked Questions (FAQs)

Does denormalization violate database design rules?

Yes, it breaks normalization but intentionally. The trade-off is faster reads at the cost of redundancy. Document the decision and keep source tables canonical.

Should I index a denormalized table?

Absolutely. Add composite indexes that match your most frequent WHERE clauses to unlock the full performance benefit.

How can I refresh large denormalized tables?

Use CREATE TABLE ... SELECT into a temp table, then RENAME TABLE swap in one atomic step. This avoids locking the live table.

Want to learn about other SQL terms?

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.