Analyze the performance of SQL statements-Compare NText Column value by using CheckSum

Source: Internet
Author: User

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
[RowNumber] ASC

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.