如何将 SQL Server Profiler 2008 的输出转储到类似 CSV 的文件

发布于 2024-10-04 03:56:55 字数 557 浏览 8 评论 0原文

我正在调试存储过程,现在我感兴趣的是运行的顺序和参数,而不是运行的速度以及两者之间可能潜入的内容并导致速度减慢。

因此,我捕获了几分钟的痕迹。问题是内容太多了,我需要缩小范围。如果我这样做 File ->另存为,我得到以下选项:

  • 跟踪文件...
  • 跟踪模板...
  • 跟踪表...
  • 跟踪 XML 文件...
  • 用于重播的跟踪 XML 文件...

现在,这些都是不错的选项,但是我真正想要的是表格格式,例如 CSV。我认为 SP 跟踪中的逗号可能会弄乱 CSV 格式。我很乐意使用其他东西,例如 || 作为分隔符。

一旦我有了表格格式,我就可以使用 grep 等对其进行过滤,然后使用 Python 轻松处理它以查看我想要的确切内容。我开始使用脚本解析 XML 文件,但发现自己在跟踪 XML 文件格式上花费了太多时间(之前没有使用过 lxml 库)。

那么...有更简单的方法吗?我至少可以将其复制到 Excel 中吗?

I am debugging stored procedures, and right now I am interested in what ran in what order and which which parameters as opposed to how fast things ran and what may sneak in between and cause a slowdown.

So, I captured a couple of minutes worth of trace. The problem is that there is too much stuff, and I need to narrow it down. If I do File -> Save As, I get the following options:

  • Trace File...
  • Trace Template...
  • Trace Table...
  • Trace XML File...
  • Trace XML File for Replay...

Now, these are decent options, but what I really want is a tabular format, such as CSV. I think that commas in SP trace would probably mess up the CSV format. I would gladly use something else, such as || as a delimiter.

Once I do have the tabular format, I can filter it down using grep, etc. and then easily process it with Python to see the exact things I want. I started parsing the XML file with a script, but found myself spending too much time on the trace XML file format (have not used lxml library before).

So ... is there an easier way? Can I at least copy it to Excel somehow?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

﹏雨一样淡蓝的深情 2024-10-11 03:56:55

如果将其保存到跟踪表中;您可以在 SQL Server 中获取表中的数据,这将让您随心所欲地操作它;包括将其转储为 CSV(如果仍需要)。文本数据列完整地显示在表中。


如果您选择保存跟踪表。系统将提示您输入表和数据库的名称。假设您在数据库暂存中将其称为 ProfilerTemp。

输入那些;您可以使用以下命令查询表

select * from scratch.dbo.ProfilerTemp

。您将在表中的跟踪窗口中看到所有内容。如果您没有过滤到仅存储过程并希望仅将它们放在选择中,

Select textdata from [Scratch].[dbo].[ProfilerTemp] 
  where eventclass = 10 
  And textdata like 'exec %' 
  and not cast(TextData as nvarchar(max))= 'exec sp_reset_connection'

则此过滤器会过滤掉非过程调用以及您可能拥有的任何连接重置。您可能需要添加更多过滤器,具体取决于您想要执行的操作。

如果您想将其作为文本文件输出;选择要归档的查询 - 结果并运行查询。这将提示输入文件名,并以文本文件形式提供参数文本。

If you save it into a trace table; you can get the data in a table in SQL Server which will let you manipulate it to your hearts content; including dumping it out to CSV if still required. The text data column is fully represented in the table.


If you choose SaveTrace Table. You will be prompted for the name of the table and the database. Lets say you call it ProfilerTemp in the database scratch.

Enter those; you can query the table using

select * from scratch.dbo.ProfilerTemp

You will see everything in the trace window in the table. If you didnt filter down to just stored procedures and want just them in the select

Select textdata from [Scratch].[dbo].[ProfilerTemp] 
  where eventclass = 10 
  And textdata like 'exec %' 
  and not cast(TextData as nvarchar(max))= 'exec sp_reset_connection'

This filters out non procedure calls and any connection resets you may have. You may need to add more filters depending on what you are trying to do.

If you want this out as a text file; choose query - results to file and run the query. This will prompt for the file name and give you the parameter text as a text file.

老旧海报 2024-10-11 03:56:55

TL;DR:复制到文本编辑器中,手动准备,然后粘贴到 Excel 中。

