I am leaning toward Option 1.
I am adding tracking the Sku so I have the maximum run rate for OEE.
Option 1
One table
Columns:
ndx, t_stamp, location, machine, event code, quantity, sku
Triggers
When a new state event or reject event happens, the sku is logged.
When a sku changes, the state is logged
Option 2
3 event tables
Stop events columns:
ndx, t_stamp, location, machine, stop code
Reject events columns:
ndx, t_stamp, location, machine, reject code, quantity
Sku events columns:
ndx, t_stamp, location, machine, sku
Option 1 query
SELECT
sku,
SUM(CASE WHEN event_code = 'Stop' THEN 1 ELSE 0 END) AS total_stops,
SUM(CASE WHEN event_code = 'Reject' THEN quantity_or_sku ELSE 0 END) AS total_rejects
FROM
your_table
WHERE
t_stamp BETWEEN '2024-03-20 08:00:00' AND '2024-03-20 09:00:00'
GROUP BY
sku;
Option 2 query
SELECT
sk.sku,
SUM(CASE WHEN s.event_code = 'Stop' THEN 1 ELSE 0 END) AS total_stops,
SUM(CASE WHEN r.reject_code IS NOT NULL THEN 1 ELSE 0 END) AS total_rejects
FROM
sku_events sk
LEFT JOIN
stop_events s ON sk.ndx = s.ndx AND s.t_stamp BETWEEN '2024-03-20 08:00:00' AND '2024-03-20 09:00:00'
LEFT JOIN
reject_events r ON sk.ndx = r.ndx AND r.t_stamp BETWEEN '2024-03-20 08:00:00' AND '2024-03-20 09:00:00'
GROUP BY
sk.sku;
I need help evaluating benefits of Option 2.
I only know that it may have faster indexed inserts, not sure.
I learned to go with a single table before with the events tables.
I had used a single table with states and rejects in one table before.
Though I did see some comments about separating those tables.
I still don't understand why to separate them, or is it just the insert speed?
Organizing SQL tables and named queries efficiently - #11 by zacharyw.larson]