我首先将其分为两个操作:生成代理键和填充维表。第一步将DISTINCT
只有3列,而第二步将变为JOIN
。索引这两个操作中使用的列可能会给您带来一些改进。
您可以将DISTINCT
与结合使用,NOT EXISTS
以避免处理已经被映射的行,如下所示:
insert into dbo.KeyMappingTable (shipto, salpha, ssalpha)
select distinct shipto, salpha, ssalpha
from dbo.source
where not exists (
select *
from dbo.KeyMappingTable
where shipto = dbo.source.shipto and salpha = dbo.source.salpha and ssalpha = dbo.source.ssalpha
)
然后便有了映射,因此您可以执行以下操作:
insert into dbo.DimShipTo (shipto_id, shipto /*, etc. */)
select
m.shipto_id,
s.shipto -- etc.
from
dbo.KeyMappingTable m
join dbo.source s
on m.shipto = s.shipto and m.salpha = s.salpha and m.ssalpha = s.ssalpha
where
not exists (
select *
from dbo.DimShipTo
where shipto_id = m.shipto_id
)
您还应该查看MERGE
,如果您使用的是Type 1维度,并且只想在地址或其他属性更改时更新地址(这通常是一个有用的命令),这将很方便。但是它只能在sql Server 2008中使用。您没有提到要使用的sql Server版本。