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


当前回答

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

其他回答

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

一个稍微开箱即用的方法是在AutoHotKey中编写一个解决方案。我用了这个,它不是完美的,但是有效而且免费。从本质上讲,这个脚本为CTRL+SHIFT+R分配了一个热键,它将复制SSMS (CTRL+C)中所选的SQL,保存一个日期戳SQL文件,然后执行突出显示的查询(F5)。如果您不习惯AHK脚本,则前导分号是注释。

;CTRL+SHIFT+R to run a query that is first saved off
^+r::
;Copy
Send, ^c
; Set variables
EnvGet, HomeDir, USERPROFILE
FormatTime, DateString,,yyyyMMdd
FormatTime, TimeString,,hhmmss
; Make a spot to save the clipboard
FileCreateDir %HomeDir%\Documents\sqlhist\%DateString%
FileAppend, %Clipboard%, %HomeDir%\Documents\sqlhist\%DateString%\%TimeString%.sql
; execute the query
Send, {f5}
Return

最大的限制是,如果你点击“执行”而不是使用键盘快捷键,这个脚本将无法工作,并且这个脚本不会保存整个文件-只保存选中的文本。但是,您总是可以修改脚本以执行查询,然后在复制/保存之前选择所有(CTRL+A)。

使用具有“在文件中查找”功能的现代编辑器可以让您搜索SQL历史记录。您甚至可以把您的文件刮到SQLite3数据库中以查询您的查询。

正如其他人所指出的,您可以使用SQL Profiler,但也可以通过sp_trace_*系统存储过程来利用它的功能。例如,这个SQL片段将(至少在2000年;我认为这对SQL 2008是一样的,但你必须仔细检查)捕获RPC:Completed和SQL:BatchCompleted事件的所有查询需要超过10秒运行,并将输出保存到一个跟踪文件,您可以在稍后的日期在SQL分析器中打开:

DECLARE @TraceID INT
DECLARE @ON BIT
DECLARE @RetVal INT
SET @ON = 1

exec @RetVal = sp_trace_create @TraceID OUTPUT, 2, N'Y:\TraceFile.trc'
print 'This trace is Trace ID = ' + CAST(@TraceID AS NVARCHAR)
print 'Return value = ' + CAST(@RetVal AS NVARCHAR)
-- 10 = RPC:Completed
exec sp_trace_setevent @TraceID, 10, 1, @ON     -- Textdata
exec sp_trace_setevent @TraceID, 10, 3, @ON     -- DatabaseID
exec sp_trace_setevent @TraceID, 10, 12, @ON        -- SPID
exec sp_trace_setevent @TraceID, 10, 13, @ON        -- Duration
exec sp_trace_setevent @TraceID, 10, 14, @ON        -- StartTime
exec sp_trace_setevent @TraceID, 10, 15, @ON        -- EndTime

-- 12 = SQL:BatchCompleted
exec sp_trace_setevent @TraceID, 12, 1, @ON     -- Textdata
exec sp_trace_setevent @TraceID, 12, 3, @ON     -- DatabaseID
exec sp_trace_setevent @TraceID, 12, 12, @ON        -- SPID
exec sp_trace_setevent @TraceID, 12, 13, @ON        -- Duration
exec sp_trace_setevent @TraceID, 12, 14, @ON        -- StartTime
exec sp_trace_setevent @TraceID, 12, 15, @ON        -- EndTime

-- Filter for duration [column 13] greater than [operation 2] 10 seconds (= 10,000ms)
declare @duration bigint
set @duration = 10000
exec sp_trace_setfilter @TraceID, 13, 0, 2, @duration

您可以从Books Online中找到每个跟踪事件、列等的ID;只需搜索sp_trace_create、sp_trace_setevent和sp_trace_setfiler sppros即可。然后,您可以像下面这样控制跟踪:

exec sp_trace_setstatus 15, 0       -- Stop the trace
exec sp_trace_setstatus 15, 1       -- Start the trace
exec sp_trace_setstatus 15, 2       -- Close the trace file and delete the trace settings

...其中'15'是跟踪ID(如上面的第一个脚本所删除的sp_trace_create所报告的)。

您可以查看哪些跟踪正在运行:

select * from ::fn_trace_getinfo(default)

我唯一要谨慎说的是——我不知道这会给你的系统带来多少负载;它将添加一些,但“一些”的大小可能取决于您的服务器有多忙。

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的最慢的20个查询

select top 20 dest.text, deqs.execution_count, deqs.total_elapsed_time, deqs.total_worker_time,
(deqs.total_elapsed_time / deqs.execution_count) as 'avg_elapse_time',
(deqs.total_worker_time / deqs.execution_count) as 'avg_worker_time'
from sys.dm_exec_query_stats as deqs
CROSS APPLY sys.dm_exec_sql_text(deqs.sql_handle) as dest
where deqs. last_execution_time >= '2021-09-27 16:00' /* YOUR DATE AND TIME HERE*/
order by 'avg_elapse_time' desc