我经常看到的订单或复制其他表的一个或多个字段,以便采取客户的产品订单的快照,当它被置于发票LINE_ITEM表。
在我的架构,但是,我可以生成一个订单,而不复制数据的视图。 因此要查找的订单/产品/价格数据是有点贵,但我节省了复制/插入时间,空间和冗余。 据我所知,复制/插入是一次性交易,而查询将需要很多次 - 但,我只用成千上万的记录10S成交在一个给定的表,我不希望表现要一个问题。
所以,因为)我的架构支持精确的查找没有一个快照,和b)我没有对查询优化的强烈需求,我觉得很有道理运行计算,而不是采取瞬间-射击。 或者是有什么我失踪,我应该始终以一个快照在这样的情况下?
下面是查找计算将是什么样子的例子:
# display order items for a particular order on a particular date
# get order, products and base prices from order_id
order_products = SELECT * FROM order_has_product ohp
INNER JOIN price ON (price.product_id = ohp.product_id)
INNER JOIN order ON (order.id = ohp.order_id)
WHERE order_id = ?
# calculate price of each product at order.datetime_opened
for op in order_products:
tax = SELECT SUM(tax.rate) FROM product_has_tax pht
INNER JOIN tax ON (tax.id = pht.tax_id)
WHERE pht.product_id = op.product_id
AND tax.date_start <= op.datetime_opened
AND tax.date_end >= op.datetime_opened
discount_product = SELECT SUM(discount.rate) FROM product_has_discount phd
INNER JOIN discount ON (discount.id = phd.discount_id)
WHERE phd.product_id = op.product_id
AND discount.date_start <= op.datetime_opened
AND discount.date_end >= op.datetime_opened
discount_customer = SELECT SUM(discount.rate) FROM customer_has_discount chd
INNER JOIN discount ON (discount.id = chd.discount_id)
WHERE chd.customer_id = op.customer_id
AND discount.date_start <= op.datetime_opened
AND discount.date_end >= op.datetime_opened
AND (chd.date_used_limited IS NULL OR chd.date_used_limited = op.datetime_opened)
discount = discount_product + discount_customer
price = op.price * (1-discount) * (1+tax)