Skip to main content
ANNOUNCEMENTMeet MariaDB Enterprise Platform 2027. Read more.

Menu

WEBINAR

Built for the Agentic Era: Introducing MariaDB Enterprise Platform 2027

Watch now

Product

DuckDB for Magento Analytics: How a MariaDB Storage Engine Made a One-Million-Order Report 63× Faster

By Roman Nozdrin

17 min read
Share
services:
  db:
    image: ghcr.io/mariadb/duckdb:11.4
SELECT VERSION();
SHOW ENGINES;
DUCKDB  YES  DuckDB storage engine
magento.sales_order       → InnoDB
magento.sales_order_item  → InnoDB
CREATE DATABASE analytics
CHARACTER SET utf8mb4
COLLATE utf8mb4_general_ci;
CREATE TABLE analytics.sales_order
LIKE magento.sales_order;

CREATE TABLE analytics.sales_order_item
LIKE magento.sales_order_item;
ALTER TABLE analytics.sales_order 
ENGINE=DUCKDB;

ALTER TABLE analytics.sales_order_item
ENGINE=DUCKDB;
INSERT INTO analytics.sales_order
SELECT * FROM magento.sales_order;
INSERT INTO analytics.sales_order_item
SELECT * FROM magento.sales_order_item;
WITH item_totals AS (
SELECT
order_id,
SUM(qty_ordered - IFNULL(qty_canceled, 0)) AS total_qty_ordered,
SUM(qty_invoiced) AS total_qty_invoiced
FROM sales_order_item
WHERE parent_item_id IS NULL
OR parent_item_id = 0
GROUP BY order_id
)
FROM sales_order AS o
INNER JOIN item_totals AS oi
ON oi.order_id = o.entity_id
START TRANSACTION;

INSERT INTO magento.sales_order (...);
SET @order_id = LAST_INSERT_ID();

INSERT INTO magento.sales_order_item (...);

INSERT INTO analytics.sales_order (...)
SELECT ...
FROM magento.sales_order
WHERE entity_id = @order_id;

INSERT INTO analytics.sales_order_item (...)
SELECT ...
FROM magento.sales_order_item
WHERE order_id = @order_id;

COMMIT;
WITH item_totals AS (
SELECT
order_id,
SUM(qty_ordered - IFNULL(qty_canceled, 0)) AS total_qty_ordered,
SUM(qty_invoiced) AS total_qty_invoiced
FROM analytics.sales_order_item
WHERE parent_item_id IS NULL OR parent_item_id = 0
GROUP BY order_id
)
SELECT
CAST(o.created_at AS DATE) AS period,
o.store_id,
o.status AS order_status,
COUNT(o.entity_id) AS orders_count,
SUM(oi.total_qty_ordered) AS total_qty_ordered,
SUM(oi.total_qty_invoiced) AS total_qty_invoiced,
SUM(
(IFNULL(o.base_grand_total, 0) - IFNULL(o.base_total_canceled, 0))
* IFNULL(o.base_to_global_rate, 0)
) AS total_income_amount,
SUM(
(
IFNULL(o.base_total_invoiced, 0)
- IFNULL(o.base_tax_invoiced, 0)
- IFNULL(o.base_shipping_invoiced, 0)
- (
IFNULL(o.base_total_refunded, 0)
- IFNULL(o.base_tax_refunded, 0)
- IFNULL(o.base_shipping_refunded, 0)
)
) * IFNULL(o.base_to_global_rate, 0)
) AS total_revenue_amount,
SUM(
(
IFNULL(o.base_total_paid, 0)
- IFNULL(o.base_total_refunded, 0)
- IFNULL(o.base_tax_invoiced, 0)
- IFNULL(o.base_shipping_invoiced, 0)
- IFNULL(o.base_total_invoiced_cost, 0)
) * IFNULL(o.base_to_global_rate, 0)
) AS total_profit_amount,
SUM(IFNULL(o.base_total_invoiced, 0) * IFNULL(o.base_to_global_rate, 0)) AS total_invoiced_amount,
SUM(IFNULL(o.base_total_canceled, 0) * IFNULL(o.base_to_global_rate, 0)) AS total_canceled_amount,
SUM(IFNULL(o.base_total_paid, 0) * IFNULL(o.base_to_global_rate, 0)) AS total_paid_amount,
SUM(IFNULL(o.base_total_refunded, 0) * IFNULL(o.base_to_global_rate, 0)) AS total_refunded_amount,
SUM(
(IFNULL(o.base_tax_amount, 0) - IFNULL(o.base_tax_canceled, 0))
* IFNULL(o.base_to_global_rate, 0)
) AS total_tax_amount,
SUM(
(IFNULL(o.base_tax_invoiced, 0) - IFNULL(o.base_tax_refunded, 0))
* IFNULL(o.base_to_global_rate, 0)
) AS total_tax_amount_actual,
SUM(
(IFNULL(o.base_shipping_amount, 0) - IFNULL(o.base_shipping_canceled, 0))
* IFNULL(o.base_to_global_rate, 0)
) AS total_shipping_amount,
SUM(
(IFNULL(o.base_shipping_invoiced, 0) - IFNULL(o.base_shipping_refunded, 0))
* IFNULL(o.base_to_global_rate, 0)
) AS total_shipping_amount_actual,
SUM(
(ABS(IFNULL(o.base_discount_amount, 0)) - IFNULL(o.base_discount_canceled, 0))
* IFNULL(o.base_to_global_rate, 0)
) AS total_discount_amount,
SUM(
(IFNULL(o.base_discount_invoiced, 0) - IFNULL(o.base_discount_refunded, 0))
* IFNULL(o.base_to_global_rate, 0)
) AS total_discount_amount_actual
FROM analytics.sales_order AS o
INNER JOIN item_totals AS oi ON oi.order_id = o.entity_id
GROUP BY CAST(o.created_at AS DATE), o.store_id, o.status
ORDER BY period, o.store_id, o.status;

No, and this experiment didn't try. Magento needs InnoDB's transactions, indexes, foreign keys, and crash recovery for orders, invoices, and refunds. DuckDB was used only for read-only analytical copies.

No. The MariaDB DuckDB storage engine runs inside MariaDB. You create tables with ENGINE=DUCKDB and query them over the same connection as your InnoDB tables. In a Docker stack, swapping the image to ghcr.io/mariadb/duckdb:11.4 was the only change.

In this test, the Magento Orders Report over 1,000,000 orders and 2,000,000 items ran 63.2× faster: 0.292 seconds versus 18.464 seconds. Results will differ for other reports, data distributions, and hardware.

Move every large table the report joins, not just one. Moving only sales_order gave 2.23×. Moving sales_order and sales_order_item together gave 63.2×

No. In this setup, DuckDB tables are snapshots. You need a full refresh, incremental sync, ETL, change-data capture, or a dual-write approach to keep them current.

Loading 1,000,000 orders took 14.333 seconds, and 2,000,000 order items took 20.159 seconds, for a total of 34.492 seconds in this environment.

In this experiment, they were removed: AUTO_INCREMENT, primary keys, unique keys, secondary indexes, and foreign keys. Column definitions stayed identical so INSERT ... SELECT * worked directly.

Reports that don't need data from the current second: daily sales reports, management dashboards, long-period revenue analysis, BI queries, and ad hoc analytics.