我有名为:
mktActualsales (SaleID, EmployeeID, PeriodID,PositionID)
mktActualSalesItems(SaleItemID, saleID, Item, Quantity)
mktSalesTargets(TargetID, EmployeeID, PeriodID,PositionID)
mktSalesTargetItems(TargetITemID, TargetID,ItemID, Quantity )
sysPeriods(PeriodID, StartDate, EndDate)
上表sale和saleDetails之间的关系很明显,通过SaleID关联,Target和TargetDetail通过TargetID关联。我要显示的是员工在所有时期的销售额和目标。有些时期缺少销售,而在其他时期可能缺少目标。
我的计划(到目前为止)是在一个子查询中采用 sale 和 saleDetail 的内部联接,在另一个子查询中采用 target 和 targetDetail ,然后在两个子查询上进行完全外部联接,以获得我需要的数据。但结果非常缓慢。我可以做些什么来加快性能。我在谷歌上搜索了完全外部连接和联合之间的性能比较,但没有任何运气。目前我不知道我想要的结果是否可以使用 Union/Union All 来实现。
编辑
这是我到目前为止的查询。也编辑了表格
SELECT sale.ActualSaleID,
COALESCE (sale.EmployeeID, saleTarget.EmployeeID) AS EmployeeID,
COALESCE (sale.PositionID, saleTarget.PositionID) AS PositionID,
COALESCE (sale.PeriodID, saleTarget.PeriodID) AS PeriodID,
COALESCE (sale.SKUID, saleTarget.SKUID) AS SKUID,
COALESCE (sale.SalesQuantity, 0) AS SalesQuantity,
saleTarget.SalesTargetID,
COALESCE (saleTarget.TargetQuantity, 0) AS TargetQuantity,
saleTarget.SalesTargetItemID,
sale.ActualSaleItemID,
p.StartDate,
p.EndDate
FROM (
SELECT s.ActualSaleID,
s.EmployeeID,
s.PeriodID,
s.PositionID,
si.ActualSaleItemID,
si.SKUID,
si.SalesQuantity
FROM dbo.mktActualSaleItems AS si
INNER JOIN dbo.mktActualSales AS s
ON si.ActualSaleID = s.ActualSaleID
) AS sale
FULL OUTER JOIN
(
SELECT t.EmployeeID,
t.PeriodID,
t.PositionID,
t.SalesTargetID,
ti.SKUID,
ti.TargetQuantity,
ti.SalesTargetItemID
FROM dbo.mktSalesTargetItems AS ti
INNER JOIN dbo.mktSalesTargets AS t
ON t.SalesTargetID = ti.SalesTargetID
) AS saleTarget
ON sale.PeriodID = saleTarget.PeriodID
AND sale.PositionID = saleTarget.PositionID
AND sale.SKUID = saleTarget.SKUID
INNER JOIN dbo.sysPeriods AS p
ON p.PeriodID = COALESCE (sale.PeriodID, saleTarget.PeriodID)