系统与问题简介
- 后端:C# + 封装的SQL查询框架
- 数据库:SQL Server 2008
- 问题:SQL查询极慢,分页查询100条需要8-10s
问题排查
查询慢SQL语句
SELECT TOP 10 QS.creation_time,
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 statement_text,
ST.text,
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 1 = 1
and QS.creation_time BETWEEN '2020-10-16 8:40:00' AND '2020-10-16 8:50:00'
AND ST.text LIKE '%%'
AND total_worker_time > 2000000
ORDER BY QS.creation_time DESC
结果
SELECT a.ordersid AS ordersid,
a.projectid AS projectid,
a.orders AS orders,
a.names AS names,
a.wbs AS wbs,
a.wbs4TP AS wbs4TP,
-- 中间省略30列....
CONVERT(NCHAR(10), a.actualTurningdate, 120) AS actualTurningdate,
CONVERT(NCHAR