SQL Profiler是否与LocalDB一起使用?

时间:2013-07-10 14:56:52

标签: sql-server profiling localdb

是否可以使用SQL事件探查器来观察请求LocalDB实例的查询?

5 个答案:

答案 0 :(得分:19)

只要知道正确的服务器名称,就可以像使用所有其他SQL版本一样使用SQL事件探查器。您可以使用SqlLocalDb实用程序找到服务器名称。

要查找它,请使用sqllocaldb info YourInstanceName查找Instance Pipe Name。它的格式为np:\\.\pipe\LOCALDB#12345\tsql\query

使用此作为服务器名称连接到服务器并开始分析

答案 1 :(得分:3)

这就是我在SQL Server Express 2012上使用的内容(注意:“LocalDB * - 我从未使用过LocalDB,所以可能与”常规“SQL Server Express不同。)

第1步:设置跟踪

这基本上就是“努力工作”。首先需要找出SQL Server的默认日志目录所在的位置。您需要此目录名称来指定跟踪文件。

然后通过执行以下操作创建跟踪:

DECLARE @TraceID int
DECLARE @tracefile nvarchar(255)
DECLARE @endDate datetime
DECLARE @size bigint

-- no file extension!
SET @tracefile = 'C:\Data\sqlserver\MSSQL11.SQLEXPRESS\MSSQL\Log\mydb_trace' 

-- tracing stops when either the max size of the file is reached 
-- or the enddate (whichever occurs first)
-- size is in MB
SET @size = 250
SET @enddate = DateAdd(DAY, 15, GetDate())

EXEC @rc = sp_trace_create @TraceID output, 2, @tracefile, @size, @enddate

现在,对于应跟踪的每个事件,您需要多次调用sp_trace_setevent来定义应返回该事件的列:

有关事件和列的完整列表,请参阅:http://msdn.microsoft.com/en-US/library/ms186265%28v=sql.90%29.aspx

-- Enable Event: 45 = SP:StmtCompleted
EXEC sp_trace_setevent @TraceID, 45, 27, @on -- 27: EventClass
EXEC sp_trace_setevent @TraceID, 45, 12, @on -- 12: SPID
EXEC sp_trace_setevent @TraceID, 45, 35, @on -- 35: DatabaseName
EXEC sp_trace_setevent @TraceID, 45, 11, @on -- 11: SQLSecurityLoginName
EXEC sp_trace_setevent @TraceID, 45,  6, @on --  6: NTUserName
EXEC sp_trace_setevent @TraceID, 45,  8, @on --  8: ClientHostName
EXEC sp_trace_setevent @TraceID, 45, 10, @on -- 10: ApplicationName
EXEC sp_trace_setevent @TraceID, 45,  1, @on --  1: TextData
EXEC sp_trace_setevent @TraceID, 45, 13, @on -- 13: Duration
EXEC sp_trace_setevent @TraceID, 45, 14, @on -- 14: StartTime
EXEC sp_trace_setevent @TraceID, 45, 15, @on -- 15: EndTime
EXEC sp_trace_setevent @TraceID, 45, 18, @on -- 18: CPU
EXEC sp_trace_setevent @TraceID, 45, 29, @on -- 29: Nesting Level

上述调用的所有必须为每个要跟踪的事件完成!

我发现事件12 = SQL:BatchCompleted42 = SP:Starting43 = SP:Completed45 = SP:StmtCompleted50 = SQL Transaction是最有趣的事件。

您可以选择设置过滤器,我通常会过滤掉系统事件,只显示特定数据库的事件:

-- Exclude system events (so only user events are shown)
-- 60: IsSystem Column
--  0: logical Operator: AND (only)
--  1: comparison operator: not equal
--  1: value
EXEC sp_trace_setfilter @TraceID, 60, 0, 1, 1

-- only mydb database
EXEC sp_trace_setfilter @TraceID, 35, 0, 6, N'mydb'

设置跟踪后,必须激活它:

EXEC sp_trace_setstatus @TraceID, 1

(注意上面必须作为单个批次运行,因为使用了变量)。

要查看跟踪的定义方式,可以使用以下语句:

select traceid,
       case property
         when 1 then 'Trace Options'
         when 2 then 'Trace file'
         when 3 then 'Max. file size'
         when 4 then 'Stop time'
         when 5 then 'Status'
       end as property_name,
       case
         when property = 5 then
            case convert(nvarchar(max), value)
               when '1' then 'Active'
               else 'Inactive'
            end
         else convert(nvarchar(max), value)
       end as value
from ::fn_trace_getinfo(null)
where property in (2,3,5)

现在将您的应用程序或任何问题语句运行到要跟踪的数据库。

第2步:检索跟踪信息

为此,您需要知道实际跟踪文件的完整路径(从步骤1开始)。请注意,对于fn_trace_gettable,您需要指定文件,包括文件扩展名。

SELECT ApplicationName,
       LoginName,
       HostName,
       SPID,
       Duration,
       StartTime,
       EndTime,
       DatabaseName,
       reads,
       writes,
       RowCounts,
       cpu,
       EventClass,
       case EventClass
        when 10 then 'RPC:Completed'
        when 11 then 'RPC:Starting'
        when 12 then 'SQL:BatchCompleted'
        when 13 then 'SQL:BatchStarting'
        when 40 then 'SQL:StmtStarting'
        when 41 then 'SQL:StmtCompleted'
        when 42 then 'SP:Starting'
        when 43 then 'SP:Completed'
        when 44 then 'SP:StmtStarting'
        when 45 then 'SP:StmtCompleted'
        when 50 then 'SQL Transaction'
        when 67 then 'Execution Warnings'
        when 71 then 'Prepare SQL'
        when 72 then 'Exec Prepared SQL'
        when 73 then 'Unprepare SQL'
       end as Event,
       LineNumber,
       TextData
FROM ::fn_trace_gettable('C:\Data\sqlserver\MSSQL11.SQLEXPRESS\MSSQL\Log\mydb_trace.log', default)
order by StartTime;

调整以上内容以返回您感兴趣的信息。

获得所需信息后,您必须关闭跟踪:

步骤3:禁用跟踪

为此您需要知道Trace-ID(例如,通过运行步骤1中的“info语句”。)使用此ID,您需要先停止跟踪,然后删除它:

-- stop the trace
EXEC sp_trace_setstatus @TraceID, 0

-- delete the trace
EXEC sp_trace_setstatus @TraceID, 2

答案 2 :(得分:3)

来自http://expressprofiler.codeplex.com/

  

ExpressProfiler(又名SqlExpress Profiler)是一个简单但足够好的替代SQL Server Profiler和基本GUI。

     

没有要求,没有安装。

     

可与SQL Server 2005/2008 / 2008r2 / 2012的Express和非Express版本(包括LocalDB)一起使用

答案 3 :(得分:1)

就像将服务器设置为(LocalDB)\ v11.0

一样简单

http://expressprofiler.codeplex.com/discussions/456518

答案 4 :(得分:0)

Microsoft SQL Server 2012 Express LocalDB是针对程序开发人员的SQL Server Express执行模式。

SQL Server Express不提供Sql Profiler。

因此,您不能将Sql Profiler用于LocalDB。

但是,你可以通过其他方式。

How to use SQL Profiler with SQL Server Express Edition