我有一个包含产品销售历史的数据库。例如下表关于重复信息的数据库设计问题
CREATE TABLE SalesHistoryTable (
OrderID, // Order Number Unique to all orders
ProductID, // Product ID can be used as a Key to look up product info in another table
Price, // Price of the product per unit at the time of the order
Quantity, // quantity of the product for the order
Total, // total cost of the order for the product. (Price * Quantity)
Date, // Date of the order
StoreID, // The store that created the Order
PRIMARY KEY(OrderID));
该表最终将有数百万的交易。由此可以为不同地理区域的产品(基于StoreID)创建配置文件。创建这些配置文件作为数据库查询可能非常耗时。例如。
SELECT ProductID, StoreID,
SUM(Total) AS Total,
SUM(Quantity) QTY,
SUM(Total)/SUM(Quantity) AS AvgPrice
FROM SalesHistoryTable
GROUP BY ProductID, StoreID;
上述查询可用于获取基于任何特定商店的产品的信息。然后,您可以确定哪家商店卖得最多,赚的最多,平均卖得最多/最少。这将作为普通的查询运行非常昂贵。假设存储大小不成问题,为了让这些类型的查询运行得更快,什么是设计思路?例如,我可以创建另一个带有重复信息的表格。 商店ID(金钥),产品ID,TotalCost,QTY,AvgPrice 并提供一个触发器,以便在收到新订单时,该商店的条目将在新表中更新。更新的成本几乎没有。
在给出上述情况时应该考虑什么?
您自己的答案是针对这种查询。在数据库中缓存结果将比您能做的任何事情提供更大的加速。这种方法的另一个好处是,如果事情由于某种原因而失去同步,那么可以把所有东西都抛出去,并用一个查询重新创建表。 – roufamatic 2010-04-07 18:14:56