我正在将测试数据库升级到2017开发人员版。现在我的表值函数有问题。对于4000的插入记录,选择性能的插入非常慢。
declare @dateStart date = '4-Jul-2019'
,@dateEnd date = '4-Jul-2019'
declare @ntnOnDate table
(
[NTM ID] char(8)
,[NTM Type] char(3) -- Posible Value: NTM, UPR, SOL
,primary key clustered([NTM ID], [NTM Type])
)
declare @finishedNTM table
(
[NTM ID] char(8)
,[NTM Type] char(3)
,Dept char(3)
,[Model Number] char(4)
,[Shoes Type] int
,[Shoes Version] int
,[Size] varchar(5)
,[Pairs] int not null
--, primary key clustered ([NTM ID], [NTM Type],Dept, [Model Number], [Shoes Type], [Shoes Version], [Size])
)
insert into @ntnOnDate
--- select statement (Only 16 record) --
insert into @finishedNTM (
[NTM ID]
, [NTM Type]
, Dept
, [Model Number]
, [Shoes Type]
, [Shoes Version]
, [Size]
, pairs
)
select
a.[TSW ID] as [NTM ID]
,'NTM' as [NTM Type]
,a.[From Department] as [Dept]
,b.[Model Number]
,b.[Shoes Type]
,b.[Shoes Version]
,b.Size
,sum(b.Pairs) as Pairs
from [TSW Master] a
inner join [TSW Details] b
on a.[TSW ID] = b.[TSW ID]
and a.Issue = b.Issue
and a.[From Department] = b.[From Department]
and a.[To Department] = b.[To Department]
--inner join @ntnOnDate c
-- on a.[TSW ID] = c.[NTM ID]
where a.Executed = 1
and a.Accepted = 1
and a.[From Department] <> '210'
--and c.[NTM Type] = 'NTM'
and a.[TSW ID] in (select x.[NTM ID] from @ntnOnDate x where x.[NTM Type] = 'NTM')
group by
a.[TSW ID]
,a.[From Department]
,b.[Model Number]
,b.[Shoes Type]
,b.[Shoes Version]
,b.Size
奇怪的是,当我在select语句中使用“ select top 4462”(select语句的记录计数为4462)时,插入将在2秒内完成。
有人可以帮助我吗?提前致谢。 阿安
答案 0 :(得分:0)
我发现自己可以调整刀片的性能。我将插入语句更改为:
insert @finishedNTM
select
a.[NTM ID]
,'NTM' as [NTM Type]
,b.[From Department] as [Dept]
,b.[Model Number]
,b.[Shoes Type]
,b.[Shoes Version]
,b.Size
,b.Pairs
from @ntnOnDate a
inner join (
select
a.[TSW ID]
,a.[From Department]
,b.[Model Number]
,b.[Shoes Type]
,b.[Shoes Version]
,b.Size
,sum(b.Pairs) as Pairs
from [TSW Master] a
inner join [TSW Details] b
on a.[TSW ID] = b.[TSW ID]
and a.Issue = b.Issue
and a.[From Department] = b.[From Department]
and a.[To Department] = b.[To Department]
where a.Executed = 1
and a.Accepted = 1
and a.[From Department] <> '210'
group by
a.[TSW ID]
,a.[From Department]
,b.[Model Number]
,b.[Shoes Type]
,b.[Shoes Version]
,b.Size
) b
on a.[NTM ID] = b.[TSW ID]
where a.[NTM Type] = 'NTM'
SUM聚合在语句块内部移动,而不是我将其与另一个临时表连接起来。
但是我仍然不知道为什么会发生这个问题。该查询在SQL Server 2014上很好。
问候 阿安