{"id":734,"date":"2011-12-07T13:00:02","date_gmt":"2011-12-07T18:00:02","guid":{"rendered":"http:\/\/enterpriseadmins.org\/blog\/?p=734"},"modified":"2011-12-03T13:07:50","modified_gmt":"2011-12-03T18:07:50","slug":"t-sql-to-show-how-long-a-task-took-to-execute","status":"publish","type":"post","link":"https:\/\/enterpriseadmins.org\/blog\/scripting\/t-sql-to-show-how-long-a-task-took-to-execute\/","title":{"rendered":"T-SQL to show how long a task took to execute"},"content":{"rendered":"<p>I have been working on various scripts that populate information into a SQL table.  The other day I was attempting to see how long some of these tables took to populate.  I could have updated the PowerShell script to include some Measure-Command information, but then I&#8217;d have to re-execute my tasks and see how long they took to run.  Since I already had all of the information populated into a table, and each row had a time stamp of when the data was entered, I could just use a T-SQL select statement to get the run time data I wanted.<\/p>\n<pre>\r\nSELECT CAST( (MAX(dataCollected) - MIN(dataCollected)) as time) as TotalRunTime, COUNT(*) as RecCount FROM tblTestInserts WHERE collectionID = MAX(collectionID)\r\n<\/pre>\n<p>The above example looks at two columns in my table.  The first column (dataCollected) is of the datetime type and has a default binding of GetDate() &#8212; so it is automatically populated with the SQL Servers date and time every time a record is inserted.  The second column (collectionID) is an integer that is calculated each time I execute the script &#8212; inside of powershell I select the maximum value of this column and then add 1.  This provides a way know how many times the task has been executed and which data is the most current.  <\/p>\n<p>This gave me exactly the information I was looking for without needing to modify my PowerShell script and re-run the collection.  It also allows me to compare current runs to previous runs (by changing the value of the collectionID in the WHERE clause).  I can also be more specific&#8230;I can add an &#8216;AND&#8217; to the where clause and see how long a specific part of the collection took to run.<\/p>\n<p>In a future post I plan to share how I was able to take a vbscript that took 8 hours to complete condense it into a 28 minute execution time powershell script.  (Here is a preview of the magic sauce &#8212; Group-Object -Property DN -AsHashTable -AsString)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have been working on various scripts that populate information into a SQL table. The other day I was attempting to see how long some of these tables took to populate. I could have updated the PowerShell script to include &hellip; <a href=\"https:\/\/enterpriseadmins.org\/blog\/scripting\/t-sql-to-show-how-long-a-task-took-to-execute\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":6,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[3],"tags":[],"class_list":["post-734","post","type-post","status-publish","format-standard","hentry","category-scripting"],"_links":{"self":[{"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/posts\/734","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/comments?post=734"}],"version-history":[{"count":6,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/posts\/734\/revisions"}],"predecessor-version":[{"id":851,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/posts\/734\/revisions\/851"}],"wp:attachment":[{"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/media?parent=734"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/categories?post=734"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/enterpriseadmins.org\/blog\/wp-json\/wp\/v2\/tags?post=734"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}