2012-04-30 12 views
0

SQLサーバーがクエリプラン(満足)を確立した後、1秒以下実行するのにかなり複雑なクエリがあります。しかし、初めてクエリが実行されるときに、プロファイラーに応じたShowPlanXMLイベントは約14秒かかります(満足のいくものではありません)。SQL ServerでShowPlan XMLを最適化することは可能ですか

ShowPlanXMLを初めて実行したときに、それがより迅速に完了するように最適化する方法はありますか?

私はプランガイドを作成しますか?ここでの情報については

は、SQLクエリ(NHibernateはによって生成される)である:

SELECT top 20 this_.UserId as UserId55_0_, this_.User_Version as User2_55_0_, this_.User_ApplicationId as User3_55_0_, this_.User_DeletedOn as User4_55_0_, this_.User_CreatedOn as User5_55_0_, this_.User_ModifiedOn as User6_55_0_, this_.User_CreatedById as User7_55_0_, this_.User_CreatedByName as User8_55_0_, this_.User_ModifiedById as User9_55_0_, this_.User_ModifiedByName as User10_55_0_, this_.User_Name as User11_55_0_, this_.User_ExternalId as User12_55_0_, this_.User_DynamicFields as User13_55_0_, 
this_.User_FirstName as User14_55_0_, this_.User_LastName as User15_55_0_, this_.User_Prefix as User16_55_0_, this_.User_Gender as User17_55_0_, this_.User_Language as 
User18_55_0_, this_.User_Code as User19_55_0_, this_.User_Nationality as User20_55_0_, this_.User_FirstLanguage as User21_55_0_, this_.User_DrivingLicence as User22_55_0_, 
this_.User_Category as User23_55_0_, this_.User_UserStatus as User24_55_0_, this_.User_UserType as User25_55_0_, this_.User_WorkPhone as User26_55_0_, this_.User_MobilePhone as 
User27_55_0_, this_.User_Fax as User28_55_0_, this_.User_Mail as User29_55_0_, this_.User_Login as User30_55_0_, this_.User_Password as User31_55_0_, this_.User_BornOn as 
User32_55_0_, this_.User_StartedOn as User33_55_0_, this_.User_FinishedOn as User34_55_0_, this_.User_Address as User35_55_0_, this_.User_PostalCode as User36_55_0_, 
this_.User_City as User37_55_0_, this_.User_Country as User38_55_0_, this_.User_PositionTitle as User39_55_0_, this_.User_Comments as User40_55_0_, this_.User_OptionalField1 as 
User41_55_0_, this_.User_OptionalField2 as User42_55_0_, this_.User_OptionalField3 as User43_55_0_, this_.User_PasswordConsecutiveFailedAttempts as User44_55_0_, 
this_.User_PasswordModificationDate as User45_55_0_, this_.User_WrongPasswordAttemptDate as User46_55_0_, this_.User_PictureUrl as User47_55_0_, this_.User_PasswordModificationStatus as User48_55_0_, this_.User_SecretQuestionConsecutiveFailedAttempts as User49_55_0_, this_.User_PlatformMailTransfer as User50_55_0_, this_.User_TimeZoneId as User51_55_0_, this_.User_ConnectionState as User52_55_0_, this_.User_LastConnectionId as User53_55_0_, this_.User_TotalPercentRealized as User54_55_0_ 
FROM Dir_User this_ 
WHERE this_.UserId in (
     SELECT distinct this_0_.UserId as y0_ 
     FROM Dir_User this_0_ inner join Dir_UserDynamicGroup dynamicgro3_ on this_0_.UserId=dynamicgro3_.UsDy_UserId 
     inner join Dir_Group dynamicgro1_ on dynamicgro3_.UsDy_DynamicGroupId=dynamicgro1_.GroupId 
     WHERE dynamicgro1_.GroupId = (51904517) 
     and this_0_.User_ApplicationId = 65536 
     and this_0_.User_DeletedOn is null 
     and this_0_.UserId in (
      SELECT distinct this_0_0_.TargetUserId as y0_ 
      FROM Dir_UserGroupMember this_0_0_ 
      WHERE this_0_0_.OwnerUserId = 7341195 
      and ((this_0_0_.Scope & 139280) != 0 or ((this_0_0_.Scope & 139280) != 0 
      and this_0_0_.GroupId = this_0_0_.SubGroupId)))) 
      ORDER BY this_.User_Name asc 
+0

コンパイルによって最初に実行されるクエリの実行時間が長くかかったり、文字通りshowplanがそれを長く取っているということですか?後者の場合、私はそれについて心配しないと言います。アプリケーションでクエリを実行しておらず、ショープランの結果を戻すことはありません。これはチューニングとトラブルシューティングのみを目的とした診断ツールです。 –

+0

これは前者です。プロファイラーによれば、ShowPlanXmlイベントは、クエリがSSMSで実行されるたびに実行されます。最初にコンパイルするのに14秒かかってから、そのプランはキャッシュからアクセスされます。 DBCC DROPCLEANBUFFERSを実行すると、キャッシュがクリアされ、計画が14秒で再計算されます –

+0

この(http://msdn.microsoft.com/en-us/library/ms188661.aspx)によれば、そのイベントは毎回発生します。クエリが実行され、「このイベントクラスを含むトレースで重大なパフォーマンスオーバーヘッドが発生する可能性があります」と言います。クエリのパフォーマンスを調べる場合は、代わりにDMV、つまりsys.dm_exec_query_statsとその兄弟を調べることをお勧めします。 –

答えて

1

ショープランのプロファイラのイベントは、SQL Serverのパフォーマンスに大きな影響を持つことができます(sqlserver.query_post_execution_showplan Performance Impactを参照してください)。ストアドプロシージャをコンパイルするのにかかる時間を正確に表示するには、別の方法を使用する必要があります。

プランキャッシュを直接調べることによって、プランのコンパイルに要した時間を特定できます(Identifying High Compile Time Statements from the Plan Cacheを参照)。

残念ながら、単純にクエリの複雑さを軽減する以外のSQL Serverクエリのコンパイル時間を短縮する方法はたくさんあります。プランのキャッシュによって計画のコンパイルが必要な頻度を減らそうとするのは、パフォーマンスを向上させるための標準的なアプローチです。

関連する問題