我对 SQL Server 的经验很少,所以我不知道这是否对其他人有用,但它对我有用:

  • 在 SQL Server Profiler 中选择所需的行。 Ctrl + C 进行复制。
  • 粘贴到可以执行正则表达式搜索和替换的纯文本编辑器中(例如,在我的例子中为 Notepad++)。
  • 正则表达式替换 (N'(''')?[^']*?)\r\n(([^']*?)\r\n)?(([^']*?)\ r\n)?$1 $4 $6
    • 这会清除 SQL 脚本中的所有换行符。
    • 继续执行“全部替换”,直到找不到更多结果。
  • 正则表达式将 (Batch(Starting|Completed)[^\\]*?)\r\n 替换为 $1
    • 这会清除更多 SQL 内容中的换行符。再次,不断更换,直到没有结果。
  • 正则表达式将 \r\nset 替换为 set
    • 这会清除审核登录脚本中的所有换行符
  • 您可能需要进行更多替换,您明白了。
  • 使用“文本导入向导”粘贴到 Excel 中。使用制表符作为分隔符。
  • 按第一列排序并删除任何无用的行(例如,在我的例子中为“审核登录”)。您可能还需要手动在列上移动一些数据(例如,在我的例子中为“EntityFramework”数据)

TL;DR: Copy into a text editor, manually prep, then paste into Excel.

I have very little experience with SQL Server, so I don't know if this will work for others, but it did for me:

  • Select desired rows in SQL Server Profiler. Ctrl + C to copy.
  • Paste into a plain text editor that can do regular expression search and replace (e.g. Notepad++ in my case).
  • Regex replace (N'(''')?[^']*?)\r\n(([^']*?)\r\n)?(([^']*?)\r\n)? with $1 $4 $6
    • This clears all newlines from SQL scripts.
    • Keep doing "Replace All" until no more results are found.
  • Regex replace (Batch(Starting|Completed)[^\\]*?)\r\n with $1
    • This clears newlines from more SQL stuff. Again, keep replacing until no results.
  • Regex replace \r\nset with set
    • This clears all newlines from Audit Login scripts
  • You may need to do more replacing, you get the idea.
  • Paste into excel using the "Text Import Wizard." Use tabs as the deliminator.
  • Sort by the first column and remove any unhelpful rows (e.g. "Audit Login" in my case). You may also need to manually move some data over a column (e.g. "EntityFramework" data in my case)
孤独患者 2024-10-11 03:56:55

试试这个:

  1. 打开 SSMS
  2. 运行 select * from fn_trace_gettable('D:\abc.trc',default)
  3. 右键单击​​并选择标题
  4. 将其粘贴到 Excel 中

Try this:

  1. Open SSMS
  2. run select * from fn_trace_gettable('D:\abc.trc',default)
  3. Right click and select with headers
  4. Paste it into Excel
残疾 2024-10-11 03:56:55

我有一堆 SQL Profiler 跟踪文件,其中包含我需要分析的死锁跟踪事件。我最终使用 SQL Profiler 将它们转换为单个 XML 文件,然后使用 lxmlpandas 库通过 Python 读取该 XML 文件。

由于数据最终存储在 pandas Dataframe 中,您可以轻松地将其转换为 CSV、Excel 等,或者在 Python 中对其进行分析(如果您熟悉 pandas)。

该脚本以 gist 形式提供。它是专门为死锁跟踪文件编写的,因为我没有任何其他可用的跟踪。换句话说,您可能需要对其进行一些调整才能达到您的目的。如果 XML 文件很大(几百 MB),请不要担心;该脚本使用 iterparse(),这意味着文件不会被读入内存,只会捕获相关元素。例如 ca 的 xdl 文件。 220MB 的解析时间不到 13 秒。

将跟踪文件保存为 XML(xdl 扩展名):

Extract

确保选择单个文件中的所有事件选项。

保存

I had a bunch of SQL Profiler trace files containing deadlock trace events that I needed to analyze. I ended up converting them to a single XML file using SQL Profiler, and then reading that XML file with Python, using the lxml and pandas libraries.

As the data ends up in a pandas Dataframe, you can easily convert it to CSV, Excel, ... or analyze it in Python (if you're familiar with pandas).

The script is available as a gist. It's written specifically for deadlock trace files, as I didn't have any other traces available. In other words, you might need to tweak it a little bit to serve your purpose. Don't worry if the XML file is large (several 100 MBs); the script uses iterparse(), which means the file won't be read into memory and only the relevant elements will be captured. E.g. an xdl file of ca. 220MB was parsed in less than 13 seconds.

Saving the trace files as XML (xdl extension):

Extract

Make sure you select the option all events in a single file.

Save

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文