简体   繁体   中英

Too Long Query Duration in SQL Server 2008 R2 DataCenter

We have a query and this is an actual execution plan

替代文字 As you can see - Clustered Index Seek takes 99%. Also it seeks on primary keys (type int ).

Table Source has 275 000 rows.
Table AuthorSource has 2 275 000 rows.

No partition and compression used.

The problem is that first time execution takes 25-40 seconds. But the second run successively takes 1-2 seconds.

Also we have replication, queue reader, log reader agents running on this server.
Amount of RAM: 4GB
Sql Server uses: 3.7GB

We think, that sql caches query after first execution for some period of time, and this is the reason, that second run takes only 1-2 seconds.

But irrespective of cache and other reasons, it is very strange, that primary key index seek query takes 20-40 seconds.

This issue is repeated. Any different parameters we provide to the query - we get same results: very long first time query and fast the second and the following.

May be some additional settings or Resource Governor ability we have to use ?

        exec sp_executesql N'
SELECT [Project1].[C1] AS         [C1]
FROM   ( SELECT CAST(1 AS bit) AS X
       ) AS [SingleRowTable1]
       LEFT OUTER JOIN
              (SELECT [GroupBy1].[A1] AS                        [C1]
              FROM    ( SELECT COUNT(CAST(1 AS bit)) AS         [A1]
                      FROM    (SELECT [Extent1].[Mention_ID]                       AS [Mention_ID]                      ,
                                      [Extent1].[Theme_ID]                         AS [Theme_ID]                        ,
                                      [Extent1].[Mention_Weight]                   AS [Mention_Weight]                  ,
                                      [Extent1].[AuthorSource_ID]                  AS [AuthorSource_ID1]                ,
                                      [Extent1].[Mention_CreationDate]             AS [Mention_CreationDate]            ,
                                      [Extent1].[Mention_DeletedMark]              AS [Mention_DeletedMark]             ,
                                      [Extent1].[Mention_AuthorTags]               AS [Mention_AuthorTags]              ,
                                      [Extent1].[Mention_Tonality]                 AS [Mention_Tonality]                ,
                                      [Extent1].[Mention_Comment]                  AS [Mention_Comment]                 ,
                                      [Extent1].[Mention_AdditionDate]             AS [Mention_AdditionDate]            ,
                                      [Extent1].[UserToAnswer_ID]                  AS [UserToAnswer_ID]                 ,
                                      [Extent1].[GeoName_ID]                       AS [GeoName_ID]                      ,
                                      [Extent1].[Geo_ID]                           AS [Geo_ID]                          ,
                                      [Extent1].[Mention_PermaLinkHash]            AS [Mention_PermaLinkHash]           ,
                                      [Extent1].[Mention_IsFiltredByAuthor]        AS [Mention_IsFiltredByAuthor]       ,
                                      [Extent1].[Mention_IsFiltredByGeo]           AS [Mention_IsFiltredByGeo]          ,
                                      [Extent1].[Mention_IsFiltredBySource]        AS [Mention_IsFiltredBySource]       ,
                                      [Extent1].[Mention_IsFiltredBySourceType]    AS [Mention_IsFiltredBySourceType]   ,
                                      [Extent1].[GengineLog_InstanceId]            AS [GengineLog_InstanceId]           ,
                                      [Extent1].[Mention_PermaLinkBinaryHash]      AS [Mention_PermaLinkBinaryHash]     ,
                                      [Extent1].[Mention_APIType]                  AS [Mention_APIType]                 ,
                                      [Extent1].[Mention_IsFilteredByAuthorSource] AS [Mention_IsFilteredByAuthorSource],
                                      [Extent1].[Mention_IsFavorite]               AS [Mention_IsFavorite]              ,
                                      [Extent1].[Mention_SpamType]                 AS [Mention_SpamType]                ,
                                      [Extent1].[MentionContent_ID]                AS [MentionContent_ID]               ,
                                      [Extent2].[AuthorSource_ID]                  AS [AuthorSource_ID2]                ,
                                      [Extent2].[Author_ID]                        AS [Author_ID]                       ,
                                      [Extent2].[Source_ID]                        AS [Source_ID]                       ,
                                      [Extent2].[Author_Nick]                      AS [Author_Nick]                     ,
                                      [Extent2].[Author_UrlBinaryHash]             AS [Author_UrlBinaryHash]            ,
                                      [Extent2].[AuthorSource_Type]                AS [AuthorSource_Type]               ,
                                      [Extent2].[Author_Url]                       AS [Author_Url]                      ,
                                      [Extent2].[AuthorSource_Description]         AS [AuthorSource_Description]        ,
                                      [Extent2].[AuthorSource_Gender]              AS [AuthorSource_Gender]
                              FROM    [dbo].[Mention]                              AS [Extent1]
                                      LEFT OUTER JOIN [dbo].[AuthorSource]         AS [Extent2]
                                      ON      [Extent1].[AuthorSource_ID] = [Extent2].[AuthorSource_ID]
                              WHERE   (
                                              [Extent1].[Mention_DeletedMark] <> CAST(1 AS bit)
                                      )
                              AND
                                      (
                                              [Extent1].[Mention_IsFiltredByAuthor] <> CAST(1 AS bit)
                                      )
                              AND
                                      (
                                              [Extent1].[Mention_IsFilteredByAuthorSource] <> CAST(1 AS bit)
                                      )
                              AND
                                      (
                                              [Extent1].[Mention_IsFiltredByGeo] <> CAST(1 AS bit)
                                      )
                              AND
                                      (
                                              [Extent1].[Mention_IsFiltredBySource] <> CAST(1 AS bit)
                                      )
                              AND
                                      (
                                              [Extent1].[Mention_IsFiltredBySourceType] <> CAST(1 AS bit)
                                      )
                              )                              AS [Filter1]
                              LEFT OUTER JOIN [dbo].[Source] AS [Extent3]
                              ON      [Filter1].[Source_ID] = [Extent3].[Source_ID]
                      WHERE   (
                                      [Filter1].[Theme_ID] = @p__linq__49557
                              )
                      AND
                              (
                                      [Extent3].[Source_Type] <> @p__linq__49558
                              )
                      ) AS [GroupaBy1]
              ) AS [Project1]
       ON     1 = 1
',N'@p__linq__49557 int,@p__linq__49558 int',@p__linq__49557=7966,@p__linq__49558=8

IndexSeeking Performance Information

Also we wrote query manually in sql with this simple code:

        Select COUNT(1) from Mention m inner join AuthorSource auth on m.AuthorSource_ID = auth.AuthorSource_ID inner join
    Source s on auth.Source_ID = s.Source_ID where 
    m.Mention_DeletedMark = 0 AND m.Mention_IsFilteredByAuthorSource = 0 AND m.Mention_IsFiltredByAuthor = 0 
    AND m.Mention_IsFiltredByGeo = 0 AND m.Mention_IsFiltredBySource = 0 AND m.Mention_IsFiltredBySourceType = 0
    AND m.Theme_ID = 7966
    and s.Source_Type <> 8 

and execution plan is the same that we posted.

The query is quite hairy, but It looks like you are missing an index on Mention.Theme_ID ?

Sql server is having problem because a lot of <> are used, meaning that it cannot use an index and must fetch everything and then sort it out.

After Martin's advices in comments to question, the answer is in understanding how SQL Server build the execution plans and counting disk reads operation needed to first query run.

In our particular situation, forced inner hash join instead of inner join give to us result as we expected and different execution plan that SQL choose by default.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM