I have a schema something like this:
-record(sale, {
id,
price,
tax,
sku,
datetime
})
-record(refund, {
id,
sale_id,
datetime
})
There are about 600,000 records in the sale
table and about 20,000 in the refund
table.
I need to fold over the data in the sales table, grouping each sku
with the amount received (price
) and the amount refunded (by checking #refund.sale_id
).
Is there an efficient way to do this with Mnesia, that avoid the N+1 query problem where for each sale
in the fold operation, I need to make a second call the mnesia to see if a refund exists? Some way of doing a naive join?
Is your sale
table ordered_set
? If yes, you can do merge join as usual. First get all refund
and sort them by sale_id
. Then just fold over sale
and accumulate into dict or ets by sku
key.