查询历史是否存储在一些日志文件中?如果有,你能告诉我怎么找到他们的位置吗?如果没有,你能给我一些建议吗?


当前回答

[因为这个问题可能会作为一个重复的问题结束。]

如果SQL Server还没有重新启动(并且计划还没有被清除等等),您可能能够在计划缓存中找到该查询。

SELECT t.[text]
FROM sys.dm_exec_cached_plans AS p
CROSS APPLY sys.dm_exec_sql_text(p.plan_handle) AS t
WHERE t.[text] LIKE N'%something unique about your query%';

如果你因为Management Studio崩溃而丢失了文件,你可以在这里找到恢复文件:

C:\Users\<you>\Documents\SQL Server Management Studio\Backup Files\

否则,你将需要使用其他工具来帮助你保存查询历史,比如Ed Harper的回答中提到的SSMS Tools Pack——尽管它在SQL Server 2012+中不是免费的。或者您可以设置一些轻量级跟踪过滤您的登录名或主机名(但请使用服务器端跟踪,而不是Profiler)。


正如@Nenad-Zivkovic评论的那样,加入sys可能会有帮助。Dm_exec_query_stats和last_execution_time排序:

SELECT t.[text], s.last_execution_time
FROM sys.dm_exec_cached_plans AS p
INNER JOIN sys.dm_exec_query_stats AS s
   ON p.plan_handle = s.plan_handle
CROSS APPLY sys.dm_exec_sql_text(p.plan_handle) AS t
WHERE t.[text] LIKE N'%something unique about your query%'
ORDER BY s.last_execution_time DESC;

其他回答

如果您需要通过SMSS执行的查询的历史记录。

你可能想试试SSMSPlus。

https://github.com/akarzazi/SSMSPlus

这个特性在SSMS中并不是现成的。

您需要SSMS 18或更新版本。

声明:我是作者。

If the queries you are interested in are dynamic queries that fail intermittently, you could log the SQL and the datetime and user in a table at the time the dynamic statement is created. It would be done on a case-by case basis though as it requires specific programming to happen and it takes a littel extra processing time, so do it only for those few queries you are most concerned about. But having a log of the specific statements executed can really help when you are trying to find out why it fails once a month only. Dynamic queries are hard to thoroughly test and sometimes you get one specific input value that just won't work and doing this logging at the time the SQL is created is often the best way to see what specifically wasn in the sql that was built.

下面的SQL查询可以显示简单的查询日志:

SELECT last_execution_time, text
FROM sys.dm_exec_query_stats stats
CROSS APPLY sys.dm_exec_sql_text(stats.sql_handle) 
ORDER BY last_execution_time

下图是它的样子:

并且,下面的SQL查询可以显示简单的事务查询日志:

SELECT Operation, [Begin Time], [End Time] 
FROM fn_dblog(NULL,NULL)

下图是它的样子:

此外,我还不知道SQL查询将简单查询日志和简单事务查询日志显示在一起。

你可以使用“每次保存自动生成脚本”,如果你使用管理工作室。 这当然不是日志。 检查一下是否对你有用。;)

[因为这个问题可能会作为一个重复的问题结束。]

如果SQL Server还没有重新启动(并且计划还没有被清除等等),您可能能够在计划缓存中找到该查询。

SELECT t.[text]
FROM sys.dm_exec_cached_plans AS p
CROSS APPLY sys.dm_exec_sql_text(p.plan_handle) AS t
WHERE t.[text] LIKE N'%something unique about your query%';

如果你因为Management Studio崩溃而丢失了文件,你可以在这里找到恢复文件:

C:\Users\<you>\Documents\SQL Server Management Studio\Backup Files\

否则,你将需要使用其他工具来帮助你保存查询历史,比如Ed Harper的回答中提到的SSMS Tools Pack——尽管它在SQL Server 2012+中不是免费的。或者您可以设置一些轻量级跟踪过滤您的登录名或主机名(但请使用服务器端跟踪,而不是Profiler)。


正如@Nenad-Zivkovic评论的那样,加入sys可能会有帮助。Dm_exec_query_stats和last_execution_time排序:

SELECT t.[text], s.last_execution_time
FROM sys.dm_exec_cached_plans AS p
INNER JOIN sys.dm_exec_query_stats AS s
   ON p.plan_handle = s.plan_handle
CROSS APPLY sys.dm_exec_sql_text(p.plan_handle) AS t
WHERE t.[text] LIKE N'%something unique about your query%'
ORDER BY s.last_execution_time DESC;