Home > Uncategorized > Timing SQL statements

Timing SQL statements

Often, when comparing two SQL statements for speed, you end up relying on the counter in the bottom left counter of SQL Management studio,
but if you’re trying to shave milliseconds off a query that takes under a second than the result "00:00:00" doesn’t give you anything to go on.

So; instead, if you declare a @timeStart as a datetime, run your query, then get a datediff in milliseconds against the current time, then you get something more meaningful.

declare @timeStart as datetime
set @timeStart = getdate()
select count(*) from foo
select datediff(ms,@timestart,getdate())

In the above code block "select count(*) from foo" is the query I want to test.

Categories: Uncategorized
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: