翻譯|使用教程|編輯:鮑佳佳|2020-10-20 11:18:41.040|閱讀 857 次
概述:查詢歷史記錄中存儲的數(shù)據(jù)對于任何DBA來說都是最有價(jià)值的。在許多情況下,必須追溯SQL Server查詢歷史記錄。例如,如果您的SQL Server發(fā)生崩潰,則可能需要調(diào)查特定的工作案例,檢查備份歷史記錄或恢復(fù)特定的查詢。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關(guān)鏈接:
dbForge Studio for SQL Server為有效的探索、分析SQL Server數(shù)據(jù)庫中的大型數(shù)據(jù)集提供全面的解決方案,并設(shè)計(jì)各種報(bào)表以幫助作出合理的決策。
點(diǎn)擊下載dbForge Studio for SQL Server最新試用版
查詢歷史記錄中存儲的數(shù)據(jù)對于任何DBA來說都是最有價(jià)值的。在許多情況下,必須追溯SQL Server查詢歷史記錄。例如,如果您的SQL Server發(fā)生崩潰,則可能需要調(diào)查特定的工作案例,檢查備份歷史記錄或恢復(fù)特定的查詢。
幸運(yùn)的是,對于任何故障或例行分析任務(wù),都會顯示完整的SQL Server查詢歷史記錄,并且存在使用這些數(shù)據(jù)的用戶友好型解決方案。
當(dāng)需要在特定時(shí)間段內(nèi)查看有關(guān)SSMS中已執(zhí)行的SQL語句的數(shù)據(jù)時(shí),可以使用以下幾個(gè)選項(xiàng):
例如,選項(xiàng)#1允許您使用以下腳本輸出100個(gè)執(zhí)行時(shí)間最長(包括所有延遲)的查詢:
with s as ( select top(100) creation_time, last_execution_time, execution_count, total_worker_time/1000 as CPU, convert(money, (total_worker_time))/(execution_count*1000)as [AvgCPUTime], qs.total_elapsed_time/1000 as TotDuration, convert(money, (qs.total_elapsed_time))/(execution_count*1000)as [AvgDur], total_logical_reads as [Reads], total_logical_writes as [Writes], total_logical_reads+total_logical_writes as [AggIO], convert(money, (total_logical_reads+total_logical_writes)/(execution_count + 0.0)) as [AvgIO], [sql_handle], plan_handle, statement_start_offset, statement_end_offset, plan_generation_num, total_physical_reads, convert(money, total_physical_reads/(execution_count + 0.0)) as [AvgIOPhysicalReads], convert(money, total_logical_reads/(execution_count + 0.0)) as [AvgIOLogicalReads], convert(money, total_logical_writes/(execution_count + 0.0)) as [AvgIOLogicalWrites], query_hash, query_plan_hash, total_rows, convert(money, total_rows/(execution_count + 0.0)) as [AvgRows], total_dop, convert(money, total_dop/(execution_count + 0.0)) as [AvgDop], total_grant_kb, convert(money, total_grant_kb/(execution_count + 0.0)) as [AvgGrantKb], total_used_grant_kb, convert(money, total_used_grant_kb/(execution_count + 0.0)) as [AvgUsedGrantKb], total_ideal_grant_kb, convert(money, total_ideal_grant_kb/(execution_count + 0.0)) as [AvgIdealGrantKb], total_reserved_threads, convert(money, total_reserved_threads/(execution_count + 0.0)) as [AvgReservedThreads], total_used_threads, convert(money, total_used_threads/(execution_count + 0.0)) as [AvgUsedThreads] from sys.dm_exec_query_stats as qs with(readuncommitted) order by convert(money, (qs.total_elapsed_time))/(execution_count*1000) desc ) select s.creation_time, s.last_execution_time, s.execution_count, s.CPU, s.[AvgCPUTime], s.TotDuration, s.[AvgDur], s.[AvgIOLogicalReads], s.[AvgIOLogicalWrites], s.[AggIO], s.[AvgIO], s.[AvgIOPhysicalReads], s.plan_generation_num, s.[AvgRows], s.[AvgDop], s.[AvgGrantKb], s.[AvgUsedGrantKb], s.[AvgIdealGrantKb], s.[AvgReservedThreads], s.[AvgUsedThreads], --st.text as query_text, case when sql_handle IS NULL then ' ' else(substring(st.text,(s.statement_start_offset+2)/2,( case when s.statement_end_offset =-1 then len(convert(nvarchar(MAX),st.text))*2 else s.statement_end_offset end - s.statement_start_offset)/2 )) end as query_text, db_name(st.dbid) as database_name, object_schema_name(st.objectid, st.dbid)+'.'+object_name(st.objectid, st.dbid) as [object_name], sp.[query_plan], s.[sql_handle], s.plan_handle, s.query_hash, s.query_plan_hash from s cross apply sys.dm_exec_sql_text(s.[sql_handle]) as st cross apply sys.dm_exec_query_plan(s.[plan_handle]) as sp
至于其他選項(xiàng),值得強(qiáng)調(diào)的是在SSMS中使用SQL Complete的可能性。
SQL Complete在指定的時(shí)間段內(nèi)將有關(guān)已執(zhí)行的SQL語句的主要信息存儲在SSMS中。使用此SSMS加載項(xiàng),您可以查看,編輯和搜索數(shù)據(jù)庫中運(yùn)行的T-SQL查詢。便捷的界面使這項(xiàng)工作更加易于管理。
特別是,請注意SQL Complete提供的以下功能:
SQL Complete解決方案存儲在磁盤上的文件中或在使用連接字符串指定的數(shù)據(jù)庫中的表中運(yùn)行的每個(gè)語句。存儲周期可以是任意的—您可以自己在SQL Complete選項(xiàng)中設(shè)置它。因此,如果您正在編寫腳本,您將獲得與所做的每一個(gè)更改相關(guān)的完整歷史記錄。但是請記住,SQL Complete只為SQL Complete與之集成的SSMS執(zhí)行的查詢存儲歷史記錄。
SQL語句保存在寫入文件和/或數(shù)據(jù)庫的列表中。該解決方案可以自動(dòng)刪除舊的SQL查詢歷史記錄日志。此外,您還可以進(jìn)行監(jiān)視和執(zhí)行任何特定查詢,同時(shí)接收其他有價(jià)值的信息進(jìn)行研究和分析。
您可以使用任何資源來按用戶檢查MS SQL Server查詢歷史記錄。可以輕松方便地為您提供各種執(zhí)行任務(wù)的工具。
點(diǎn)擊下載dbForge Studio for SQL Server,并通過30天免費(fèi)試用版自行檢查此功能!慧都現(xiàn)隆重推出“軟件國產(chǎn)化服務(wù)”活動(dòng),現(xiàn)dbForge Studio SQL Sever直降3000,在線訂購正版授權(quán)最低只要1710元!
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自: