Azureはじめました

Windows Azureで業務システムを組んでみる日記

SQLAzureでSQL Server Profiler的なデータを取得する

早くProfilerに対応してください(´;ω;`)

クエリで出力

パフォーマンスを監視するクエリ  SQL Azureに限らず SQL Server でも使えるスクリプトですが、プロファイラを現在使うことのできない SQL Azureでは下記のスクリプトが便利です。このスクリプトを使うと、頻繁に実行されるクエリをその頻度の順番で最大10個まで出力させることができます。(ただし、データベースごとの出力内容となります。)

create table #t 
( 
  query_hash varbinary(64), 
  query_plan_hash varbinary(64), 
  num_cache_entries bigint, 
  execution_count bigint, 
  min_creation_time datetime, 
  max_last_execution_time datetime, 
  total_worker_time bigint, 
  min_worker_time bigint, 
  max_worker_time bigint, 
  avg_worker_time float 
) 
go 


insert into #t 
select top 10 
  query_hash, 
  query_plan_hash, 
  count(*) as num_cache_entries, 
  sum(execution_count) as execution_count, 
  min(creation_time) as min_creation_time, 
  max(last_execution_time) as max_last_execution_time, 
  sum(total_worker_time) as total_worker_time, 
  min(min_worker_time) as min_worker_time, 
  max(max_worker_time) as max_worker_time, 
  sum(total_worker_time) / sum(execution_count) as avg_worker_time 
from sys.dm_exec_query_stats 
group by query_hash, query_plan_hash 
order by total_worker_time desc; 

declare @query_hash varbinary(64), @query_plan_hash varbinary(64) 
declare @query_num int = 1 
declare c1 cursor for select query_hash, query_plan_hash from #t 
open c1 
fetch next from c1 into @query_hash, @query_plan_hash; 
while @@fetch_status = 0 

begin 
  select top 1 
    @query_num as query_rank, 
    s.*, 
    p.*, 
    replace (replace (substring (st.[text], s.statement_start_offset/2 + 1, 
  case when s.statement_end_offset = -1 then len (convert(nvarchar(max), st.[text])) 
  else s.statement_end_offset/2 - s.statement_start_offset/2 + 1 
  end), char(13), ' '), char(10), ' ') as sample_statement_text 
  from sys.dm_exec_query_stats s 
    cross apply sys.dm_exec_query_plan(s.plan_handle) as p 
    cross apply sys.dm_exec_sql_text(s.sql_handle) as st 
  where s.query_hash = @query_hash 
    and s.query_plan_hash = @query_plan_hash; 

  fetch next from c1 into @query_hash, @query_plan_hash; 
  select @query_num = @query_num + 1; 
end 
deallocate c1 
go 

drop table #t 
go 
[http://blogs.msdn.com/b/dsazurejp/archive/2011/06/27/sql-azure-performance-script.aspx:title=[SQL Azure] 頻繁に実行されるクエリのパフォーマンスを監視する便利なスクリプト - Microsoft Azure サポート チーム サイト - Site Home - MSDN Blogs]

f:id:twisted0517:20150821194531p:plain

でてきた。

ShowPlanXMLをもうちょっと見やすく

query_plan列にはShowPlanXML形式のXMLが有るんだがこれが実に見にくい。
もうちょっと何とかならんのかと思ったら何とかなった。

I've just rolled out the awesome HTML+CSS+JS view of the showplan XML using the XSLT from this project : http://code.google.com/p/html-query-plan/ (you can see it now if you visit the original link, above).

Interpreting SQL Server's Showplan XML - Database Administrators Stack Exchange

JustinPealing/html-query-plan · GitHub

これをダウンロード、展開して展開したフォルダにmsxsl.exeをコピーする。
Download Command Line Transformation Utility (msxsl.exe) from Official Microsoft Download Center

んで、qp.batにXMLを流し込めばOK
f:id:twisted0517:20150821195957p:plain
マウスホバーで詳細も見れる。
やるじゃん╭( ・ㅂ・)و

SSMSから簡単に

SSMSでツール>外部ツールを選んで
f:id:twisted0517:20150821200110p:plain
こうじゃ。

後はShowPlanXMLを表示した状態で今設定したQueryPlanXMLVisualizerを起動すればOK