To analyze the performance of the SQL statements captured by SQL Profiler, you need to find the statements with high execution frequency and long time.
The tables generated by SQL Profiler are as follows:
Create table [dbo]. [LijiDownload] (
[RowNumber] [int] IDENTITY (0, 1) not null,
[EventClass] [int] NULL,
[TextData] [ntext] COLLATE SQL _Latin1_General_CP1_CI_AS NULL,
[ApplicationName] [nvarchar] (128) COLLATE SQL _Latin1_General_CP1_CI_AS NULL,
[NTUserName] [nvarchar] (128) COLLATE SQL _Latin1_General_CP1_CI_AS NULL,
[LoginName] [nvarchar] (128) COLLATE SQL _Latin1_General_CP1_CI_AS NULL,
[CPU] [int] NULL,
[Reads] [bigint] NULL,
[Writes] [bigint] NULL,
[Duration] [bigint] NULL,
[ClientProcessID] [int] NULL,
[SPID] [int] NULL,
[StartTime] [datetime] NULL,
[BinaryData] [image] NULL,
[SumIndex] [int] NULL, --- to analyze the added
PRIMARY KEY CLUSTERED
(
[RowNumber] ASC
) WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
Text data cannot be compared and compared with Group operations. Therefore, the sumIndex column is introduced to identify the same TextData value.
-- Generate the data of the sumIndex Column
Update LijiDownload set sumIndex =CheckSum (substring (TextData, 0,500 ))
-- 500:You can specify the maximum length of the captured SQL statement.
-- Find the first 10 statements that are frequently used:
Select top 10 sumIndex, count (sumIndex) as usedMuch
Into # temp
From LijiDownload
Group by sumIndex
Order by usedMuch desc
Select distinct t. usedMuch, substring (L. TextData, 0,500) as TextData
From # temp t inner join LijiDownload L
On t. sumIndex = L. sumIndex
Order by t. usedmuch desc
-- Find the top 10 statements in total
Select distinct substring (L. TextData, 0,500) as TextData, B. TotalDuration
From LijiDownload L inner join
(Select top 10 sum (Duration) as TotalDuration, sumIndex from LijiDownload
Where eventclass = 41 -- the type is an SQL statement
Group by sumIndex
Order by TotalDuration desc) B
On L. sumIndex = B. sumIndex
Order by B. TotalDuration desc