转自:http://www.cnblogs.com/MR_ke/archive/2010/08/25/1807856.html
SQL Server在运行一段时间,随着数据的积累,SQL运行效率会逐步降低,为了使用业务系统正常动作,经常IT部门需要花高价请SQL调优专家来解决。其实调优也不复杂,主要是找到影响效率的SQL,然后对症下药,这里给出几个技巧,相信对大家非常实用。
1、检查SQL阻塞原因
1
2
3
|
select
blocking_session_id, wait_duration_ms, session_id
from
sys.dm_os_waiting_tasks
where
blocking_session_id
is
not
null
|
2、检查前10个等待资源的SQL语句
1
2
3
|
select
top
10 *
from
sys.dm_os_wait_stats
order
by
wait_time_ms
desc
|
3、查询显示 CPU 平均占用率最高的前50个SQL 语句
1
2
3
4
|
SELECT
TOP
50 total_worker_time/execution_count
AS
[
Avg
CPU
Time
],
(
SELECT
SUBSTRING
(text,statement_start_offset/2,(
CASE
WHEN
statement_end_offset = -1
then
LEN(
CONVERT
(nvarchar(
max
), text)) * 2
ELSE
statement_end_offset
end
-statement_start_offset)/2)
FROM
sys.dm_exec_sql_text(sql_handle))
AS
query_text, *
FROM
sys.dm_exec_query_stats
ORDER
BY
[
Avg
CPU
Time
]
DESC
|
4、CPU 瓶颈通常由以下原因引起:查询计划并非最优、配置不当、设计因素不良或硬件资源不足。下面的常用查询可帮助您确定导致CPU瓶颈的原因。下面的查询使您能够深入了解当前缓存的哪些批处理或过程占用了大部分CPU资源。
1
2
3
4
5
6
7
8
|
SELECT
TOP
50
SUM
(qs.total_worker_time)
AS
total_cpu_time,
SUM
(qs.execution_count)
AS
total_execution_count,
COUNT
(*)
AS
number_of_statements,
qs.sql_handle
FROM
sys.dm_exec_query_stats
AS
qs
GROUP
BY
qs.sql_handle
ORDER
BY
SUM
(qs.total_worker_time)
DESC
|
5、下面的查询显示缓存计划所占用的CPU总使用率(带 SQL 文本)。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
SELECT
total_cpu_time,
total_execution_count,
number_of_statements,
s2.text
ROM
(
SELECT
TOP
50
SUM
(qs.total_worker_time)
AS
total_cpu_time,
SUM
(qs.execution_count)
AS
total_execution_count,
COUNT
(*)
AS
number_of_statements,
qs.sql_handle
FROM
sys.dm_exec_query_stats
AS
qs
GROUP
BY
qs.sql_handle
ORDER
BY
SUM
(qs.total_worker_time)
DESC
)
AS
stats
CROSS
APPLY sys.dm_exec_sql_text(stats.sql_handle)
AS
s2
|
6、下面的示例查询显示已重新编译的前 25 个存储过程。plan_generation_num 指示该查询已重新编译的次数。
1
2
3
4
5
6
7
8
9
10
11
|
select
top
25
sql_text.text,
sql_handle,
plan_generation_num,
execution_count,
dbid,
objectid
from
sys.dm_exec_query_stats a
cross
apply sys.dm_exec_sql_text(sql_handle)
as
sql_text
where
plan_generation_num > 1
order
by
plan_generation_num
desc
|
7、效率较低的查询计划可能增大 CPU 占用率。下面的查询显示哪个查询占用了最多的 CPU 累计使用率。
1
2
3
4
5
6
|
SELECT
highest_cpu_queries.plan_handle, highest_cpu_queries.total_worker_time, q.dbid, q.objectid, q.number, q.encrypted, q.[text]
from
(
select
top
50 qs.plan_handle, qs.total_worker_time
from
sys.dm_exec_query_stats qs
order
by
qs.total_worker_time
desc
)
as
highest_cpu_queries
cross
apply sys.dm_exec_sql_text(plan_handle)
as
q
order
by
highest_cpu_queries.total_worker_time
desc
|
8、下面的查询显示一些可能占用大量 CPU 使用率的运算符(例如 ‘%Hash Match%’、‘%Sort%’)以找出可疑对象。
1
2
3
4
5
6
7
|
select
*
from
sys.dm_exec_cached_plans
cross
apply sys.dm_exec_query_plan(plan_handle)
where
cast
(query_plan
as
nvarchar(
max
))
like
'%Sort%'
or
cast
(query_plan
as
nvarchar(
max
))
like
'%Hash Match%'
|
9、如果已检测到效率低下并导致 CPU 占用率较高的查询计划,请对该查询中涉及的表运行 UPDATE STATISTICS 以查看该问题是否仍然存在。然后,收集相关数据并将此问题报告给 PerformancePoint Planning 支持人员。如果您的系统存在过多的编译和重新编译,可能会导致系统出现与 CPU 相关的性能问题。您可以运行下面的 DMV 查询来找出过多的编译/重新编译。
1
2
|
select
*
from
sys.dm_exec_query_optimizer_info
where
counter =
'optimizations'
or
counter =
'elapsed time'
|