如何在 SQL Server Management Studio 中查看执行的查询?

发布于 2024-08-25 09:32:25 字数 179 浏览 6 评论 0原文

我是 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 技术交流群。

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

发布评论

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

评论(8

猫卆 2024-09-01 09:32:25

使用 SQL Profiler 并使用过滤器来获取最昂贵的查询。

Use SQL Profiler and use a filter on it to get the most expensive queries.

半﹌身腐败 2024-09-01 09:32:25

使用活动监视器。它是顶部栏中的最后一个工具栏。它将向您显示“最近昂贵的查询”列表。可以双击查看执行计划等。

活动监视器按钮

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.

Activity Monitor Button

安人多梦 2024-09-01 09:32:25

如果您想查看已执行的查询,则没有支持的默认方法可以执行此操作。您可以尝试一些解决方法,但不要指望找到所有方法。

您肯定无法看到 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.

深海不蓝 2024-09-01 09:32:25

您需要一个 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.

听不够的曲调 2024-09-01 09:32:25

从 Management Studio 对正在运行的进程运行以下查询:

DBCC inputbuffer( spid# )

这将返回当前针对所提供的 SPID 的数据库运行的 SQL。请注意,您需要适当的权限才能运行此命令。

这比运行跟踪更好,因为它针对特定的 SPID。您可以根据 CPUTime 和 DiskIO 查看它是否长时间运行。

获取 SPID 64 详细信息的示例:

DBCC inputbuffer(64)

Run the following query from Management Studio on a running process:

DBCC inputbuffer( spid# )

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:

DBCC inputbuffer(64)
拧巴小姐 2024-09-01 09:32:25
     SELECT *  FROM sys.dm_exec_sessions es
  INNER JOIN sys.dm_exec_connections ec
      ON es.session_id = ec.session_id
  CROSS APPLY sys.dm_exec_sql_text(ec.most_recent_sql_handle) where es.session_id=65 under see text contain...
     SELECT *  FROM sys.dm_exec_sessions es
  INNER JOIN sys.dm_exec_connections ec
      ON es.session_id = ec.session_id
  CROSS APPLY sys.dm_exec_sql_text(ec.most_recent_sql_handle) where es.session_id=65 under see text contain...
油饼 2024-09-01 09:32:25

如果您希望 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.

怀念你的温柔 2024-09-01 09:32:25

更清晰的查询,针对 Studio sql 查询是:

SELECT text  FROM sys.dm_exec_sessions es
  INNER JOIN sys.dm_exec_connections ec
      ON es.session_id = ec.session_id
  CROSS APPLY sys.dm_exec_sql_text(ec.most_recent_sql_handle) 
  where program_name like '%Query'

More clear query, targeting Studio sql queries is :

SELECT text  FROM sys.dm_exec_sessions es
  INNER JOIN sys.dm_exec_connections ec
      ON es.session_id = ec.session_id
  CROSS APPLY sys.dm_exec_sql_text(ec.most_recent_sql_handle) 
  where program_name like '%Query'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文