我们有以下格式的表格:
Order(OrderID,CustomerID,OrderDate,CreatedByUserID,LastModifiedByUserID)
OrderItem(OrderID,ProductID,ProviderID,ItemStatus,CompletedByUserID)
Companies(CompanyID, CompanyName, CompanyParentID, CompanyRegionID)
生成订单报告所需的完整数据表需要近 12 个连接和大约 250 个字段。下面是一个简短的例子
SELECT o.OrderID, o.CustomerID ... FROM Orders AS o
INNER JOIN OrderItems AS items ON o.OrderID = items.OrderID
INNER JOIN Products AS p ON items.ProductID = p.ProductID
INNER JOIN Companies AS cust ON o.CustomerID = cust.ComapnyID
LEFT OUTER JOIN Companies AS prov ON items.ProviderID = prov.ComapnyID
INNER JOIN Users AS u1 ON items.CreatedByUserID = u1.UserID
INNER JOIN Users AS u2 ON items.LastModifiedByUserID = u2.UserID
LEFT OUTER JOIN Users AS ui1 ON items.CompletedByUserID = ui1.UserID
LEFT OUTER JOIN Users AS ui2 ON items.VerifiedByUserID = ui2.UserID
LEFT OUTER JOIN Companies AS parent ON cust.CompanyParentID = parent.ComapnyID
LEFT OUTER JOIN Companies AS region ON cust.CompanyRegionID = region.ComapnyID
我的问题是:既然这是一个报告应用程序,我们应该运行一次这个 SQL(例如每小时)并将数据复制到运行报告的临时表中,还是应该在用户请求时始终运行所有这些连接看报告?
笔记:
- 报告最长可能会过期一个小时。因为它们通常每周/每月运行一次。
- 数据是多租户的。即根据谁在运行报告(客户、母公司、区域办事处、产品供应商等)对其进行过滤