USEFUL SITES :
Write for Us
Check Your SQLAs I mentioned in the introduction, I often use time to measure the performance of my T-SQL and stored procedures. Therefore, I’m interested in the following values:
This not only can help you improving your code in the development phase. But you can also trace a client application that lacks performance in your productive system. Query Analyzer can only show you the durations while executing. But I’m also interested in the network, and how it affects performance!Now, I’ll show you how to test how long it takes for your T-SQL code to run. You can modify the code to test your stored procedures as well. To get the starting and ending DATETIME from the server is easy, with this stored procedure:Create Procedure spTestSQL@strSQL as nvarchar(400),@dtStart datetime OUTPUT,@dtEnd datetime OUTPUTAsSet nocount onSet @dtStart= GetDate()Exec sp_executeSQL @strSqlSet @dtEnd= GetDate()return In the ‘lost milliseconds’ section, I told you about the VB Timer() function. Because the integer part of a date variable contains the date-part, and Timer() counts the seconds of a day, you can simulate your own date variable with milliseconds by using the DOUBLE data type:Dim dblNow as Double DblNow = Int(Now) + timer / 86400 ' the date from Now + the time from Timer!Debug.Print FormatDt(dblNow)Finally, to measure the SQL, you will easily get through this code:Sub TestSQL()Dim cmd As New ADODB.CommandDim dblCltStart As Double, dblCltEnd As Double ' client datesDim dblSvrStart As Double, dblSvrEnd As Double ' server datesWith cmd.ActiveConnection = “Your connection string”.CommandType = adCmdStoredProc' !!!CAUTION!!!!! ' clear server's caches;'only sysadmins can execute this command;'don't use this command on productive systems!.CommandText = "DBCC DROPCLEANBUFFERS" ' !!!CAUTION!!!!!' init command with the SQL to test and the start/end datetime parameters.CommandText = "spTestSQL".Parameters.Append .CreateParameter("@strSQL", adVarChar, adParamInput, 400, "YourSQL").Parameters.Append .CreateParameter("@dtStart", adDate, adParamOutput).Parameters.Append .CreateParameter("@dtEnd", adDate, adParamOutput)dblCltStart = Int(Now) + timer / 86400 ' client's start with milliseconds.Execute 'run procedure spTestSQLdblCltEnd = Int(Now) + timer / 86400 ' client's end with millisecondsdblSvrStart = .Parameters("@dtStart") ' server's startdblSvrEnd = .Parameters("@dtEnd") ' server's endEnd WithSet cmd = Nothing'output date/time and durationsDebug.Print _"Code started at: " & FormatDt(dblCltStart) & vbCrLf & _"Server started at: " & FormatDt(dblSvrStart) & vbCrLf & _"Sever ended at: " & FormatDt(dblSvrEnd) & vbCrLf & _"Code ended at: " & FormatDt(dblCltEnd) & vbCrLf & _vbCrLf & _"Full duration: " & FormatDt(dblCltEnd - dblCltStart, "hh:mm:ss") & vbCrLf & _"Server duration: " & FormatDt(dblSvrEnd - dblSvrStart, "hh:mm:ss") & vbCrLf & _"Network duration: " & _ FormatDt((dblCltEnd - dblCltStart) - (dblSvrEnd - dblSvrStart), "hh:mm:ss")End SubOf course you must substitute ”Your connection String” and “YourSQL” in the code above. In order to omit caching effects, I have entered the DBCC DROPCLEANBUFFERS command, which clears all caches. You need sysadmin rights to execute this. I warn you not to use it on a production system! After execution of the code, you will see the a similar output in your debug-window:Code started at: 2004-07-23 18:13:13.989Server started at: 2004-07-23 18:13:12.183Sever ended at: 2004-07-23 18:13:20.200Code ended at: 2004-07-23 18:13:22.771Full duration: 00:00:08.782Server duration: 00:00:08.017Network duration: 00:00:00.765You may have noticed the different timestamps on the server and client. That’s because the two clocks are not synchronized. For that reason the durations are correct, but you cannot calculate the time between sending and executing the command. If you like to do that, you can synchronize the two clocks by hardware or software. (E.g. use the sample code from http://www.thescarms.com/vbasic/syncclock.asp with your server’s ip.)You will be astonished, if you run this code on a local SQL Server or MSDE installation. Code started at: 2004-07-23 21:54:05.806Server started at: 2004-07-23 21:54:05.803Sever ended at: 2004-07-23 21:54:05.863Code ended at: 2004-07-23 21:54:05.866Full duration: 00:00:00.060Server duration: 00:00:00.060Network duration: 00:00:00.000It seems like the SQL Server starts before it gets the command to run. Why? Because SQL Server 2000 is so smart it can guess there is soon a command coming in!