我有一个SQL数据库服务器和2个数据库,它们具有相同的结构和数据。我在2个数据库中运行相同的sql查询,其中一个需要更长时间,而另一个在不到50%的时间内完成。他们都有不同的执行计划。
视图的查询如下:
SELECT DISTINCT i.SmtIssuer, i.SecID, ra.AssetNameCurrency AS AssetIdCurrency, i.IssuerCurrency, seg.ProxyCurrency, shifts.ScenarioDate, ten.TenorID, ten.Tenor,
shifts.Shift, shifts.BusinessDate, shifts.ScenarioNum
FROM dbo.tblRrmIssuer AS i INNER JOIN
dbo.tblRrmSegment AS seg ON i.Identifier = seg.Identifier AND i.SegmentID = seg.SegmentID INNER JOIN
dbo.tblRrmAsset AS ra ON seg.AssetID = ra.AssetID INNER JOIN
dbo.tblRrmHistSimShift AS shifts ON seg.Identifier = shifts.Identifier AND i.SegmentID = shifts.SegmentID INNER JOIN
dbo.tblRrmTenor AS ten ON shifts.TenorID = ten.TenorID INNER JOIN
dbo.tblAsset AS a ON i.SmtIssuer = a.SmtIssuer INNER JOIN
dbo.tblRrmSource AS sc ON seg.SourceID = sc.SourceID
WHERE (a.AssetTypeID = 0) AND (sc.SourceName = 'CsVaR') AND (shifts.SourceID =
(SELECT SourceID
FROM dbo.tblRrmSource
WHERE (SourceName = 'CsVaR')))
我已经尝试过的东西是 - 重建和放大重新组织表上的索引(tblRRMHistSimShifts - 此表有超过200万条记录),检查服务器上的锁或其他后台进程或错误,服务器的最大并行度为0。
还有什么可以建议您解决此问题吗?
答案 0 :(得分:1)
事实上,您在同一台服务器上拥有两个数据库且具有相同的数据集(如您所说)并不能确保相同的执行计划。
以下是查询计划可能不同的部分原因:
专注于查看查询运行缓慢的原因或查看实际执行计划,而不是比较。检查慢速查询的实际执行计划将给出一个为什么运行速度较慢的提示。
另外,我不会添加NO LOCK语句来解决问题。根据我的经验,大多数慢查询可以通过代码或索引进行调整,而不是根据您的交易添加可能会修改或旧结果集的NO LOCK提示。
答案 1 :(得分:0)
最好的方法是重建&重新组织您的请求
SELECT DISTINCT i.SmtIssuer, i.SecID, ra.AssetNameCurrency AS AssetIdCurrency, i.IssuerCurrency, seg.ProxyCurrency, shifts.ScenarioDate, ten.TenorID, ten.Tenor,
shifts.Shift, shifts.BusinessDate, shifts.ScenarioNum
FROM dbo.tblRrmIssuer AS i INNER JOIN dbo.tblRrmSegment AS seg ON i.Identifier = seg.Identifier AND i.SegmentID = seg.SegmentID
INNER JOIN dbo.tblRrmSource AS sc ON seg.SourceID = sc.SourceID
INNER JOIN dbo.tblRrmAsset AS ra ON seg.AssetID = ra.AssetID
INNER JOIN dbo.tblRrmHistSimShift AS shifts ON seg.Identifier = shifts.Identifier AND i.SegmentID = shifts.SegmentID AND shifts.SourceID = sc.SourceID
INNER JOIN dbo.tblRrmTenor AS ten ON shifts.TenorID = ten.TenorID
INNER JOIN dbo.tblAsset AS a ON i.SmtIssuer = a.SmtIssuer
WHERE (a.AssetTypeID = 0) AND (sc.SourceName = 'CsVaR')