查询历史是否存储在一些日志文件中?如果有,你能告诉我怎么找到他们的位置吗?如果没有,你能给我一些建议吗?
当前回答
如果您需要通过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 Profiler监视SQL查询
你可以使用“每次保存自动生成脚本”,如果你使用管理工作室。 这当然不是日志。 检查一下是否对你有用。;)
我相信在座各位的回答。
这个脚本展示了如何找到影响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
一个稍微开箱即用的方法是在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数据库中以查询您的查询。
推荐文章
- 如何在Ruby On Rails中使用NuoDB手动执行SQL命令
- 查询JSON类型内的数组元素
- 确定记录是否存在的最快方法
- 如何配置slf4j-simple
- 获得PostgreSQL数据库中当前连接数的正确查询
- 在SQL选择语句Order By 1的目的是什么?
- MySQL数据库表中的最大记录数
- 如何在Python中记录源文件名称和行号
- 从现有模式生成表关系图(SQL Server)
- 我如何循环通过一组记录在SQL Server?
- MongoDB记录所有查询
- HyperLogLog算法是如何工作的?
- 数据库和模式的区别
- 如何在SQL Server中一次更改多个列
- 如何禁用标准错误流的日志记录?