我正在运行一个包含多个连接的查询,并返回用于QA环境的SQL Server实例上的4K记录。当我通过SSMS在我的本地开发机器和用于DEV环境的SQL Server实例上运行查询时,查询立即返回(0秒),但是当在QA环境SSMS查询窗口上运行相同的查询时,它需要大约50秒返回。该查询是使用Entity Framework 6从.NET应用程序生成的.NET应用程序中使用的代码如下:
var includes = new List<Expression<Func<TAM, object>>>();
includes.Add(t => t.AssetClassModels);
includes.Add(t => t.AssetClassModels.Select(acm => acm.Allocations));
includes.Add(t => t.AssetClassModels.Select(acm => acm.Allocations.Select(a => a.TAMAssetClass)));
includes.Add(t => t.AssetClassModels.Select(acm => acm.Allocations.Select(a => a.TAMAssetClass.BroadAssetClassType)));
includes.Add(t => t.AssetClassModels.Select(acm => acm.Allocations.Select(a => a.TAMAssetClass.Correlations)));
var tam = innerUnitOfWork.Repository<TAM>().Find(x => x.Id == tamResult.TAM.Id, includes);
上面代码生成的SQL查询如下:
exec sp_executesql N'SELECT
[Project4].[Number] AS [Number],
[Project4].[Id] AS [Id],
[Project4].[ProductTypeId] AS [ProductTypeId],
[Project4].[GlidePathTypeId] AS [GlidePathTypeId],
[Project4].[CashEquivalentPreferenceId] AS [CashEquivalentPreferenceId],
[Project4].[TAMClientId] AS [TAMClientId],
[Project4].[C3] AS [C1],
[Project4].[Number1] AS [Number1],
[Project4].[Id1] AS [Id1],
[Project4].[TAMId] AS [TAMId],
[Project4].[C2] AS [C2],
[Project4].[Id3] AS [Id2],
[Project4].[DisplayOrder] AS [DisplayOrder],
[Project4].[Id2] AS [Id3],
[Project4].[TAMAssetClassId] AS [TAMAssetClassId],
[Project4].[Percentage] AS [Percentage],
[Project4].[ModelId] AS [ModelId],
[Project4].[Name] AS [Name],
[Project4].[IsCore] AS [IsCore],
[Project4].[ExpectedReturn] AS [ExpectedReturn],
[Project4].[StandardDeviation] AS [StandardDeviation],
[Project4].[BroadAssetClassTypeId] AS [BroadAssetClassTypeId],
[Project4].[Id4] AS [Id4],
[Project4].[Name1] AS [Name1],
[Project4].[DisplayOrder1] AS [DisplayOrder1],
[Project4].[C1] AS [C3],
[Project4].[Id5] AS [Id5],
[Project4].[TAMAssetClassXAxisId] AS [TAMAssetClassXAxisId],
[Project4].[TAMAssetClassYAxisId] AS [TAMAssetClassYAxisId],
[Project4].[Correlation] AS [Correlation]
FROM ( SELECT
[Project3].[Id] AS [Id],
[Project3].[Number] AS [Number],
[Project3].[ProductTypeId] AS [ProductTypeId],
[Project3].[GlidePathTypeId] AS [GlidePathTypeId],
[Project3].[CashEquivalentPreferenceId] AS [CashEquivalentPreferenceId],
[Project3].[TAMClientId] AS [TAMClientId],
[Project3].[Id1] AS [Id1],
[Project3].[Number1] AS [Number1],
[Project3].[TAMId] AS [TAMId],
[Project3].[Id2] AS [Id2],
[Project3].[TAMAssetClassId] AS [TAMAssetClassId],
[Project3].[Percentage] AS [Percentage],
[Project3].[ModelId] AS [ModelId],
[Project3].[DisplayOrder] AS [DisplayOrder],
[Project3].[Id3] AS [Id3],
[Project3].[Name] AS [Name],
[Project3].[IsCore] AS [IsCore],
[Project3].[ExpectedReturn] AS [ExpectedReturn],
[Project3].[StandardDeviation] AS [StandardDeviation],
[Project3].[BroadAssetClassTypeId] AS [BroadAssetClassTypeId],
[Project3].[Id4] AS [Id4],
[Project3].[Name1] AS [Name1],
[Project3].[DisplayOrder1] AS [DisplayOrder1],
[Project3].[Id5] AS [Id5],
[Project3].[TAMAssetClassXAxisId] AS [TAMAssetClassXAxisId],
[Project3].[TAMAssetClassYAxisId] AS [TAMAssetClassYAxisId],
[Project3].[Correlation] AS [Correlation],
CASE WHEN ([Project3].[Id1] IS NULL) THEN CAST(NULL AS int) WHEN ([Project3].[Id2] IS NULL) THEN CAST(NULL AS int) WHEN ([Project3].[Id5] IS NULL) THEN CAST(NULL AS int) ELSE 1 END AS [C1],
CASE WHEN ([Project3].[Id1] IS NULL) THEN CAST(NULL AS int) WHEN ([Project3].[Id2] IS NULL) THEN CAST(NULL AS int) ELSE 1 END AS [C2],
CASE WHEN ([Project3].[Id1] IS NULL) THEN CAST(NULL AS int) ELSE 1 END AS [C3]
FROM ( SELECT
[Project2].[Id] AS [Id],
[Project2].[Number] AS [Number],
[Project2].[ProductTypeId] AS [ProductTypeId],
[Project2].[GlidePathTypeId] AS [GlidePathTypeId],
[Project2].[CashEquivalentPreferenceId] AS [CashEquivalentPreferenceId],
[Project2].[TAMClientId] AS [TAMClientId],
[Project2].[Id1] AS [Id1],
[Project2].[Number1] AS [Number1],
[Project2].[TAMId] AS [TAMId],
[Project2].[Id2] AS [Id2],
[Project2].[TAMAssetClassId] AS [TAMAssetClassId],
[Project2].[Percentage] AS [Percentage],
[Project2].[ModelId] AS [ModelId],
[Project2].[DisplayOrder] AS [DisplayOrder],
[Project2].[Id3] AS [Id3],
[Project2].[Name] AS [Name],
[Project2].[IsCore] AS [IsCore],
[Project2].[ExpectedReturn] AS [ExpectedReturn],
[Project2].[StandardDeviation] AS [StandardDeviation],
[Project2].[BroadAssetClassTypeId] AS [BroadAssetClassTypeId],
[Project2].[Id4] AS [Id4],
[Project2].[Name1] AS [Name1],
[Project2].[DisplayOrder1] AS [DisplayOrder1],
[Project2].[Id5] AS [Id5],
[Project2].[TAMAssetClassXAxisId] AS [TAMAssetClassXAxisId],
[Project2].[TAMAssetClassYAxisId] AS [TAMAssetClassYAxisId],
[Project2].[Correlation] AS [Correlation]
FROM ( SELECT
[Limit1].[Id] AS [Id],
[Limit1].[Number] AS [Number],
[Limit1].[ProductTypeId] AS [ProductTypeId],
[Limit1].[GlidePathTypeId] AS [GlidePathTypeId],
[Limit1].[CashEquivalentPreferenceId] AS [CashEquivalentPreferenceId],
[Limit1].[TAMClientId] AS [TAMClientId],
[Join4].[Id1] AS [Id1],
[Join4].[Number] AS [Number1],
[Join4].[TAMId] AS [TAMId],
[Join4].[Id2] AS [Id2],
[Join4].[TAMAssetClassId] AS [TAMAssetClassId],
[Join4].[Percentage] AS [Percentage],
[Join4].[ModelId] AS [ModelId],
[Join4].[DisplayOrder1] AS [DisplayOrder],
[Join4].[Id3] AS [Id3],
[Join4].[Name1] AS [Name],
[Join4].[IsCore] AS [IsCore],
[Join4].[ExpectedReturn] AS [ExpectedReturn],
[Join4].[StandardDeviation] AS [StandardDeviation],
[Join4].[BroadAssetClassTypeId] AS [BroadAssetClassTypeId],
[Join4].[Id4] AS [Id4],
[Join4].[Name2] AS [Name1],
[Join4].[DisplayOrder2] AS [DisplayOrder1],
[Join4].[Id5] AS [Id5],
[Join4].[TAMAssetClassXAxisId] AS [TAMAssetClassXAxisId],
[Join4].[TAMAssetClassYAxisId] AS [TAMAssetClassYAxisId],
[Join4].[Correlation] AS [Correlation]
FROM (SELECT TOP (2)
[Extent1].[Id] AS [Id],
[Extent1].[Number] AS [Number],
[Extent1].[ProductTypeId] AS [ProductTypeId],
[Extent1].[GlidePathTypeId] AS [GlidePathTypeId],
[Extent1].[CashEquivalentPreferenceId] AS [CashEquivalentPreferenceId],
[Extent1].[TAMClientId] AS [TAMClientId]
FROM [dbo].[TAM] AS [Extent1]
WHERE [Extent1].[Id] = @p__linq__0 ) AS [Limit1]
LEFT OUTER JOIN (SELECT [Extent2].[Id] AS [Id1], [Extent2].[Number] AS [Number], [Extent2].[TAMId] AS [TAMId], [Join3].[Id2], [Join3].[TAMAssetClassId], [Join3].[Percentage], [Join3].[ModelId], [Join3].[DisplayOrder1], [Join3].[Id3], [Join3].[Name1], [Join3].[IsCore], [Join3].[ExpectedReturn], [Join3].[StandardDeviation], [Join3].[BroadAssetClassTypeId], [Join3].[Id4], [Join3].[Name2], [Join3].[DisplayOrder2], [Join3].[Id5], [Join3].[TAMAssetClassXAxisId], [Join3].[TAMAssetClassYAxisId], [Join3].[Correlation]
FROM [dbo].[TAMAssetClassModel] AS [Extent2]
LEFT OUTER JOIN (SELECT [Extent3].[Id] AS [Id2], [Extent3].[TAMAssetClassId] AS [TAMAssetClassId], [Extent3].[Percentage] AS [Percentage], [Extent3].[ModelId] AS [ModelId], [Extent3].[DisplayOrder] AS [DisplayOrder1], [Extent4].[Id] AS [Id3], [Extent4].[Name] AS [Name1], [Extent4].[IsCore] AS [IsCore], [Extent4].[ExpectedReturn] AS [ExpectedReturn], [Extent4].[StandardDeviation] AS [StandardDeviation], [Extent4].[BroadAssetClassTypeId] AS [BroadAssetClassTypeId], [Extent5].[Id] AS [Id4], [Extent5].[Name] AS [Name2], [Extent5].[DisplayOrder] AS [DisplayOrder2], [Extent6].[Id] AS [Id5], [Extent6].[TAMAssetClassXAxisId] AS [TAMAssetClassXAxisId], [Extent6].[TAMAssetClassYAxisId] AS [TAMAssetClassYAxisId], [Extent6].[Correlation] AS [Correlation]
FROM [dbo].[TAMAssetClassAllocation] AS [Extent3]
INNER JOIN [dbo].[TAMAssetClass] AS [Extent4] ON [Extent3].[TAMAssetClassId] = [Extent4].[Id]
INNER JOIN [dbo].[BroadAssetClassType] AS [Extent5] ON [Extent4].[BroadAssetClassTypeId] = [Extent5].[Id]
LEFT OUTER JOIN [dbo].[TAMAssetClassCorrelation] AS [Extent6] ON [Extent3].[TAMAssetClassId] = [Extent6].[TAMAssetClassYAxisId] ) AS [Join3] ON [Extent2].[Id] = [Join3].[ModelId] ) AS [Join4] ON [Limit1].[Id] = [Join4].[TAMId]
) AS [Project2]
) AS [Project3]
) AS [Project4]
ORDER BY [Project4].[Id] ASC, [Project4].[C3] ASC, [Project4].[Id1] ASC, [Project4].[C2] ASC, [Project4].[Id3] ASC, [Project4].[Id2] ASC, [Project4].[Id4] ASC, [Project4].[C1] ASC',N'@p__linq__0 uniqueidentifier',@p__linq__0='63C18415-1101-47A9-9BDE-5950AAACA488'
当所有本地,DEV和QA环境中的数据库结构(表)和数据相同时,更有可能导致QA环境执行速度降低的原因是什么?如何加快执行此查询所需的时间?所有三种环境的索引都是相同的。
答案 0 :(得分:0)
检查两台服务器上的执行计划,很可能你会发现QA服务器没有优化其执行计划中的连接
如果是这种情况,您需要在QA服务器上运行update statistics
以优化其执行计划
答案 1 :(得分:0)
正如您所提到的,表和数据以及索引在两种环境中都是相同的。所以我能想到的唯一原因是QA环境中的统计数据没有更新。我之前也遇到过这个问题,更新统计数据就解决了这个问题。