{"id":2178,"date":"2018-09-04T17:49:39","date_gmt":"2018-09-04T17:49:39","guid":{"rendered":"http:\/\/nenadnoveljic.com\/blog\/?p=2178"},"modified":"2018-09-04T17:49:39","modified_gmt":"2018-09-04T17:49:39","slug":"sql-server-extended-events-trace-overhead","status":"publish","type":"post","link":"https:\/\/nenadnoveljic.com\/blog\/sql-server-extended-events-trace-overhead\/","title":{"rendered":"SQL Server Extended Events Trace Overhead"},"content":{"rendered":"<h1>SQL Server<\/h1>\n<p>In this blog post I&#8217;ll be measuring the Extended Events Trace overhead. Besides that I&#8217;ll be explaining potential pitfalls when interpreting the trace data. <\/p>\n<p>The testing was conducted on SQL Server 2017, build 14.0.1000.169.<\/p>\n<p>The following SQL batch took <span style=\"color:red\">61<\/span> seconds to complete:<\/p>\n<pre><code>\r\nDECLARE @starttime datetime, @endtime datetime\r\nDECLARE @cnt int = 0, @ts_start int ;\r\n\r\nSET @starttime =getdate() ;\r\n\r\nBEGIN\r\n    WHILE @cnt < 100000000\r\n\tBEGIN\r\n\t   SET @cnt = @cnt + 1;\r\n\tEND;\r\nEND\r\n\r\nSET @endtime = GETDATE() ;\r\nSELECT @@spid spid, Datediff(s, @starttime, @endtime) elapsed_seconds\r\n\r\nGO\r\n\r\nspid      <span style=\"color:red\">elapsed_seconds<\/span>\r\n58        <span style=\"color:red\">61<\/span>\r\n<\/code><\/pre>\n<p>By sampling sys.dm_exec_requests in another session we can verify that the batch is CPU bound (the times are in ms):<\/p>\n<pre><code>\r\ndeclare @spid int = 58, @i int = 1, @samples int = 60 ;\r\nbegin\r\n  while ( @i <= @samples ) \r\n  begin\r\n    select cpu_time,total_elapsed_time from sys.dm_exec_requests \r\n\t  where session_id = @spid\r\n\twaitfor delay '00:00:01'\r\n\tset @i = @i + 1\r\n  end\r\nend ;\r\n\r\n<span style=\"color:blue\">cpu_time<\/span>    total_elapsed_time\r\n<span style=\"color:blue\">56802<\/span>\t     60483\r\n<\/code><\/pre>\n<p>After starting the sql_batch_completed trace the execution time increased by more than 20%!<\/p>\n<pre><code>CREATE EVENT SESSION [batch] ON SERVER \r\nADD EVENT sqlserver.sql_batch_completed(SET collect_batch_text=(1)\r\n    WHERE ([sqlserver].[session_id]=(58)))\r\nWITH (MAX_MEMORY=4096 KB,EVENT_RETENTION_MODE=ALLOW_SINGLE_EVENT_LOSS,MAX_DISPATCH_LATENCY=30 SECONDS,MAX_EVENT_SIZE=0 KB,MEMORY_PARTITION_MODE=NONE,TRACK_CAUSALITY=OFF,STARTUP_STATE=OFF)\r\nGO<\/code><\/pre>\n<pre><code>\r\nspid        elapsed_seconds\r\n58          <span style=\"color:red\">74<\/span>\r\n<\/code><\/pre>\n<p>According to the Extended Event Trace the overhead wasn&#8217;t spent on the CPU:<\/p>\n<pre><code>\r\ncpu_time: <span style=\"color:blue\">63,781,000 us<\/span>\r\nduration: 74,653,151 us\r\n<\/code><\/pre>\n<p>In contrast, according to sys.dm_exec_requests the overhead was completely CPU bound:<\/p>\n<pre><code>\r\ncpu_time    total_elapsed_time\r\n<span style=\"color:blue\">73632<\/span>       74270\r\n<\/code><\/pre>\n<p>By observing the CPU time with the Task Manager we can confirm that the cpu_time information in sys.dm_exec_requests is correct.<\/p>\n<p>Luckily, not all events are detrimental to the performance. For example, the logout event doesn&#8217;t have any measurable impact, although it also collects the cpu_time:<\/p>\n<pre><code>spid       elapsed_seconds\r\n58         62<\/span><\/code><\/pre>\n<pre><code>CREATE EVENT SESSION [logout] ON SERVER \r\nADD EVENT sqlserver.logout(\r\n    WHERE ([package0].[equal_uint64]([sqlserver].[session_id],(58))))\r\nWITH (MAX_MEMORY=4096 KB,EVENT_RETENTION_MODE=ALLOW_SINGLE_EVENT_LOSS,MAX_DISPATCH_LATENCY=30 SECONDS,MAX_EVENT_SIZE=0 KB,MEMORY_PARTITION_MODE=NONE,TRACK_CAUSALITY=OFF,STARTUP_STATE=OFF)\r\nGO<\/code><\/pre>\n<h1>Oracle<\/h1>\n<p>I executed an equivalent test case on an Oracle 12.2 database on Solaris 11.3:<\/p>\n<pre><code>alter session set tracefile_identifier = 'LOOP' ;\r\nalter session set timed_statistics=true ;\r\nalter session set events '10046 trace name context forever, level 12' ;\r\n\r\nset timing on \r\n\r\ndeclare \r\n  i integer := 0 ;\r\nbegin\r\n  while i < 500000000\r\n  loop\r\n    i := i + 1 ;\r\n  end loop ;\r\nend ;\r\n\/\r\n\r\nALTER SESSION SET EVENTS '10046 trace name context off';<\/code><\/pre>\n<p>The tracing didn't introduce any measurable difference. The Oracle database CPU instrumentation is very efficient - it performs just two OS kernel calls to get the cumulative process CPU times before and after the SQL execution. Their difference is the time the process spent on the CPU. <\/p>\n<p>By the way, here are two articles about Oracle instrumentation internals:<\/p>\n<ul>\n<li><a href=\"http:\/\/nenadnoveljic.com\/blog\/database-call-cpu-instrumentation\/\">Database Call CPU Instrumentation<\/a><\/li>\n<li><a href=\"http:\/\/nenadnoveljic.com\/blog\/wait-events-dtrace\/\">Tracking Wait Events with DTrace<\/a><\/li>\n<\/ul>\n<h1>Conclusions<\/h1>\n<p>So, we can draw the following conclusions from the experiment:<\/p>\n<ul>\n<li>Some SQL Server Extended Events, like sql_batch_completed can cause a significant increase in the execution time.<\/li>\n<li>This overhead is CPU bound.<\/li>\n<li>The overhead cpu_time is not part of the cpu_time reported in the the Extended Events trace.<\/li>\n<li>The overhead cpu_time is contained in sys.dm_exec_requests.cpu_time.<\/li>\n<li>The Oracle database code instrumentation rocks.<\/li>\n<\/ul>\n<p>By using the methodology described above it makes sense to quantify the overhead of individual events before interpreting the data. Also, in some cases the trace data can be combined with sys.dm_exec_requests to get accurate results.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Measuring Extended Events Trace overhead and correctly interpreting the trace information. <a href=\"https:\/\/nenadnoveljic.com\/blog\/sql-server-extended-events-trace-overhead\/\" class=\"more-link\">Continue Reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[21,17],"tags":[],"class_list":["post-2178","post","type-post","status-publish","format-standard","hentry","category-comparison-oracle-sql-server","category-sql-server"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>SQL Server Extended Events Trace Overhead - All-round Database Topics<\/title>\n<meta name=\"description\" content=\"SQL Server Extended Events trace overhead. Pitfalls when interpreting data. Comparison with Oracle.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/nenadnoveljic.com\/blog\/sql-server-extended-events-trace-overhead\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"SQL Server Extended Events Trace Overhead - All-round Database Topics\" \/>\n<meta property=\"og:description\" content=\"SQL Server Extended Events trace overhead. Pitfalls when interpreting data. Comparison with Oracle.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/nenadnoveljic.com\/blog\/sql-server-extended-events-trace-overhead\/\" \/>\n<meta property=\"og:site_name\" content=\"All-round Database Topics\" \/>\n<meta property=\"article:published_time\" content=\"2018-09-04T17:49:39+00:00\" \/>\n<meta name=\"author\" content=\"Nenad Noveljic\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@NenadNoveljic\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Nenad Noveljic\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/sql-server-extended-events-trace-overhead\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/sql-server-extended-events-trace-overhead\\\/\"},\"author\":{\"name\":\"Nenad Noveljic\",\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/#\\\/schema\\\/person\\\/51458d9dd86dbbdd19f5add451d44efa\"},\"headline\":\"SQL Server Extended Events Trace Overhead\",\"datePublished\":\"2018-09-04T17:49:39+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/sql-server-extended-events-trace-overhead\\\/\"},\"wordCount\":357,\"commentCount\":0,\"articleSection\":[\"Comparison Oracle-SQL Server\",\"SQL Server\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/sql-server-extended-events-trace-overhead\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/sql-server-extended-events-trace-overhead\\\/\",\"url\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/sql-server-extended-events-trace-overhead\\\/\",\"name\":\"SQL Server Extended Events Trace Overhead - All-round Database Topics\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/#website\"},\"datePublished\":\"2018-09-04T17:49:39+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/#\\\/schema\\\/person\\\/51458d9dd86dbbdd19f5add451d44efa\"},\"description\":\"SQL Server Extended Events trace overhead. Pitfalls when interpreting data. Comparison with Oracle.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/sql-server-extended-events-trace-overhead\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/sql-server-extended-events-trace-overhead\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/sql-server-extended-events-trace-overhead\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"SQL Server Extended Events Trace Overhead\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/\",\"name\":\"All-round Database Topics\",\"description\":\"Nenad Noveljic\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/#\\\/schema\\\/person\\\/51458d9dd86dbbdd19f5add451d44efa\",\"name\":\"Nenad Noveljic\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/a97b796613ea48ec8a7b79c8ffe1c685dcffc920c68121f6238d5caab5070670?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/a97b796613ea48ec8a7b79c8ffe1c685dcffc920c68121f6238d5caab5070670?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/a97b796613ea48ec8a7b79c8ffe1c685dcffc920c68121f6238d5caab5070670?s=96&d=mm&r=g\",\"caption\":\"Nenad Noveljic\"},\"sameAs\":[\"nenad-noveljic-9b746a6\",\"https:\\\/\\\/x.com\\\/NenadNoveljic\"],\"url\":\"https:\\\/\\\/nenadnoveljic.com\\\/blog\\\/author\\\/nenad\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"SQL Server Extended Events Trace Overhead - All-round Database Topics","description":"SQL Server Extended Events trace overhead. Pitfalls when interpreting data. Comparison with Oracle.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/nenadnoveljic.com\/blog\/sql-server-extended-events-trace-overhead\/","og_locale":"en_US","og_type":"article","og_title":"SQL Server Extended Events Trace Overhead - All-round Database Topics","og_description":"SQL Server Extended Events trace overhead. Pitfalls when interpreting data. Comparison with Oracle.","og_url":"https:\/\/nenadnoveljic.com\/blog\/sql-server-extended-events-trace-overhead\/","og_site_name":"All-round Database Topics","article_published_time":"2018-09-04T17:49:39+00:00","author":"Nenad Noveljic","twitter_card":"summary_large_image","twitter_creator":"@NenadNoveljic","twitter_misc":{"Written by":"Nenad Noveljic","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/nenadnoveljic.com\/blog\/sql-server-extended-events-trace-overhead\/#article","isPartOf":{"@id":"https:\/\/nenadnoveljic.com\/blog\/sql-server-extended-events-trace-overhead\/"},"author":{"name":"Nenad Noveljic","@id":"https:\/\/nenadnoveljic.com\/blog\/#\/schema\/person\/51458d9dd86dbbdd19f5add451d44efa"},"headline":"SQL Server Extended Events Trace Overhead","datePublished":"2018-09-04T17:49:39+00:00","mainEntityOfPage":{"@id":"https:\/\/nenadnoveljic.com\/blog\/sql-server-extended-events-trace-overhead\/"},"wordCount":357,"commentCount":0,"articleSection":["Comparison Oracle-SQL Server","SQL Server"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/nenadnoveljic.com\/blog\/sql-server-extended-events-trace-overhead\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/nenadnoveljic.com\/blog\/sql-server-extended-events-trace-overhead\/","url":"https:\/\/nenadnoveljic.com\/blog\/sql-server-extended-events-trace-overhead\/","name":"SQL Server Extended Events Trace Overhead - All-round Database Topics","isPartOf":{"@id":"https:\/\/nenadnoveljic.com\/blog\/#website"},"datePublished":"2018-09-04T17:49:39+00:00","author":{"@id":"https:\/\/nenadnoveljic.com\/blog\/#\/schema\/person\/51458d9dd86dbbdd19f5add451d44efa"},"description":"SQL Server Extended Events trace overhead. Pitfalls when interpreting data. Comparison with Oracle.","breadcrumb":{"@id":"https:\/\/nenadnoveljic.com\/blog\/sql-server-extended-events-trace-overhead\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/nenadnoveljic.com\/blog\/sql-server-extended-events-trace-overhead\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/nenadnoveljic.com\/blog\/sql-server-extended-events-trace-overhead\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/nenadnoveljic.com\/blog\/"},{"@type":"ListItem","position":2,"name":"SQL Server Extended Events Trace Overhead"}]},{"@type":"WebSite","@id":"https:\/\/nenadnoveljic.com\/blog\/#website","url":"https:\/\/nenadnoveljic.com\/blog\/","name":"All-round Database Topics","description":"Nenad Noveljic","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/nenadnoveljic.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/nenadnoveljic.com\/blog\/#\/schema\/person\/51458d9dd86dbbdd19f5add451d44efa","name":"Nenad Noveljic","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/a97b796613ea48ec8a7b79c8ffe1c685dcffc920c68121f6238d5caab5070670?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/a97b796613ea48ec8a7b79c8ffe1c685dcffc920c68121f6238d5caab5070670?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/a97b796613ea48ec8a7b79c8ffe1c685dcffc920c68121f6238d5caab5070670?s=96&d=mm&r=g","caption":"Nenad Noveljic"},"sameAs":["nenad-noveljic-9b746a6","https:\/\/x.com\/NenadNoveljic"],"url":"https:\/\/nenadnoveljic.com\/blog\/author\/nenad\/"}]}},"_links":{"self":[{"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/posts\/2178","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/comments?post=2178"}],"version-history":[{"count":1,"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/posts\/2178\/revisions"}],"predecessor-version":[{"id":2194,"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/posts\/2178\/revisions\/2194"}],"wp:attachment":[{"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/media?parent=2178"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/categories?post=2178"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nenadnoveljic.com\/blog\/wp-json\/wp\/v2\/tags?post=2178"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}