我有以下查询:
SELECT fpa.scenario_id,
fpa.facility_id,
cge.CostGroupId result_total_id,
mp_surrogate_id,
CAST(SUM(fpa.raw_amount * cge.CostSign) AS DECIMAL(25, 13))
result_total_amount
INTO ADM_FactProfitTotalAmount_1
FROM #tempAmount fpa
JOIN ResultTest cge ON cge.CostId = fpa.process_id
WHERE fpa.scenario_id = 1
GROUP BY fpa.scenario_id, fpa.facility_id, cge.CostGroupId, fpa.mp_surrogate_id
- 在
#tempAmount
我有 2.2 亿行。 - 在
ResultTest
我有 150 行。
我有一个索引#tempAmount
:
CREATE NONCLUSTERED INDEX #tempAmount_process_id
ON #tempAmount(scenario_id, facility_id, mp_surrogate_id, process_id )
执行大约需要 1 小时。是否可以对其进行优化?
编辑:
我在 ResultTest 列 CostId 上创建了索引,更改了一些其他索引和查询
CREATE CLUSTERED INDEX #tempFactAmount_index
ON #tempAmount (process_id ,facility_id, mp_surrogate_id )
SELECT ISNULL(CAST(1 as BIGINT), 0) scenario_id,
fpa.facility_id,
cge.CostGroupId result_total_id,
fpa.mp_surrogate_id,
CAST(SUM(fpa.raw_amount * cge.CostSign) AS DECIMAL(25, 13)) result_total_amount
INTO ADM_FactProfitTotalAmount_1
FROM ResultTest cge
JOIN #tempAmount fpa ON cge.CostId = fpa.process_id
GROUP BY fpa.facility_id, fpa.mp_surrogate_id, cge.CostGroupId
执行计划:
41% 插入 ADM_FactProfitTotalAmount_1
51% 哈希匹配聚合
2% 哈希匹配内连接