我有一个大约有800万条记录的审计表。我最近添加了两个新列,我需要从现有列更新一些规则/条件。基本上,最初,只要在表中更新FK,它就会将新旧FK ID存储到审计表中。例如
Table A
ID Name
1 First A
2 Second A
3 Third A
Table B
ID AID Name
1 1 First B
2 1 Second B
3 2 Third B
Audit
ID TableName FieldName OldValue NewValue
现在如果我更新表B的第一条记录 从1 1 First B到1 3 First B然后审计表将更改存储为
Audit
ID TableName FieldName OldValue NewValue
1 Table B AID 1 3
现在我更新了审核表以存储FK的实际文本值,即上面的更改将存储为
Audit
ID TableName FieldName OldValue NewValue OldText NewText
1 Table B AID 1 3 First A Third A
问题是我已经有大约800万条记录需要新列。我写了下面的查询来做那个
declare @sql nvarchar(max);
declare @start int = 1
while @start <= 8000000
begin
select top 10000 @sql = COALESCE(@sql+'Update Audit set ','Update Audit set') +
isnull(' OldText = ('+ dbo.GetFKText(i.TableName, i.FieldName)+case when len(isnull(i.OldValue,'')) < 1 then null else i.OldValue end +'),',' OldText = OldValue, ') +
isnull(' NewText = ('+ dbo.GetFKText(i.TableName, i.FieldName)+case when len(isnull(i.NewValue,'')) < 1 then null else i.NewValue end +')',' NewText = NewValue ') +
' where AuditID = '+cast(i.AuditID as nvarchar(200))+' and lower(ltrim(rtrim(TableName))) <> ''audit'';'
from Audit i where i.AuditID >= @start
exec sp_executesql @sql
set @start = @start+10000;
end
获取文本功能(基本上我得到的列名为=(TableName)+&#39;名称&#39;或(TablName)+(SomeText)+&#39;名称&#39;这只是我的惯例在所有表中都遵循了)
declare @res nvarchar(max)='';
declare @fn nvarchar(200);
declare @ttn nvarchar(200);
declare @tcn nvarchar(200);
SELECT top 1
@ttn = kcu.table_name
,@tcn = kcu.column_name
FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE ccu
INNER JOIN INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS rc
ON ccu.CONSTRAINT_NAME = rc.CONSTRAINT_NAME
INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE kcu
ON kcu.CONSTRAINT_NAME = rc.UNIQUE_CONSTRAINT_NAME
Where ccu.TABLE_NAME = @TableName and ccu.COLUMN_NAME = @FieldName
if isnull(@ttn,'') != '' and ISNULL(@tcn,'') != ''
begin
select @fn= COLUMN_NAME
from (SELECT top 1 COLUMN_NAME ,
case when COLUMN_NAME like (@ttn+'Name') then 0
when COLUMN_NAME like (@ttn+'%Name') then 1
when COLUMN_NAME like (@ttn+'Code') then 2
when COLUMN_NAME like (@ttn+'%Code') then 3 else 4 end as CPriority
FROM JVO.INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = @ttn and (COLUMN_NAME like '%Name' or COLUMN_NAME like '%Code'
)
order by CPriority) as aa;
RETURN 'select '+@fn+' from '+@ttn+' where '+@tcn+' = ';
end
return null;
它的工作但非常慢,它在13个小时内更新了大约100万条记录。任何人都可以帮助改进此查询或建议更新它的替代方法。
由于