【SQL-历史执行语句查询】 查询对数据库执行了哪些SQL

时间:2022-09-08 14:51:39

Sql语句

SELECT TOP 1000 
    QS.creation_time as '创建时间',
    SUBSTRING(ST.text,(QS.statement_start_offset/2)+1,
    ((CASE QS.statement_end_offset WHEN -1 THEN DATALENGTH(st.text) 
        ELSE QS.statement_end_offset END - QS.statement_start_offset)/2) + 1
    ) AS '查询语句' ,
    ST.text as '执行文本',
    QS.execution_count AS '执行次数',
    QS.total_elapsed_time AS '耗时',
    QS.total_logical_reads AS '逻辑读取次数',
    QS.total_logical_writes AS '逻辑写入次数',
    QS.total_physical_reads AS '物理读取次数', 
    QS.total_worker_time,
    QS.last_worker_time,
    QS.max_worker_time,
    QS.min_worker_time
FROM 
    sys.dm_exec_query_stats QS
CROSS APPLY 
    sys.dm_exec_sql_text(QS.sql_handle) ST
WHERE   
    QS.creation_time BETWEEN '2018-01-24 00:00:00' AND '2018-01-25 00:00:00' --根据时间搜索 
    AND ST.text LIKE '%%'    --根据语句内容搜索
ORDER BY 
    QS.creation_time DESC