{"id":328502,"date":"2022-01-24T15:00:36","date_gmt":"2022-01-24T15:00:36","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=328502"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=328502","title":{"rendered":"<span>\u041d\u043e\u0440\u043c\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f SQL profiler \u0442\u0440\u0435\u0439\u0441\u0430 \u0434\u043b\u044f \u0433\u0440\u0443\u043f\u043f\u0438\u0440\u043e\u0432\u043a\u0438<\/span>"},"content":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0415\u0441\u043b\u0438 \u0432\u044b \u0440\u0430\u0437\u0431\u0438\u0440\u0430\u0435\u0442\u0435\u0441\u044c &#171;\u043f\u043e\u0447\u0435\u043c\u0443 \u0442\u043e\u0440\u043c\u043e\u0437\u0438\u0442 \u0431\u0430\u0437\u0430&#187; \u0438 \u0443 \u0432\u0430\u0441 \u0435\u0441\u0442\u044c \u0442\u0440\u0435\u0439\u0441, \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u044b\u0439 <strong>MS SQL profiler,<\/strong> \u0442\u043e \u0447\u0442\u043e \u0432\u044b \u0434\u0435\u043b\u0430\u0435\u0442\u0435 \u043f\u0435\u0440\u0432\u044b\u043c \u0434\u0435\u043b\u043e\u043c? \u041f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e, \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u0442\u0435 \u0435\u0433\u043e \u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u0443, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u0440\u0430\u0437\u0431\u0438\u0440\u0430\u0442\u044c\u0441\u044f \u0441 \u043d\u0438\u043c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0440\u043e\u0434\u043d\u043e\u0433\u043e SQL, \u0430 \u043d\u0435 \u0432 GUI. <\/p>\n<p>\u041e\u0447\u0435\u043d\u044c \u0445\u043e\u0442\u0435\u043b\u043e\u0441\u044c \u0431\u044b \u0441\u0434\u0435\u043b\u0430\u0442\u044c <strong>group by TextData<\/strong>, \u043d\u043e \u0443\u0432\u044b &#8212; \u0442\u0430\u043a \u043d\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u0441\u044f \u0438\u0437-\u0437\u0430 \u0440\u0430\u0437\u043d\u044b\u0445 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432 \u0443 \u043f\u0440\u043e\u0446\u0435\u0434\u0443\u0440 \u0438 \u043a\u0432\u0435\u0440\u0435\u0439. \u0410 \u0432\u044b\u0440\u0430\u0437\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u0441\u043f\u043e\u0441\u043e\u0431\u043d\u043e\u0441\u0442\u0435\u0439 SQL \u043d\u0435 \u0445\u0432\u0430\u0442\u0435\u0442, \u0447\u0442\u043e\u0431\u044b \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e &#8216;\u043d\u043e\u0440\u043c\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c&#8217; \u0442\u0440\u0435\u0439\u0441. <\/p>\n<p>\u041d\u043e \u0432\u0435\u0434\u044c \u043c\u043e\u0436\u043d\u043e \u0441\u043a\u0440\u0435\u0441\u0442\u0438\u0442\u044c \u0435\u0436\u0430 \u0438 \u0443\u0436\u0430, <strong>SQL<\/strong> \u0438 <strong>Python<\/strong>, \u0438 \u0440\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0432 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0441\u0442\u0440\u043e\u043a! \u041f\u043e\u043b\u0435\u0437\u043d\u044b\u0435 \u0441\u043a\u0440\u0438\u043f\u0442\u044b \u043d\u0438\u0436\u0435<\/p>\n<h2>\u0418\u0442\u0430\u043a, \u043a\u0430\u043a\u0438\u0435 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043d\u0430\u043c \u0445\u043e\u0442\u0435\u043b\u043e\u0441\u044c \u0431\u044b \u0441\u0434\u0435\u043b\u0430\u0442\u044c?<\/h2>\n<p>\u041e\u0431\u0449\u0443\u044e \u043d\u043e\u0440\u043c\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e (\u043c\u043d\u043e\u0433\u043e \u043f\u0440\u043e\u0431\u0435\u043b\u043e\u0432 \u043f\u043e\u0434\u0440\u044f\u0434, \u0442\u0430\u0431\u0443\u043b\u044f\u0446\u0438\u0438 \u0438 \u043f\u0435\u0440\u0435\u0432\u043e\u0434\u044b \u0441\u0442\u0440\u043e\u043a).<\/p>\n<p>\u0414\u0430\u043b\u0435\u0435, \u0437\u0430\u043c\u0435\u043d\u0438\u043c \u0447\u0438\u0441\u043b\u0430 (\u043a\u0430\u043a \u0446\u0435\u043b\u044b\u0435, \u0442\u0430\u043a \u0438 hex) \u043d\u0430 #. \u0410 \u0432\u0441\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u0437\u0430\u043c\u0435\u043d\u0438\u043c \u043d\u0430 &#8216;~&#8217;, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code class=\"sql\">select 1,'alpha',0x478ddaaee,X,N'str' from TAB   -- \u043f\u0440\u0435\u0432\u0440\u0430\u0442\u0438\u0442\u0441\u044f \u0432  select #,'~',#,X,N'~' from TAB<\/code><\/pre>\n<p>\u042d\u0442\u043e \u043d\u0435 \u0440\u0435\u0448\u0430\u0435\u0442 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443 \u0441\u043f\u0438\u0441\u043a\u043e\u0432 \u0440\u0430\u0437\u043d\u043e\u0439 \u0434\u043b\u0438\u043d\u044b:<\/p>\n<pre><code class=\"sql\">select * from TAB where id in (1,2,3) select * from TAB where id in (5,6,7,8) -- \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u0441\u044f select * from TAB where id in (#,#,#) select * from TAB where id in (#,#,#,#)<\/code><\/pre>\n<p>\u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0437\u0430\u043c\u0435\u043d\u0438\u043c \u0441\u043f\u0438\u0441\u043a\u0438 \u0447\u0438\u0441\u0435\u043b \u043d\u0430 &lt;nlist>, \u0430 \u0441\u0442\u0440\u043e\u043a \u043d\u0430 &lt;slist>, \u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u0435\u0441\u043b\u0438 \u0434\u043e \u0441\u043f\u0438\u0441\u043a\u0430 \u0435\u0441\u0442\u044c \u0445\u043e\u0442\u044c \u043e\u0434\u043d\u0430 \u043e\u0442\u043a\u0440\u044b\u0432\u0430\u044e\u0449\u0430\u044f \u0441\u043a\u043e\u0431\u043a\u0430, \u0438\u043d\u0430\u0447\u0435 \u044d\u0442\u043e \u0441\u043f\u0438\u0441\u043e\u043a \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432 \u043f\u0440\u043e\u0446\u0435\u0434\u0443\u0440\u044b:<\/p>\n<pre><code class=\"sql\">exec prc 1,2,3,'alpha','beta' select * from TAB where id in (5,6,7,8) select * from STAB where names in ('alpha','beta') -- \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u0441\u044f exec prc #,#,#,'~','~' select * from TAB where id in (&lt;nlist>) select * from STAB where names in (&lt;slist>)<\/code><\/pre>\n<p>\u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e \u043c\u044b \u043d\u0435 \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u043c \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u043f\u0440\u043e\u0446\u0435\u0434\u0443\u0440\u044b \u043a \u0441\u043f\u0438\u0441\u043a\u0443.<\/p>\n<p>\u0414\u0430\u043b\u0435\u0435, \u0434\u043b\u044f <em>sp_executesql<\/em> \u0438 <em>sql_cursorprepexec<\/em> \u043c\u044b &#8216;\u0432\u044b\u043a\u0443\u0441\u044b\u0432\u0430\u0435\u043c&#8217; \u0441\u0430\u043c \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u0442\u043e\u043c \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043f\u043e \u043e\u0431\u0449\u0438\u043c \u043f\u0440\u0430\u0432\u0438\u043b\u0430\u043c:<\/p>\n<pre><code class=\"sql\">exec sp_executesql N'SELECT * FROM TAB where CAT=N''X'' and id=@p__linq__0'    ,N'@p__linq__0 int',@p__linq__0=725 -- \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u0441\u044f    SELECT * FROM TAB WHERE CAT=N'~' and id=@p__linq__0<\/code><\/pre>\n<p>\u041d\u0430\u043a\u043e\u043d\u0435\u0446, \u0447\u0430\u0441\u0442\u043e \u0432\u0441\u0442\u0440\u0435\u0447\u0430\u044e\u0442\u0441\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0441 \u0438\u043c\u0435\u043d\u0430\u043c\u0438, \u0441\u0433\u0435\u043d\u0435\u0440\u0435\u043d\u043d\u044b\u043c\u0438 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438:<\/p>\n<pre><code class=\"swift\">select * INTO [#sells_a5250e98_2ec7_495a_abe2_e314d0a9b5e6] from X drop table [#f42350e3_4aa3_1234_25e4_e314d0adf3e2] -- \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u0441\u044f select * INTO [#sells...] from X drop table [#G]<\/code><\/pre>\n<p>\u0412 \u043f\u0435\u0440\u0432\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0438\u043c\u044f \u0443\u0441\u0435\u043a\u0430\u0435\u0442\u0441\u044f \u043f\u043e \u043f\u0435\u0440\u0432\u043e\u043c\u0443 \u043f\u043e\u0434\u0447\u0435\u0440\u043a\u0438\u0432\u0430\u043d\u0438\u044e, \u0432\u043e \u0432\u0442\u043e\u0440\u043e\u043c, \u043a\u043e\u0433\u0434\u0430 \u0432\u0441\u0435 \u0438\u043c\u044f &#8212; GUID, \u043e\u0441\u0442\u0430\u0435\u0442\u0441\u044f #G<\/p>\n<h2>\u0410 \u0442\u0435\u043f\u0435\u0440\u044c \u0441\u0430\u043c\u0438 \u0441\u043a\u0440\u0438\u043f\u0442\u044b<\/h2>\n<p>\u0412\u043d\u0430\u0447\u0430\u043b\u0435 \u043f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u043a\u0430, \u0434\u043e\u043b\u0436\u043d\u043e \u0431\u044b\u0442\u044c \u0432\u043a\u043b\u044e\u0447\u0435\u043d\u043e:<\/p>\n<pre><code class=\"sql\">EXECUTE sp_configure 'external scripts enabled', 1; GO RECONFIGURE; GO<\/code><\/pre>\n<p>\u0414\u0430\u043b\u0435\u0435 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0442\u0430\u0431\u043b\u0438\u0447\u043a\u0443 \u0432 \u0431\u0430\u0437\u0435, \u0433\u0434\u0435 \u043c\u044b \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u043c \u0442\u0440\u0435\u0439\u0441\u044b. \u041d\u0430\u0437\u043e\u0432\u0435\u043c \u0435\u0435 T. \u0421\u043f\u0438\u0441\u043e\u043a \u043a\u043e\u043b\u043e\u043d\u043e\u043a \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u043e\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u043f\u043e \u0441\u0432\u043e\u0435\u043c\u0443 \u0432\u043a\u0443\u0441\u0443.<\/p>\n<pre><code class=\"sql\">create table T (rowNumber int, TextData varchar(max),    CPU int, Reads int, Writes int, Duration int)<\/code><\/pre>\n<p>\u0410 \u0442\u0435\u043f\u0435\u0440\u044c \u0433\u043b\u0430\u0432\u043d\u043e\u0435. \u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e \u0432 \u043a\u043e\u0434\u0435 Python \u0432\u0441\u0435 \u043e\u0434\u0438\u043d\u0430\u0440\u043d\u044b\u0435 \u043a\u0430\u0432\u044b\u0447\u043a\u0438 \u0443\u0434\u0432\u043e\u0435\u043d\u044b, \u0442\u0430\u043a \u043a\u0430\u043a \u0432\u044b \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u0442\u0435 \u044d\u0442\u043e \u0438\u0437 SQL:<\/p>\n<pre><code class=\"sql\">set nocount on  truncate table T insert into T EXECUTE sp_execute_external_script @language = N'Python'     , @script = N' import re  def norm(s):   # initial cleanup   s = s.replace(\"\\n\", \" \").replace(\"\\r\", \" \").replace(\"\\t\", \" \") # eols &amp; tabs   s = s.replace(\"  \", \" \").strip()   s = re.sub(\"\\s+\",\" \",s)+\" \" # get rid of multiple spaces, last space for parsing numbers at the end    # open sql_executesql   if (s[0:19].casefold() == \"exec sp_executesql \"):      s = s[21:]     s = re.sub(\"(.*?[^''])''.*\\Z\",\"\\\\1\",s) # leftmost quote which is not double quote    # sp_cursorprepexec   prep = re.compile(\".*\\Wexec sp_cursorprepexec\\W\", re.IGNORECASE)   if prep.match(s):     s = s.rsplit(\",N''\",1)[1] # extract statement after N     s = re.sub(\"(.*?[^''])''.*\\Z\",\"\\\\1\",s) # leftmost quote which is not double quote     s = \"sp_cursorprepexec \" + s.replace(\"''''\",\"''\") + \" \"    s = re.sub(\"''''\", \"\",s) # replace double single quotes with nothing   s = re.sub(\"(\\\\W)#(\\w{36,36})(\\\\W)\", \"\\\\1#G\\\\3\", s) # replace #&lt;guid name> with #G   s = re.sub(\"(\\\\W)(#[^\\W_]+)(\\w+)(\\\\W)\", \"\\\\1\\\\2...\\\\4\", s) # replace unique temp table names #tab_12345 with #tab...      # replace any number with # loop because , is eaten by last \\W so some values are skipped   while True:     new = re.sub(\"(\\\\W)(\\\\d+)(\\\\W)\", \"\\\\1#\\\\3\", s)      if new == s: break     s = new   s = re.sub(\"-#\", \"#\", s) # negative numbers    # hex numbers   while True:     new = re.sub(\"(\\\\W)(0x[0-9A-Fa-f]+)(\\\\W)\", \"\\\\1#\\\\3\", s)      if new == s: break     s = new    s = re.sub(\"(\\'')(.*?)(\\'')\", \"''~''\", s) # replace any string with ''~''    # remove spaces around , so lists can be always collapsed   s = re.sub(\"\\s,\", \",\", s)   s = re.sub(\",\\s\", \",\", s)    # collapse numeric lists, there must be at least one ( before the list, otherwise these are parameters   while True:     new = re.sub(\"(\\(.*?)#,#\", \"\\\\1&lt;nlist>\", s)     if new == s: break     s = new   while True:     new = re.sub(\"&lt;nlist>,&lt;nlist>\", \"&lt;nlist>\", s)     if new == s: break     s = new   s = re.sub(\"&lt;nlist>,#\", \"&lt;nlist>\", s)    # collapse string lists   while True:     new = re.sub(\"(\\(.*?)''~'',''~''\", \"\\\\1&lt;slist>\", s)     if new == s: break     s = new   while True:     new = re.sub(\"&lt;slist>,&lt;slist>\", \"&lt;slist>\", s)     if new == s: break     s = new   s = re.sub(\"&lt;slist>,''~''\", \"&lt;slist>\", s)    return s  OutputDataSet = InputDataSet n = 0 for r in OutputDataSet[\"RowNumber\"]:   s = OutputDataSet[\"TextData\"][n]   OutputDataSet[\"TextData\"][n] = norm(s)   n += 1 ',  @input_data_1 = N'SELECT RowNumber,convert(nvarchar(max),TextData) as TextData,    CPU,Reads,Writes,Duration\/1000 as Duration FROM trc where textData is not null' <\/code><\/pre>\n<p>\u041e\u0441\u043e\u0431\u043e\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0435 \u0434\u0432\u0435 \u0441\u0442\u0440\u043e\u0447\u043a\u0438: \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u0435 <strong>trc<\/strong> \u043d\u0430 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435 \u0432\u0430\u0448\u0435\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0441 \u0442\u0440\u0435\u0439\u0441\u043e\u043c, \u0438 \u043d\u0435 \u0437\u0430\u0431\u0443\u0434\u044c\u0442\u0435 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0441\u044e\u0434\u0430 \u0435\u0449\u0435 \u043a\u043e\u043b\u043e\u043d\u043a\u0438, \u0435\u0441\u043b\u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u043b\u0438 \u0438\u0437 \u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u0443 <strong>T<\/strong>. Duration \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442\u0441\u044f \u043a \u043c\u0438\u043b\u043b\u0438\u0441\u0435\u043a\u0443\u043d\u0434\u0430\u043c \u0438\u0437 \u043c\u0438\u043a\u0440\u043e\u0441\u0435\u043a\u0443\u043d\u0434.<\/p>\n<p>\u0421\u0430\u043c\u0430 \u0433\u0440\u0443\u043f\u043f\u0438\u0440\u043e\u0432\u043a\u0430 \u0442\u0440\u0438\u0432\u0438\u0430\u043b\u044c\u043d\u0430:<\/p>\n<pre><code class=\"sql\">declare @totalCPU float, @totalReads float, @totalWrites float, @totalDuration float  select @totalCPU=sum(CPU), @totalReads=sum(Reads), @totalWrites=sum(Writes), @totalDuration=sum(Duration) from T  select count(*) as cnt,    sum(CPU) as CPU,sum(Reads) as Reads, sum(Writes) as Writes, sum(Duration) as Duration,     sum(CPU)\/(@totalCPU+0.001) as PctCPU, sum(Reads)\/(@totalReads+0.001) as PctReads,    sum(Writes)\/(@totalWrites+0.001) as PctWrites,sum(Duration)\/(@totalDuration+0.001) as PctDuration,     TextData from T     group by TextData order by 1 desc<\/code><\/pre>\n<p>\u0412\u044b\u0432\u043e\u0434\u0438\u043c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u043e\u0432 \u0438 \u0441\u0443\u043c\u043c\u044b <em>CPU, Reads, Writes, Duration<\/em> \u043a\u0430\u043a \u0435\u0441\u0442\u044c \u0438 \u0432 \u043f\u0440\u043e\u0446\u0435\u043d\u0442\u0430\u0445 \u043a \u043f\u043e\u043b\u043d\u043e\u043c\u0443 \u0442\u0440\u0435\u0439\u0441\u0443. \u0412 \u043a\u043e\u043d\u0446\u0435 <em>ORDER BY<\/em> \u043f\u043e \u0432\u043a\u0443\u0441\u0443.<\/p>\n<p>\u0421\u043a\u043e\u0440\u043e\u0441\u0442\u044c \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0441\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u043e\u043a\u043e\u043b\u043e 120 \u0441\u0442\u0440\u043e\u043a \u0432 \u0441\u0435\u043a\u0443\u043d\u0434\u0443, \u0442\u0430\u043a \u0447\u0442\u043e \u043d\u0430 \u0442\u0440\u0435\u0439\u0441 \u0438\u0437 \u043c\u0438\u043b\u043b\u0438\u043e\u043d\u0430 \u0437\u0430\u043f\u0438\u0441\u0435\u0439 \u0443 \u0432\u0430\u0441 \u0443\u0439\u0434\u0435\u0442 \u043e\u043a\u043e\u043b\u043e \u0442\u0440\u0435\u0445 \u0447\u0430\u0441\u043e\u0432. \u041d\u0430\u0432\u0435\u0440\u043d\u043e\u0435, \u044d\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0443\u0441\u043a\u043e\u0440\u0438\u0442\u044c &#8212; \u043d\u043e \u0437\u0430\u0447\u0435\u043c? \u041f\u043e\u043a\u0430 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0442\u0440\u0435\u0439\u0441, \u0432\u044b \u0441\u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u043e\u0433\u0443\u043b\u044f\u0442\u044c \u0438\u043b\u0438 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0444\u0438\u043b\u044c\u043c \u043d\u0435 \u043a\u043e\u0440\u044f \u0441\u0435\u0431\u044f \u0442\u0435\u043c, \u0447\u0442\u043e \u0432\u044b \u044f\u043a\u043e\u0431\u044b \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0434\u0435\u043b\u0430\u0435\u0442\u0435. <\/p>\n<h2>\u0418 \u0435\u0449\u0435 \u043f\u043e\u043b\u0435\u0437\u043d\u043e\u0441\u0442\u044c<\/h2>\n<p>\u041c\u043d\u0435 \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u043b\u043e\u0441\u044c \u0438\u0441\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u044c \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u044c \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043f\u0440\u043e\u0446\u0435\u0434\u0443\u0440\u044b \u043e\u0442 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432 \u0442\u0440\u0435\u0439\u0441\u0435 \u0438\u0434\u0443\u0442 \u0437\u0430\u043f\u0438\u0441\u0438:<\/p>\n<pre><code class=\"sql\">exec MyReport @from='2022-01-01',@to='2022-01-20',@flag='all' exec MyReport @from='2020-01-01',@to='2022-01-20',@flag='some' exec MyReport @from='2022-01-20',@to='2022-01-20',@flag='all'<\/code><\/pre>\n<p>\u041d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0432\u044b\u0437\u043e\u0432\u044b \u0431\u044b\u043b\u0438 \u043a\u0443\u0434\u0430 \u043c\u0435\u0434\u043b\u0435\u043d\u043d\u0435\u0435 \u0447\u0435\u043c \u043e\u0431\u044b\u0447\u043d\u043e, \u0438 \u044f \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u043b, \u0447\u0442\u043e \u0434\u0435\u043b\u043e \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 \u0432\u0440\u0435\u043c\u0435\u043d, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442 \u043e\u0442\u0447\u0435\u0442. \u041f\u043e\u043c\u043e\u0436\u0435\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u044f:<\/p>\n<pre><code class=\"sql\">create function [dbo].[getpar] (@par varchar(32), @t varchar(max)) returns varchar(8000) as   begin   declare @i int   set @i = charindex('@'+@par,@t)   if @i &lt; 0 return ''   set @t=substring(@t,@i+len(@par)+2,8000)   if left(@t,1)='N' set @t=substring(@t,2,8000)   if left(@t,1)='''' set @t=substring(@t,2,8000)   set @i = charindex(',@',@t)   if @i > 0 set @t=substring(@t,1,@i-1)   if left(reverse(@t),1)='''' set @t=substring(@t,1,len(@t)-1)   return @t   end<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u0440\u043e\u0430\u043d\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432\u044b\u0437\u043e\u0432\u044b \u0442\u0430\u043a:<\/p>\n<pre><code class=\"sql\">select 1+datediff(dd,          convert(datetime,dbo.getpar('from',TextData)),          convert(datetime,dbo.getpar('to',TextData))) as days   , CPU,Reads,Writes,Duration\/1000 as Duration   from MyTrace where TextData like 'exec%MyReport%'<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c \u0433\u0438\u043f\u043e\u0442\u0435\u0437\u0443, \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u0432  Excel X-Y scatter diagram (X-days, Y-Duration \u0438\u043b\u0438 CPU)<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"v-portal\" style=\"display:none;\"><\/div>\n<\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/647449\/\"> https:\/\/habr.com\/ru\/post\/647449\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0415\u0441\u043b\u0438 \u0432\u044b \u0440\u0430\u0437\u0431\u0438\u0440\u0430\u0435\u0442\u0435\u0441\u044c &#171;\u043f\u043e\u0447\u0435\u043c\u0443 \u0442\u043e\u0440\u043c\u043e\u0437\u0438\u0442 \u0431\u0430\u0437\u0430&#187; \u0438 \u0443 \u0432\u0430\u0441 \u0435\u0441\u0442\u044c \u0442\u0440\u0435\u0439\u0441, \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u044b\u0439 <strong>MS SQL profiler,<\/strong> \u0442\u043e \u0447\u0442\u043e \u0432\u044b \u0434\u0435\u043b\u0430\u0435\u0442\u0435 \u043f\u0435\u0440\u0432\u044b\u043c \u0434\u0435\u043b\u043e\u043c? \u041f\u0440\u0430\u0432\u0438\u043b\u044c\u043d\u043e, \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u0442\u0435 \u0435\u0433\u043e \u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u0443, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u0440\u0430\u0437\u0431\u0438\u0440\u0430\u0442\u044c\u0441\u044f \u0441 \u043d\u0438\u043c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0440\u043e\u0434\u043d\u043e\u0433\u043e SQL, \u0430 \u043d\u0435 \u0432 GUI. <\/p>\n<p>\u041e\u0447\u0435\u043d\u044c \u0445\u043e\u0442\u0435\u043b\u043e\u0441\u044c \u0431\u044b \u0441\u0434\u0435\u043b\u0430\u0442\u044c <strong>group by TextData<\/strong>, \u043d\u043e \u0443\u0432\u044b &#8212; \u0442\u0430\u043a \u043d\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u0441\u044f \u0438\u0437-\u0437\u0430 \u0440\u0430\u0437\u043d\u044b\u0445 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432 \u0443 \u043f\u0440\u043e\u0446\u0435\u0434\u0443\u0440 \u0438 \u043a\u0432\u0435\u0440\u0435\u0439. \u0410 \u0432\u044b\u0440\u0430\u0437\u0438\u0442\u0435\u043b\u044c\u043d\u044b\u0445 \u0441\u043f\u043e\u0441\u043e\u0431\u043d\u043e\u0441\u0442\u0435\u0439 SQL \u043d\u0435 \u0445\u0432\u0430\u0442\u0435\u0442, \u0447\u0442\u043e\u0431\u044b \u044d\u0444\u0444\u0435\u043a\u0442\u0438\u0432\u043d\u043e &#8216;\u043d\u043e\u0440\u043c\u0430\u043b\u0438\u0437\u043e\u0432\u0430\u0442\u044c&#8217; \u0442\u0440\u0435\u0439\u0441. <\/p>\n<p>\u041d\u043e \u0432\u0435\u0434\u044c \u043c\u043e\u0436\u043d\u043e \u0441\u043a\u0440\u0435\u0441\u0442\u0438\u0442\u044c \u0435\u0436\u0430 \u0438 \u0443\u0436\u0430, <strong>SQL<\/strong> \u0438 <strong>Python<\/strong>, \u0438 \u0440\u0435\u0448\u0438\u0442\u044c \u0437\u0430\u0434\u0430\u0447\u0443 \u0432 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0441\u0442\u0440\u043e\u043a! \u041f\u043e\u043b\u0435\u0437\u043d\u044b\u0435 \u0441\u043a\u0440\u0438\u043f\u0442\u044b \u043d\u0438\u0436\u0435<\/p>\n<h2>\u0418\u0442\u0430\u043a, \u043a\u0430\u043a\u0438\u0435 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u043d\u0430\u043c \u0445\u043e\u0442\u0435\u043b\u043e\u0441\u044c \u0431\u044b \u0441\u0434\u0435\u043b\u0430\u0442\u044c?<\/h2>\n<p>\u041e\u0431\u0449\u0443\u044e \u043d\u043e\u0440\u043c\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044e (\u043c\u043d\u043e\u0433\u043e \u043f\u0440\u043e\u0431\u0435\u043b\u043e\u0432 \u043f\u043e\u0434\u0440\u044f\u0434, \u0442\u0430\u0431\u0443\u043b\u044f\u0446\u0438\u0438 \u0438 \u043f\u0435\u0440\u0435\u0432\u043e\u0434\u044b \u0441\u0442\u0440\u043e\u043a).<\/p>\n<p>\u0414\u0430\u043b\u0435\u0435, \u0437\u0430\u043c\u0435\u043d\u0438\u043c \u0447\u0438\u0441\u043b\u0430 (\u043a\u0430\u043a \u0446\u0435\u043b\u044b\u0435, \u0442\u0430\u043a \u0438 hex) \u043d\u0430 #. \u0410 \u0432\u0441\u0435 \u0441\u0442\u0440\u043e\u043a\u0438 \u0437\u0430\u043c\u0435\u043d\u0438\u043c \u043d\u0430 &#8216;~&#8217;, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440:<\/p>\n<pre><code class=\"sql\">select 1,'alpha',0x478ddaaee,X,N'str' from TAB   -- \u043f\u0440\u0435\u0432\u0440\u0430\u0442\u0438\u0442\u0441\u044f \u0432  select #,'~',#,X,N'~' from TAB<\/code><\/pre>\n<p>\u042d\u0442\u043e \u043d\u0435 \u0440\u0435\u0448\u0430\u0435\u0442 \u043f\u0440\u043e\u0431\u043b\u0435\u043c\u0443 \u0441\u043f\u0438\u0441\u043a\u043e\u0432 \u0440\u0430\u0437\u043d\u043e\u0439 \u0434\u043b\u0438\u043d\u044b:<\/p>\n<pre><code class=\"sql\">select * from TAB where id in (1,2,3) select * from TAB where id in (5,6,7,8) -- \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u0441\u044f select * from TAB where id in (#,#,#) select * from TAB where id in (#,#,#,#)<\/code><\/pre>\n<p>\u041f\u043e\u044d\u0442\u043e\u043c\u0443 \u0437\u0430\u043c\u0435\u043d\u0438\u043c \u0441\u043f\u0438\u0441\u043a\u0438 \u0447\u0438\u0441\u0435\u043b \u043d\u0430 &lt;nlist>, \u0430 \u0441\u0442\u0440\u043e\u043a \u043d\u0430 &lt;slist>, \u043d\u043e \u0442\u043e\u043b\u044c\u043a\u043e \u0435\u0441\u043b\u0438 \u0434\u043e \u0441\u043f\u0438\u0441\u043a\u0430 \u0435\u0441\u0442\u044c \u0445\u043e\u0442\u044c \u043e\u0434\u043d\u0430 \u043e\u0442\u043a\u0440\u044b\u0432\u0430\u044e\u0449\u0430\u044f \u0441\u043a\u043e\u0431\u043a\u0430, \u0438\u043d\u0430\u0447\u0435 \u044d\u0442\u043e \u0441\u043f\u0438\u0441\u043e\u043a \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432 \u043f\u0440\u043e\u0446\u0435\u0434\u0443\u0440\u044b:<\/p>\n<pre><code class=\"sql\">exec prc 1,2,3,'alpha','beta' select * from TAB where id in (5,6,7,8) select * from STAB where names in ('alpha','beta') -- \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u0441\u044f exec prc #,#,#,'~','~' select * from TAB where id in (&lt;nlist>) select * from STAB where names in (&lt;slist>)<\/code><\/pre>\n<p>\u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e \u043c\u044b \u043d\u0435 \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u043c \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u044b \u043f\u0440\u043e\u0446\u0435\u0434\u0443\u0440\u044b \u043a \u0441\u043f\u0438\u0441\u043a\u0443.<\/p>\n<p>\u0414\u0430\u043b\u0435\u0435, \u0434\u043b\u044f <em>sp_executesql<\/em> \u0438 <em>sql_cursorprepexec<\/em> \u043c\u044b &#8216;\u0432\u044b\u043a\u0443\u0441\u044b\u0432\u0430\u0435\u043c&#8217; \u0441\u0430\u043c \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u0442\u043e\u043c \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043f\u043e \u043e\u0431\u0449\u0438\u043c \u043f\u0440\u0430\u0432\u0438\u043b\u0430\u043c:<\/p>\n<pre><code class=\"sql\">exec sp_executesql N'SELECT * FROM TAB where CAT=N''X'' and id=@p__linq__0'    ,N'@p__linq__0 int',@p__linq__0=725 -- \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u0441\u044f    SELECT * FROM TAB WHERE CAT=N'~' and id=@p__linq__0<\/code><\/pre>\n<p>\u041d\u0430\u043a\u043e\u043d\u0435\u0446, \u0447\u0430\u0441\u0442\u043e \u0432\u0441\u0442\u0440\u0435\u0447\u0430\u044e\u0442\u0441\u044f \u0432\u0440\u0435\u043c\u0435\u043d\u043d\u044b\u0435 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0441 \u0438\u043c\u0435\u043d\u0430\u043c\u0438, \u0441\u0433\u0435\u043d\u0435\u0440\u0435\u043d\u043d\u044b\u043c\u0438 \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0447\u0435\u0441\u043a\u0438:<\/p>\n<pre><code class=\"swift\">select * INTO [#sells_a5250e98_2ec7_495a_abe2_e314d0a9b5e6] from X drop table [#f42350e3_4aa3_1234_25e4_e314d0adf3e2] -- \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u0441\u044f select * INTO [#sells...] from X drop table [#G]<\/code><\/pre>\n<p>\u0412 \u043f\u0435\u0440\u0432\u043e\u043c \u0441\u043b\u0443\u0447\u0430\u0435 \u0438\u043c\u044f \u0443\u0441\u0435\u043a\u0430\u0435\u0442\u0441\u044f \u043f\u043e \u043f\u0435\u0440\u0432\u043e\u043c\u0443 \u043f\u043e\u0434\u0447\u0435\u0440\u043a\u0438\u0432\u0430\u043d\u0438\u044e, \u0432\u043e \u0432\u0442\u043e\u0440\u043e\u043c, \u043a\u043e\u0433\u0434\u0430 \u0432\u0441\u0435 \u0438\u043c\u044f &#8212; GUID, \u043e\u0441\u0442\u0430\u0435\u0442\u0441\u044f #G<\/p>\n<h2>\u0410 \u0442\u0435\u043f\u0435\u0440\u044c \u0441\u0430\u043c\u0438 \u0441\u043a\u0440\u0438\u043f\u0442\u044b<\/h2>\n<p>\u0412\u043d\u0430\u0447\u0430\u043b\u0435 \u043f\u043e\u0434\u0433\u043e\u0442\u043e\u0432\u043a\u0430, \u0434\u043e\u043b\u0436\u043d\u043e \u0431\u044b\u0442\u044c \u0432\u043a\u043b\u044e\u0447\u0435\u043d\u043e:<\/p>\n<pre><code class=\"sql\">EXECUTE sp_configure 'external scripts enabled', 1; GO RECONFIGURE; GO<\/code><\/pre>\n<p>\u0414\u0430\u043b\u0435\u0435 \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u0442\u0430\u0431\u043b\u0438\u0447\u043a\u0443 \u0432 \u0431\u0430\u0437\u0435, \u0433\u0434\u0435 \u043c\u044b \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u0435\u043c \u0442\u0440\u0435\u0439\u0441\u044b. \u041d\u0430\u0437\u043e\u0432\u0435\u043c \u0435\u0435 T. \u0421\u043f\u0438\u0441\u043e\u043a \u043a\u043e\u043b\u043e\u043d\u043e\u043a \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u043e\u043f\u043e\u043b\u043d\u044f\u0442\u044c \u043f\u043e \u0441\u0432\u043e\u0435\u043c\u0443 \u0432\u043a\u0443\u0441\u0443.<\/p>\n<pre><code class=\"sql\">create table T (rowNumber int, TextData varchar(max),    CPU int, Reads int, Writes int, Duration int)<\/code><\/pre>\n<p>\u0410 \u0442\u0435\u043f\u0435\u0440\u044c \u0433\u043b\u0430\u0432\u043d\u043e\u0435. \u041e\u0431\u0440\u0430\u0442\u0438\u0442\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435, \u0447\u0442\u043e \u0432 \u043a\u043e\u0434\u0435 Python \u0432\u0441\u0435 \u043e\u0434\u0438\u043d\u0430\u0440\u043d\u044b\u0435 \u043a\u0430\u0432\u044b\u0447\u043a\u0438 \u0443\u0434\u0432\u043e\u0435\u043d\u044b, \u0442\u0430\u043a \u043a\u0430\u043a \u0432\u044b \u043f\u0435\u0440\u0435\u0434\u0430\u0435\u0442\u0435 \u044d\u0442\u043e \u0438\u0437 SQL:<\/p>\n<pre><code class=\"sql\">set nocount on  truncate table T insert into T EXECUTE sp_execute_external_script @language = N'Python'     , @script = N' import re  def norm(s):   # initial cleanup   s = s.replace(\"\\n\", \" \").replace(\"\\r\", \" \").replace(\"\\t\", \" \") # eols &amp; tabs   s = s.replace(\"  \", \" \").strip()   s = re.sub(\"\\s+\",\" \",s)+\" \" # get rid of multiple spaces, last space for parsing numbers at the end    # open sql_executesql   if (s[0:19].casefold() == \"exec sp_executesql \"):      s = s[21:]     s = re.sub(\"(.*?[^''])''.*\\Z\",\"\\\\1\",s) # leftmost quote which is not double quote    # sp_cursorprepexec   prep = re.compile(\".*\\Wexec sp_cursorprepexec\\W\", re.IGNORECASE)   if prep.match(s):     s = s.rsplit(\",N''\",1)[1] # extract statement after N     s = re.sub(\"(.*?[^''])''.*\\Z\",\"\\\\1\",s) # leftmost quote which is not double quote     s = \"sp_cursorprepexec \" + s.replace(\"''''\",\"''\") + \" \"    s = re.sub(\"''''\", \"\",s) # replace double single quotes with nothing   s = re.sub(\"(\\\\W)#(\\w{36,36})(\\\\W)\", \"\\\\1#G\\\\3\", s) # replace #&lt;guid name> with #G   s = re.sub(\"(\\\\W)(#[^\\W_]+)(\\w+)(\\\\W)\", \"\\\\1\\\\2...\\\\4\", s) # replace unique temp table names #tab_12345 with #tab...      # replace any number with # loop because , is eaten by last \\W so some values are skipped   while True:     new = re.sub(\"(\\\\W)(\\\\d+)(\\\\W)\", \"\\\\1#\\\\3\", s)      if new == s: break     s = new   s = re.sub(\"-#\", \"#\", s) # negative numbers    # hex numbers   while True:     new = re.sub(\"(\\\\W)(0x[0-9A-Fa-f]+)(\\\\W)\", \"\\\\1#\\\\3\", s)      if new == s: break     s = new    s = re.sub(\"(\\'')(.*?)(\\'')\", \"''~''\", s) # replace any string with ''~''    # remove spaces around , so lists can be always collapsed   s = re.sub(\"\\s,\", \",\", s)   s = re.sub(\",\\s\", \",\", s)    # collapse numeric lists, there must be at least one ( before the list, otherwise these are parameters   while True:     new = re.sub(\"(\\(.*?)#,#\", \"\\\\1&lt;nlist>\", s)     if new == s: break     s = new   while True:     new = re.sub(\"&lt;nlist>,&lt;nlist>\", \"&lt;nlist>\", s)     if new == s: break     s = new   s = re.sub(\"&lt;nlist>,#\", \"&lt;nlist>\", s)    # collapse string lists   while True:     new = re.sub(\"(\\(.*?)''~'',''~''\", \"\\\\1&lt;slist>\", s)     if new == s: break     s = new   while True:     new = re.sub(\"&lt;slist>,&lt;slist>\", \"&lt;slist>\", s)     if new == s: break     s = new   s = re.sub(\"&lt;slist>,''~''\", \"&lt;slist>\", s)    return s  OutputDataSet = InputDataSet n = 0 for r in OutputDataSet[\"RowNumber\"]:   s = OutputDataSet[\"TextData\"][n]   OutputDataSet[\"TextData\"][n] = norm(s)   n += 1 ',  @input_data_1 = N'SELECT RowNumber,convert(nvarchar(max),TextData) as TextData,    CPU,Reads,Writes,Duration\/1000 as Duration FROM trc where textData is not null' <\/code><\/pre>\n<p>\u041e\u0441\u043e\u0431\u043e\u0435 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435 \u043d\u0430 \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0435 \u0434\u0432\u0435 \u0441\u0442\u0440\u043e\u0447\u043a\u0438: \u0437\u0430\u043c\u0435\u043d\u0438\u0442\u0435 <strong>trc<\/strong> \u043d\u0430 \u043d\u0430\u0437\u0432\u0430\u043d\u0438\u0435 \u0432\u0430\u0448\u0435\u0439 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u0441 \u0442\u0440\u0435\u0439\u0441\u043e\u043c, \u0438 \u043d\u0435 \u0437\u0430\u0431\u0443\u0434\u044c\u0442\u0435 \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0441\u044e\u0434\u0430 \u0435\u0449\u0435 \u043a\u043e\u043b\u043e\u043d\u043a\u0438, \u0435\u0441\u043b\u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u043b\u0438 \u0438\u0437 \u0432 \u0442\u0430\u0431\u043b\u0438\u0446\u0443 <strong>T<\/strong>. Duration \u043f\u0440\u0438\u0432\u043e\u0434\u0438\u0442\u0441\u044f \u043a \u043c\u0438\u043b\u043b\u0438\u0441\u0435\u043a\u0443\u043d\u0434\u0430\u043c \u0438\u0437 \u043c\u0438\u043a\u0440\u043e\u0441\u0435\u043a\u0443\u043d\u0434.<\/p>\n<p>\u0421\u0430\u043c\u0430 \u0433\u0440\u0443\u043f\u043f\u0438\u0440\u043e\u0432\u043a\u0430 \u0442\u0440\u0438\u0432\u0438\u0430\u043b\u044c\u043d\u0430:<\/p>\n<pre><code class=\"sql\">declare @totalCPU float, @totalReads float, @totalWrites float, @totalDuration float  select @totalCPU=sum(CPU), @totalReads=sum(Reads), @totalWrites=sum(Writes), @totalDuration=sum(Duration) from T  select count(*) as cnt,    sum(CPU) as CPU,sum(Reads) as Reads, sum(Writes) as Writes, sum(Duration) as Duration,     sum(CPU)\/(@totalCPU+0.001) as PctCPU, sum(Reads)\/(@totalReads+0.001) as PctReads,    sum(Writes)\/(@totalWrites+0.001) as PctWrites,sum(Duration)\/(@totalDuration+0.001) as PctDuration,     TextData from T     group by TextData order by 1 desc<\/code><\/pre>\n<p>\u0412\u044b\u0432\u043e\u0434\u0438\u043c \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440\u043e\u0432 \u0438 \u0441\u0443\u043c\u043c\u044b <em>CPU, Reads, Writes, Duration<\/em> \u043a\u0430\u043a \u0435\u0441\u0442\u044c \u0438 \u0432 \u043f\u0440\u043e\u0446\u0435\u043d\u0442\u0430\u0445 \u043a \u043f\u043e\u043b\u043d\u043e\u043c\u0443 \u0442\u0440\u0435\u0439\u0441\u0443. \u0412 \u043a\u043e\u043d\u0446\u0435 <em>ORDER BY<\/em> \u043f\u043e \u0432\u043a\u0443\u0441\u0443.<\/p>\n<p>\u0421\u043a\u043e\u0440\u043e\u0441\u0442\u044c \u043e\u0431\u0440\u0430\u0431\u043e\u0442\u043a\u0438 \u0441\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u043e\u043a\u043e\u043b\u043e 120 \u0441\u0442\u0440\u043e\u043a \u0432 \u0441\u0435\u043a\u0443\u043d\u0434\u0443, \u0442\u0430\u043a \u0447\u0442\u043e \u043d\u0430 \u0442\u0440\u0435\u0439\u0441 \u0438\u0437 \u043c\u0438\u043b\u043b\u0438\u043e\u043d\u0430 \u0437\u0430\u043f\u0438\u0441\u0435\u0439 \u0443 \u0432\u0430\u0441 \u0443\u0439\u0434\u0435\u0442 \u043e\u043a\u043e\u043b\u043e \u0442\u0440\u0435\u0445 \u0447\u0430\u0441\u043e\u0432. \u041d\u0430\u0432\u0435\u0440\u043d\u043e\u0435, \u044d\u0442\u043e \u043c\u043e\u0436\u043d\u043e \u0443\u0441\u043a\u043e\u0440\u0438\u0442\u044c &#8212; \u043d\u043e \u0437\u0430\u0447\u0435\u043c? \u041f\u043e\u043a\u0430 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u0442\u0440\u0435\u0439\u0441, \u0432\u044b \u0441\u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u043e\u0433\u0443\u043b\u044f\u0442\u044c \u0438\u043b\u0438 \u043f\u043e\u0441\u043c\u043e\u0442\u0440\u0435\u0442\u044c \u0444\u0438\u043b\u044c\u043c \u043d\u0435 \u043a\u043e\u0440\u044f \u0441\u0435\u0431\u044f \u0442\u0435\u043c, \u0447\u0442\u043e \u0432\u044b \u044f\u043a\u043e\u0431\u044b \u043d\u0438\u0447\u0435\u0433\u043e \u043d\u0435 \u0434\u0435\u043b\u0430\u0435\u0442\u0435. <\/p>\n<h2>\u0418 \u0435\u0449\u0435 \u043f\u043e\u043b\u0435\u0437\u043d\u043e\u0441\u0442\u044c<\/h2>\n<p>\u041c\u043d\u0435 \u043f\u0440\u0438\u0445\u043e\u0434\u0438\u043b\u043e\u0441\u044c \u0438\u0441\u0441\u043b\u0435\u0434\u043e\u0432\u0430\u0442\u044c \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u044c \u0441\u043a\u043e\u0440\u043e\u0441\u0442\u0438 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043f\u0440\u043e\u0446\u0435\u0434\u0443\u0440\u044b \u043e\u0442 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u043e\u0432. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0432 \u0442\u0440\u0435\u0439\u0441\u0435 \u0438\u0434\u0443\u0442 \u0437\u0430\u043f\u0438\u0441\u0438:<\/p>\n<pre><code class=\"sql\">exec MyReport @from='2022-01-01',@to='2022-01-20',@flag='all' exec MyReport @from='2020-01-01',@to='2022-01-20',@flag='some' exec MyReport @from='2022-01-20',@to='2022-01-20',@flag='all'<\/code><\/pre>\n<p>\u041d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0432\u044b\u0437\u043e\u0432\u044b \u0431\u044b\u043b\u0438 \u043a\u0443\u0434\u0430 \u043c\u0435\u0434\u043b\u0435\u043d\u043d\u0435\u0435 \u0447\u0435\u043c \u043e\u0431\u044b\u0447\u043d\u043e, \u0438 \u044f \u043f\u0440\u0435\u0434\u043f\u043e\u043b\u043e\u0436\u0438\u043b, \u0447\u0442\u043e \u0434\u0435\u043b\u043e \u0432 \u0438\u043d\u0442\u0435\u0440\u0432\u0430\u043b\u0435 \u0432\u0440\u0435\u043c\u0435\u043d, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043e\u0431\u0440\u0430\u0431\u0430\u0442\u044b\u0432\u0430\u0435\u0442 \u043e\u0442\u0447\u0435\u0442. \u041f\u043e\u043c\u043e\u0436\u0435\u0442 \u0444\u0443\u043d\u043a\u0446\u0438\u044f:<\/p>\n<pre><code class=\"sql\">create function [dbo].[getpar] (@par varchar(32), @t varchar(max)) returns varchar(8000) as   begin   declare @i int   set @i = charindex('@'+@par,@t)   if @i &lt; 0 return ''   set @t=substring(@t,@i+len(@par)+2,8000)   if left(@t,1)='N' set @t=substring(@t,2,8000)   if left(@t,1)='''' set @t=substring(@t,2,8000)   set @i = charindex(',@',@t)   if @i > 0 set @t=substring(@t,1,@i-1)   if left(reverse(@t),1)='''' set @t=substring(@t,1,len(@t)-1)   return @t   end<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0432\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u043f\u0440\u043e\u0430\u043d\u0430\u043b\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0432\u044b\u0437\u043e\u0432\u044b \u0442\u0430\u043a:<\/p>\n<pre><code class=\"sql\">select 1+datediff(dd,          convert(datetime,dbo.getpar('from',TextData)),          convert(datetime,dbo.getpar('to',TextData))) as days   , CPU,Reads,Writes,Duration\/1000 as Duration   from MyTrace where TextData like 'exec%MyReport%'<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u043e\u0432\u0435\u0440\u0438\u0442\u044c \u0433\u0438\u043f\u043e\u0442\u0435\u0437\u0443, \u043f\u043e\u0441\u0442\u0440\u043e\u0438\u0432  Excel X-Y scatter diagram (X-days, Y-Duration \u0438\u043b\u0438 CPU)<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"v-portal\" style=\"display:none;\"><\/div>\n<\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/647449\/\"> https:\/\/habr.com\/ru\/post\/647449\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-328502","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/328502","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=328502"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/328502\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=328502"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=328502"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=328502"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}