如何将 SQL Server Profiler 2008 的输出转储到类似 CSV 的文件
我正在调试存储过程,现在我感兴趣的是运行的顺序和参数,而不是运行的速度以及两者之间可能潜入的内容并导致速度减慢。
因此,我捕获了几分钟的痕迹。问题是内容太多了,我需要缩小范围。如果我这样做 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果将其保存到跟踪表中;您可以在 SQL Server 中获取表中的数据,这将让您随心所欲地操作它;包括将其转储为 CSV(如果仍需要)。文本数据列完整地显示在表中。
如果您选择保存→跟踪表。系统将提示您输入表和数据库的名称。假设您在数据库暂存中将其称为 ProfilerTemp。
输入那些;您可以使用以下命令查询表
。您将在表中的跟踪窗口中看到所有内容。如果您没有过滤到仅存储过程并希望仅将它们放在选择中,
则此过滤器会过滤掉非过程调用以及您可能拥有的任何连接重置。您可能需要添加更多过滤器,具体取决于您想要执行的操作。
如果您想将其作为文本文件输出;选择要归档的查询 - 结果并运行查询。这将提示输入文件名,并以文本文件形式提供参数文本。
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 Save → Trace 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
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
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.
TL;DR:复制到文本编辑器中,手动准备,然后粘贴到 Excel 中。
我对 SQL Server 的经验很少,所以我不知道这是否对其他人有用,但它对我有用:
(N'(''')?[^']*?)\r\n(([^']*?)\r\n)?(([^']*?)\ r\n)?
与$1 $4 $6
(Batch(Starting|Completed)[^\\]*?)\r\n
替换为$1
\r\nset
替换为set
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:
(N'(''')?[^']*?)\r\n(([^']*?)\r\n)?(([^']*?)\r\n)?
with$1 $4 $6
(Batch(Starting|Completed)[^\\]*?)\r\n
with$1
\r\nset
withset
试试这个:
select * from fn_trace_gettable('D:\abc.trc',default)
Try this:
select * from fn_trace_gettable('D:\abc.trc',default)
我有一堆 SQL Profiler 跟踪文件,其中包含我需要分析的死锁跟踪事件。我最终使用 SQL Profiler 将它们转换为单个 XML 文件,然后使用
lxml
和pandas
库通过 Python 读取该 XML 文件。由于数据最终存储在 pandas
Dataframe
中,您可以轻松地将其转换为 CSV、Excel 等,或者在 Python 中对其进行分析(如果您熟悉 pandas)。该脚本以 gist 形式提供。它是专门为死锁跟踪文件编写的,因为我没有任何其他可用的跟踪。换句话说,您可能需要对其进行一些调整才能达到您的目的。如果 XML 文件很大(几百 MB),请不要担心;该脚本使用
iterparse()
,这意味着文件不会被读入内存,只会捕获相关元素。例如 ca 的xdl
文件。 220MB 的解析时间不到 13 秒。将跟踪文件保存为 XML(
xdl
扩展名):确保选择单个文件中的所有事件选项。
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
andpandas
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. anxdl
file of ca. 220MB was parsed in less than 13 seconds.Saving the trace files as XML (
xdl
extension):Make sure you select the option all events in a single file.