{"id":20,"date":"2014-10-27T21:56:00","date_gmt":"2014-10-27T21:56:00","guid":{"rendered":""},"modified":"2016-01-30T23:27:11","modified_gmt":"2016-01-30T23:27:11","slug":"t-sql-executing-query-multiple-times","status":"publish","type":"post","link":"https:\/\/tpodolak.com\/blog\/2014\/10\/27\/t-sql-executing-query-multiple-times\/","title":{"rendered":"T-SQL &#8211; executing query multiple times"},"content":{"rendered":"<p>For time to time during my everyday work I have to do some SQL query optimization. The simplest way of measuring whether my changes improved the performance or not, is simply measure the execution time of given query (I know that this is not the best way of doing that, but let&#8217;s leave aside execution plans for now). However measuring time of single executin is not very meaningfull. Thats why I like execute query multiple times by using this simple trick<\/p>\n<pre lang=\"bash\">\r\nGO 100\r\n<\/pre>\n<p><a href=\"http:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2014\/10\/t-sql-executing-query-multiple-times\/GO1000.png\" rel=\"attachment wp-att-292\"><img decoding=\"async\" src=\"http:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2014\/10\/t-sql-executing-query-multiple-times\/GO1000.png\" alt=\"executing query multiple times\" width=\"450\" class=\"aligncenter size-full wp-image-292\" srcset=\"https:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2014\/10\/t-sql-executing-query-multiple-times\/GO1000.png 496w, https:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2014\/10\/t-sql-executing-query-multiple-times\/GO1000-150x51.png 150w, https:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2014\/10\/t-sql-executing-query-multiple-times\/GO1000-300x102.png 300w\" sizes=\"(max-width: 496px) 100vw, 496px\" \/><\/a><br \/>\nand then calculate an average time of execution by dividing the total time by execution count. As You can see, all You have to do is to specify execution count after the GO statment. Despite the fact that Management Studio indicates that there is an error in the SQL, this is perfectly valid code. Of course You should hide execution results in order to measure only the time of execution, not the time of rendering data. To do that, right click on query window and choose <i>&#8220;Query Options&#8230;&#8221;<\/i><br \/>\n<a href=\"http:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2014\/10\/t-sql-executing-query-multiple-times\/queryoptions.png\" rel=\"attachment wp-att-293\"><img decoding=\"async\" src=\"http:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2014\/10\/t-sql-executing-query-multiple-times\/queryoptions.png\" alt=\"queryoptions\" width=\"450\" class=\"aligncenter size-full wp-image-293\" srcset=\"https:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2014\/10\/t-sql-executing-query-multiple-times\/queryoptions.png 456w, https:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2014\/10\/t-sql-executing-query-multiple-times\/queryoptions-150x96.png 150w, https:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2014\/10\/t-sql-executing-query-multiple-times\/queryoptions-300x193.png 300w\" sizes=\"(max-width: 456px) 100vw, 456px\" \/><\/a><br \/>\nthen select &#8220;Results&#8221; node and check &#8220;Discard results after execution&#8221;<br \/>\n<a href=\"http:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2014\/10\/t-sql-executing-query-multiple-times\/queryoptionshighlighted.png\" rel=\"attachment wp-att-294\"><img decoding=\"async\" src=\"http:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2014\/10\/t-sql-executing-query-multiple-times\/queryoptionshighlighted.png\" alt=\"queryoptionshighlighted\" width=\"450\" class=\"aligncenter size-full wp-image-294\" srcset=\"https:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2014\/10\/t-sql-executing-query-multiple-times\/queryoptionshighlighted.png 649w, https:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2014\/10\/t-sql-executing-query-multiple-times\/queryoptionshighlighted-150x91.png 150w, https:\/\/tpodolak.com\/blog\/wp-content\/uploads\/2014\/10\/t-sql-executing-query-multiple-times\/queryoptionshighlighted-300x181.png 300w\" sizes=\"(max-width: 649px) 100vw, 649px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>For time to time during my everyday work I have to do some SQL query optimization. The simplest way of measuring whether my changes improved the performance or not, is simply measure the execution time of given query (I know that this is not the best way of doing that, but let&#8217;s leave aside execution [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[37,41],"tags":[185,187],"class_list":["post-20","post","type-post","status-publish","format-standard","hentry","category-sql","category-t-sql","tag-sql","tag-t-sql"],"_links":{"self":[{"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/posts\/20","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/comments?post=20"}],"version-history":[{"count":5,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/posts\/20\/revisions"}],"predecessor-version":[{"id":531,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/posts\/20\/revisions\/531"}],"wp:attachment":[{"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/media?parent=20"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/categories?post=20"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tpodolak.com\/blog\/wp-json\/wp\/v2\/tags?post=20"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}