如何解决活动监视器上的 INSERT 语句昂贵的查询
我有一个大约 300k 的对象列表。我正在通过 Web API 批量发送它。由于这个对象是多级的,我决定以 1000 个为一批发送项目,这些项目转换为 JSON 字符串。目前,我看到每秒插入大约 50 条记录,我估计需要大约 1 个多小时才能完成。
在我的 SQL Server 中,我有一个接受 JSON 字符串并使用 OPENJSON 的存储过程。数据被插入到一个 TempTable 中,然后我重复使用 while 如图所示(不确定是否有更好的方法)
While (Select Count(*) From #temporganisations Where process_ind = 0) > 0
Begin
select top 1
@orgid=torg.orgid
from
#temporganisations torg
where torg.process_ind=0
exec AddOrganisation... retvalid out
exec AddLocation...retvalid
exec AddContact...retvalid
etc
.
.
.
delete from #temporganisations
where orgid= @orgid
end
如图所示,在此存储过程中调用了多个存储过程。每个存储过程在执行 INSERT 语句之前都会进行重复检查。我正在努力加快这个过程。完成这个过程真的需要很长时间。所以我发现了活动监视器,并注意到“最近昂贵的查询”如下所示显示了被调用的 INSERT 语句
我只是想知道我是否可以做些什么来提高这些 INSERT 的性能,这是其中一个 INSERT 的执行计划
我在某处读到我可以直接执行 INSERT 但存储过程将是巨大的,因为我需要添加重复检查。我还阅读了有关使用变量表而不是临时表的文章,但我还阅读了一篇文章,其中指出变量表适用于小型数据集。提前致谢。
以下是 XML 结构。如您所见,我需要将每个子节点链接到作为组织的父节点。
<Organisations>
<Organisation orgRecordClass="R43">
<Name>ORGANIZATION X</Name>
<Date>
<Type value="Operational" />
<Start value="1991-04-01" />
<End value="1994-03-31" />
</Date>
<OrgId root="5161f" assigningAuthorityName="H51" extension="R34" />
<Status value="Inactive" />
<LastChangeDate value="2013-05-08" />
<GeoLoc>
<Location>
<AddrLn1>458 HOMER ROAD</AddrLn1>
<Town>LONDON</Town>
<PostCode>E1 8PL</PostCode>
<Country>ENGLAND</Country>
<UPRN>21521</UPRN>
</Location>
</GeoLoc>
<Contacts>
<Contact type="tel" value=" 233344" />
<Contact type="fax" value=" 233355" />
</Contacts>
<Roles>
<Role id="R12" uniqueRoleId="1" primaryRole="true">
<Date>
<Type value="Operational" />
<Start value="1991-04-01" />
<End value="1994-03-31" />
</Date>
</Role>
</Roles>
<Rels>
<Rel id="RE6" uniqueRelId="58005">
<Date>
<Type value="Operational" />
<Start value="1991-04-01" />
<End value="1994-03-31" />
</Date>
<Status value="Inactive" />
</Rel>
</Rels>
<Succs>
<Succ uniqueSuccId="12">
<Date>
<Type value="Legal" />
<Start value="1993-04-01" />
</Date>
<Type>Successor</Type>
<Target>
<OrgId root="5161f" assigningAuthorityName="H51" extension="R561" />
<PrimaryRoleId id="R12" uniqueRoleId="37607" />
</Target>
</Succ>
<Succ uniqueSuccId="12">
<Date>
<Type value="Legal" />
<Start value="1993-04-01" />
</Date>
<Type>Successor</Type>
<Target>
<OrgId root="5161f" assigningAuthorityName="H51" extension="R561" />
<PrimaryRoleId id="R12" uniqueRoleId="37607" />
</Target>
</Succ>
</Succs>
</Organisation>
解决方法
首先要遵循在 SQL Server 中运行逐行进程的基本最佳实践。这意味着使用真正的游标而不是那种奇怪的游标式循环,并将整个循环包装在一个事务中。
begin transaction
declare c cursor local for
select * from #temporganisations
open c
fetch next from c into @orgid,@name,...
while @@fetch_status = 0
begin
exec AddLocation...
exec AddContact...
etc
.
.
.
fetch next from c into @orgid,...
end
commit transaction
如果没有事务,事务日志必须在每次插入后物理刷新。
第二件事是将其转变为面向批处理的过程。您可以重写每个存储过程以接受多个输入行(TVP、JSON 或 XML),或者只对每个存储过程进行编码以从 #temporganisations 中读取。存储过程可以使用会话中声明的任何临时表,因此您可以通过在调用过程之前插入临时表来将批量数据传递到存储过程中。
EG
create or alter procedure AddLocation
as
begin
insert into Location (Name,Address)
select distinct Name,Address
from #temporganisations
end
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。