SQL内连接按性能缓慢排序

时间:2017-12-05 14:55:35

标签: sql sql-server performance entity-framework azure

我们在Azure中有一个.NET应用程序,其中一个页面显示了时间条目的概述。我们使用Entity Framework来构建和执行SQL查询。当我们按时间表的一列(如日期或小时)排序时,它足够快。但是当我们对来自另一个表的列(来自内部联接,如用户diplayname)进行排序时,它真的很慢。

此外,当我们在DTU指标中查看我们的Azure门户时(在此查询之后),其峰值达到100%DTU。我们有P4服务计划,有500个DTU,所以我认为不一定是个问题。

这是一个由Entity Framework执行的慢查询示例:

exec sp_executesql N'SELECT 
[Project1].[Id] AS [Id], 
[Project1].[Date] AS [Date], 
[Project1].[Hours] AS [Hours], 
[Project1].[Notes] AS [Notes], 
[Project1].[StartEnd] AS [StartEnd], 
[Project1].[Status] AS [Status], 
[Project1].[Timer] AS [Timer], 
[Project1].[TimeRowId] AS [TimeRowId], 
[Project1].[InvoiceId] AS [InvoiceId], 
[Project1].[Pause] AS [Pause], 
[Project1].[ClientStatus] AS [ClientStatus], 
[Project1].[ExternalUrl] AS [ExternalUrl], 
[Project1].[ExternalName] AS [ExternalName], 
[Project1].[ApprovedBy] AS [ApprovedBy], 
[Project1].[ApprovedDate] AS [ApprovedDate], 
[Project1].[ClientApprovedBy] AS [ClientApprovedBy], 
[Project1].[ClientApprovedDate] AS [ClientApprovedDate]
FROM ( SELECT 
    [Extent1].[Id] AS [Id], 
    [Extent1].[Date] AS [Date], 
    [Extent1].[Hours] AS [Hours], 
    [Extent1].[Notes] AS [Notes], 
    [Extent1].[StartEnd] AS [StartEnd], 
    [Extent1].[Status] AS [Status], 
    [Extent1].[Timer] AS [Timer], 
    [Extent1].[TimeRowId] AS [TimeRowId], 
    [Extent1].[InvoiceId] AS [InvoiceId], 
    [Extent1].[Pause] AS [Pause], 
    [Extent1].[ClientStatus] AS [ClientStatus], 
    [Extent1].[ExternalUrl] AS [ExternalUrl], 
    [Extent1].[ExternalName] AS [ExternalName], 
    [Extent1].[ApprovedBy] AS [ApprovedBy], 
    [Extent1].[ApprovedDate] AS [ApprovedDate], 
    [Extent1].[ClientApprovedBy] AS [ClientApprovedBy], 
    [Extent1].[ClientApprovedDate] AS [ClientApprovedDate], 
    [Extent6].[DisplayName] AS [DisplayName]
    FROM      [dbo].[Time] AS [Extent1]
    INNER JOIN [dbo].[TimeRow] AS [Extent2] ON [Extent1].[TimeRowId] = [Extent2].[Id]
    INNER JOIN [dbo].[ProjectUser] AS [Extent3] ON [Extent2].[ProjectUserId] = [Extent3].[Id]
    INNER JOIN [dbo].[Project] AS [Extent4] ON [Extent3].[ProjectId] = [Extent4].[Id]
    INNER JOIN [dbo].[Customer] AS [Extent5] ON [Extent4].[CustomerId] = [Extent5].[Id]
    INNER JOIN [dbo].[User] AS [Extent6] ON [Extent3].[UserId] = [Extent6].[Id]
    WHERE ([Extent5].[CompanyId] = @p__linq__0) AND (@p__linq__1 <= [Extent1].[Date]) AND (@p__linq__2 >= [Extent1].[Date])
)  AS [Project1]
ORDER BY [Project1].[DisplayName] DESC
OFFSET 0 ROWS FETCH NEXT 25 ROWS ONLY ',N'@p__linq__0 int,@p__linq__1 datetime2(7),@p__linq__2 datetime2(7)',@p__linq__0=19218,@p__linq__1='2017-01-01 00:00:00',@p__linq__2='2017-12-31 00:00:00'

我们尝试在displayname(用户表)上添加一个非聚集索引,但这并没有产生任何不同。此外,我们在Azure上对数据库进行了自动调整,因此您必须认为Azure会自行添加必要的索引,对吧?

我们如何才能使此查询执行?

评论05-12-2017 我想我们解决了这个问题。我们在Database Tuning Adviser中执行查询并获得了9个推荐索引。在我们应用此建议后,查询非常快!

1 个答案:

答案 0 :(得分:0)

我无法回答您关于如何更快地进行查询的问题,正如Sean Lange所说,我们需要表格结构和执行计划。

我对Azure不太熟悉,但您应该看到正在使用的任何索引。

本文件:https://docs.microsoft.com/en-us/azure/sql-database/sql-database-advisor

  

使用建议创建的索引始终标记为   auto_created索引。您可以看到哪些索引是auto_created   看着sys.indexes视图。

该文档可以帮助您查找Azure是否为您创建了任何索引,或者您是否需要接受任何建议。