我正在处理性能不佳的查询,该查询无法处理具有数百万条记录的大型数据集。参数作为JSON传递到存储过程中,并在该过程中提取。
在公用表表达式中使用OPENJSON分配JSON数据时,查询需要 74秒:
create procedure [getResults] ( @Input nvarchar(max) = '' ) as
declare @MaxResults int = json_value(@Input, '$.MaxResults');
with SearchInfo (FirstName, MiddleName, LastName) as (
select FirstName, MiddleName, LastName
from openjson(@Input)
with ( FirstName nvarchar(50) '$.FirstName', MiddleName nvarchar(50) '$.MiddleName', LastName nvarchar(50) '$.LastName' )
)
select top (@RowCount) * from SearchInfo cross apply getMatches(FirstName, MiddleName, LastName)
如果将OPENJSON移到公用表表达式之外,则查询仅需 18秒:
create procedure [getResults] ( @Input nvarchar(max) = '' ) as
declare @MaxResults int = json_value(@Input, '$.MaxResults');
declare @FirstName nvarchar(50), @MiddleName nvarchar(50), @LastName nvarchar(50);
select @FirstName = FirstName, @MiddleName = MiddleName, @LastName = LastName
from openjson(@Input)
with ( FirstName = '$.FirstName', MiddleName = '$.MiddleName', LastName = '$.LastName' );
with SearchInfo (FirstName, MiddleName, LastName) as (
select FirstName, MiddleName, LastName
from (values (@FirstName, @MiddleName, @LastName))
)
select top (@RowCount) * from SearchInfo cross apply getMatches(@FirstName, @MiddleName, @LastName)
如果我完全放弃OPENJSON,并且仅使用JSON_VALUE,则查询仅需 4秒:
create procedure [getResults] ( @Input nvarchar(max) = '' ) as
declare @MaxResults int = json_value(@Input, '$.MaxResults');
declare @FirstName nvarchar(50) = json_value(@Input, '$.FirstName');
declare @MiddleName nvarchar(1) = json_value(@Input, '$.MiddleName');
declare @LastName nvarchar(50) = json_value(@Input, '$.LastName');
with SearchInfo (FirstName, MiddleName, LastName) as (
select FirstName, MiddleName, LastName
from (values (@FirstName, @MiddleName , @LastName))
)
select top (@RowCount) * from SearchInfo cross apply getMatches(@FirstName, @MiddleName, @LastName)
最后,如果我完全删除JSON调用,则查询仅需 1秒:
create procedure [getResults] ( @Input nvarchar(max) = '' ) as
declare @MaxResults int = json_value(@Input, '$.MaxResults');
declare @FirstName nvarchar(50) = 'Chris';
declare @MiddleName nvarchar(1) = 'H';
declare @LastName nvarchar(50) = 'Schiffhauer';
select @FirstName = FirstName, @MiddleName = MiddleName, @LastName = LastName
from openjson(@Input)
with ( FirstName = '$.FirstName', MiddleName = '$.MiddleName', LastName = '$.LastName' );
with SearchInfo (FirstName, MiddleName, LastName) as (
select FirstName, MiddleName, LastName
from (values (@FirstName, @MiddleName, @LastName))
)
select top (@RowCount) * from SearchInfo cross apply getMatches(@FirstName, @MiddleName, @LastName)
在SQL Server存储过程中提取JSON数据以免引入不必要的延迟的最快方法是什么?