您可以创建一个表变量,并在其中跟踪代码段执行时间的持续时间。
例如:
DECLARE @Tracking TAABLE
(
ID int IDENTITY (1, 1) PRIMARY KEY NOT NULL,
TheStep varchar(50),
Start datetime,
End datetime
)
DECLARE
@TheStep varchar(50),
@Start datetime,
@End datetime
-- first block of code to track
SET @TheStep = 'Grab data from MyTable.'
SET @Start = getdate()
SELECT * FROM MyTable
SET @End = getdate()
INSERT INTO @Tracking (TheStep, Start, End)
SELECT @TheStep, @Start, @End
-- reset variables for next batch of commands to measure
SELECT @TheStep = NULL, @Start = NULL, @End = NULL
-- after all is said and done, display the results along with duration (ms) for each.
SELECT TheStep, DATEDIFF(ms, Start, End)
FROM @Tracking
SQL事件探查器可以显示整个批处理以及单个语句的聚合数据(包括读/写)。不过这不是T-SQL方法。 –