Json 加入&;在SQL Server中处理大型数据集的速度太慢

Json 加入&;在SQL Server中处理大型数据集的速度太慢,json,sql-server,join,large-data,Json,Sql Server,Join,Large Data,我们发现下面的查询大约需要和小时才能加载1000多条记录。表“q”和表文件中的记录超过300万条,f大约有200万条。有没有关于如何加快速度的建议 Select q.Id, vStorageId,vRootId, q.vPath, p.[FileName], p.Original, p.[User], IsNull(p.[Size], 0) as FileSize, p.[Extension], p.Encode, p.[IsPicture], p.IsUp

我们发现下面的查询大约需要和小时才能加载1000多条记录。表“q”和表文件中的记录超过300万条,f大约有200万条。有没有关于如何加快速度的建议

Select 
    q.Id, vStorageId,vRootId, q.vPath, p.[FileName], 
    p.Original, p.[User],
    IsNull(p.[Size], 0) as FileSize, p.[Extension], p.Encode, 
    p.[IsPicture], p.IsUploaded, 
    (Case when p.IsDirectory=1 then GetUtcDate() else p.Time end) as [Modified],
    IsNull(p.[Created], getUTCDate()) as [Created], 
    IsNull(p.IsDirectory,0) as IsDirectory
from 
    Ques q
cross apply 
    openJson(Request,'$.created') 
        WITH([Message] nvarchar(255) '$.message',  
             [FileName] nvarchar (1024) '$.name',  
             Original nvarchar (1024) '$.name',  
             [User] nvarchar (1024) '$.user', 
             [Size] bigint '$.size',  [Extension] nvarchar (255) '$.ext',  
             Encode bit '$.encode',  [IsPicture] bit '$.picture',  
             IsUploaded bit '$.uploaded', 
             [Time] DateTime2 '$.time',  [Created] DateTime2 '$.created',  
             IsDirectory int '$.dir') p
left join 
    dbo.Files f on f.[vPath] = q.[vPath] 
                and f.[FileName] = p.[FileName] 
                and q.vStorageID = f.StorageID
where 
    topic = 'Scan' 
    and State = 4 
    and f.Id is null;

上载执行计划并将链接添加到您的问题。由于连接使用JSON中的投影值,您可以尝试使用持久化或索引计算列将其“升级”到Ques上的列。谢谢David,我们已经做了部分工作(请参阅筛选器vPath和vStorageId等),但将进一步调查。。。还有其他提示吗?[topic]和[State]列从何而来?(为什么没有在他们身上使用别名?)。我们最终通过将JSON取出到它自己的表中,对它进行索引,并更改左连接来解决这个问题。结果是扫描现在需要几个小时,而不是几个星期,增量扫描也运行得非常快。