如何在 SQL Server Management Studio 中查看执行的查询?
我是 SQL Server Management Studio 的新手,我想知道:有没有办法查看对数据库运行了哪些查询?
在活动监视器中,有一个“最近昂贵的查询”报告,但我猜这不是所有查询,因为我没有看到我运行过的查询。
我正在运行 SQL Server Express 2008 v 10.0.1600.22。
I am new to SQL Server Management Studio and am wondering: is there is a way to see which queries have been run against a database?
In the Activity monitor, there is a "Recent Expensive Queries" report but I'm guessing that isn't all of the queries since I'm not seeing the ones I have run.
I am running SQL Server Express 2008 v 10.0.1600.22.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
使用 SQL Profiler 并使用过滤器来获取最昂贵的查询。
Use SQL Profiler and use a filter on it to get the most expensive queries.
使用活动监视器。它是顶部栏中的最后一个工具栏。它将向您显示“最近昂贵的查询”列表。可以双击查看执行计划等。
Use the Activity Monitor. It's the last toolbar in the top bar. It will show you a list of "Recent Expensive Queries". You can double-click them to see the execution plan, etc.
如果您想查看已执行的查询,则没有支持的默认方法可以执行此操作。您可以尝试一些解决方法,但不要指望找到所有方法。
您肯定无法看到 SELECT 语句,但有一种方法可以通过读取事务日志来查看其他 DML 和 DDL 命令(假设数据库处于完全恢复模式)。
您可以使用 DBCC LOG 或 fn_dblog 命令或第三方日志阅读器(例如 ApexSQL Log)(请注意,该工具附带价格)来执行此操作)
现在,如果您计划审计将来要执行的语句,那么您可以使用 SQL Profiler 来捕获所有内容。
If you want to see queries that are already executed there is no supported default way to do this. There are some workarounds you can try but don’t expect to find all.
You won’t be able to see SELECT statements for sure but there is a way to see other DML and DDL commands by reading transaction log (assuming database is in full recovery mode).
You can do this using DBCC LOG or fn_dblog commands or third party log reader like ApexSQL Log (note that tool comes with a price)
Now, if you plan on auditing statements that are going to be executed in the future then you can use SQL Profiler to catch everything.
您需要一个 SQL 探查器,它实际上在 SQL Management Studio 外部运行。如果您有 SQL Server 的付费版本(如开发人员版本),它应该作为另一个实用程序包含在其中。
如果您使用的是免费版本 (SQL Express),您可以下载它们的免费软件配置文件。我使用过 AnjLab 的分析器(位于 http://sites.google.com/site/sqlprofiler),而且看起来效果很好。
You need a SQL profiler, which actually runs outside SQL Management Studio. If you have a paid version of SQL Server (like the developer edition), it should be included in that as another utility.
If you're using a free edition (SQL Express), they have freeware profiles that you can download. I've used AnjLab's profiler (available at http://sites.google.com/site/sqlprofiler), and it seemed to work well.
从 Management Studio 对正在运行的进程运行以下查询:
这将返回当前针对所提供的 SPID 的数据库运行的 SQL。请注意,您需要适当的权限才能运行此命令。
这比运行跟踪更好,因为它针对特定的 SPID。您可以根据 CPUTime 和 DiskIO 查看它是否长时间运行。
获取 SPID 64 详细信息的示例:
Run the following query from Management Studio on a running process:
This will return the SQL currently being run against the database for the SPID provided. Note that you need appropriate permissions to run this command.
This is better than running a trace since it targets a specific SPID. You can see if it's long running based on its CPUTime and DiskIO.
Example to get details of SPID 64:
如果您希望 SSMS 维护查询历史记录,请使用 SSMS 工具包插件。
如果您想监视 SQL Server 当前正在运行的查询,请使用 SQL PROfiler,正如其他人已经建议的那样。
If you want SSMS to maintain a query history, use the SSMS Tool Pack add on.
If you want to monitor the SQL Server for currently running queries, use SQL PRofiler as other have already suggested.
更清晰的查询,针对 Studio sql 查询是:
More clear query, targeting Studio sql queries is :