zoukankan      html  css  js  c++  java
  • sqlserver常用调优脚本(转)

    (转)以备不时之需

    最耗时的sql

    declare @n int 
    set @n=500 ;

    with cte1 as
    (
    select a.*,t.*
    from sys.dm_exec_query_stats a
    cross apply sys.dm_exec_sql_text(a.plan_handle) t
    where t.dbid >= 5
    )

    select 
    t.dbid,db_name(t.dbid) as dbname, 
    a.total_worker_time,a.avg_time_ms,a.execution_count,a.cache_count,
    replace(replace(t.text,CHAR(10),' '),CHAR(13),' ')
    from 
    (
    select top(@n)
    plan_handle,
    sum(total_worker_time) / 1000 as total_worker_time , 
    sum(execution_count) as execution_count , 
    count(1) as cache_count,
    (sum(total_worker_time) / sum(execution_count) ) / 1000 as avg_time_ms
    from cte1 
    group by plan_handle
    order by avg_time_ms desc
    ) a 
    cross apply sys.dm_exec_sql_text(a.plan_handle) t
    where avg_time_ms > 200
    order by avg_time_ms desc

    go

     最频繁的sql

    declare @n int 
    set @n=500 ;

    with cte1 as
    (
    select a.*,t.*
    from sys.dm_exec_query_stats a
    cross apply sys.dm_exec_sql_text(a.plan_handle) t
    where t.dbid >= 5
    )

    select 
    t.dbid,db_name(t.dbid) as dbname,
    a.execution_count,a.total_worker_time,a.avg_time_ms,a.cache_count,
    replace(replace(t.text,CHAR(10),' '),CHAR(13),' ')
    from 
    (
    select top(@n)
    plan_handle,
    sum(total_worker_time) / 1000 as total_worker_time , 
    sum(execution_count) as execution_count , 
    count(1) as cache_count,
    (sum(total_worker_time) / sum(execution_count) ) / 1000 as avg_time_ms
    from cte1 
    group by plan_handle
    order by avg_time_ms desc
    ) a 
    cross apply sys.dm_exec_sql_text(a.plan_handle) t
    order by execution_count desc

    go

     耗cpu的sql

    declare @n int 
    set @n=500 ;

    with cte1 as
    (
    select a.*,t.*
    from sys.dm_exec_query_stats a
    cross apply sys.dm_exec_sql_text(a.plan_handle) t
    where t.dbid >= 5
    )

    select 
    t.dbid,db_name(t.dbid) as dbname,
    a.total_logical_reads,a.avg_reads,a.total_logical_writes,a.avg_writes,a.execution_count,
    a.total_worker_time,a.avg_time_ms,a.cache_count,
    replace(replace(t.text,CHAR(10),' '),CHAR(13),' ')
    from 
    (
    select top(@n)
    plan_handle,
    sum(total_logical_reads) as total_logical_reads,
    (sum(total_logical_reads) / sum(execution_count) ) as avg_reads,
    sum(total_logical_writes) as total_logical_writes,
    (sum(total_logical_writes) / sum(execution_count) ) as avg_writes,
    sum(execution_count) as execution_count,
    count(1) as cache_count,
    sum(total_worker_time) as total_worker_time , 
    (sum(total_worker_time) / sum(execution_count) ) / 1000 as avg_time_ms
    from cte1 
    group by plan_handle
    order by ( (sum(total_logical_reads) / sum(execution_count) ) + (sum(total_logical_writes) / sum(execution_count) ) ) desc
    ) a 
    cross apply sys.dm_exec_sql_text(a.plan_handle) t
    order by (avg_reads + avg_writes) desc

    go

    当前数据库可能缺少的索引

    select d.*
    , s.avg_total_user_cost
    , s.avg_user_impact
    , s.last_user_seek
    ,s.unique_compiles
    from sys.dm_db_missing_index_group_stats s
    ,sys.dm_db_missing_index_groups g
    ,sys.dm_db_missing_index_details d
    where s.group_handle = g.index_group_handle

    and d.index_handle = g.index_handle
    order by s.avg_user_impact desc

    当前数据库没用到的索引

    select tb_name=object_name(a.object_id)
    ,idx_name=b.name
    ,last_user_update
    ,c.colid,c.keyno
    ,col_name=d.name
    into #tmp
    from sys.dm_db_index_usage_stats a left join sys.indexes b on a.object_id=b.object_id and a.index_id=b.index_id
    left join sys.sysindexkeys c on c.id=a.object_id and c.indid=a.index_id
    left join syscolumns d on d.id=c.id and d.colid=c.colid
    where database_id=db_id()
    and last_user_seek is null 
    and last_user_scan is null 
    and last_user_lookup is null
    and last_user_update is not null

    order by tb_name,idx_name,keyno


    select tb_name,idx_name,last_user_update
    ,keywords= stuff(
    (select ','+ col_name
    FROM #tmp 
    where tb_name=a.tb_name and idx_name=a.idx_name 
    order by tb_name,idx_name,keyno
    for xml path('') ) 
    ,1,1,'')
    from #tmp a 
    group by tb_name,idx_name,last_user_update

    drop table #tmp


    go

    当前数据库索引的使用率

    SELECT
    object_name(object_id) as table_name,
    (
    select name
    from sys.indexes
    where object_id = stats.object_id and index_id = stats.index_id
    ) as index_name,
    *
    FROM sys.dm_db_index_usage_stats as stats
    WHERE database_id = DB_ID()
    order by table_name

    当前数据库指定表的索引使用情况

    declare @table as nvarchar(100)
    set @table = 't_ire_candidate';

    SELECT
    (
    select name
    from sys.indexes
    where object_id = stats.object_id and index_id = stats.index_id
    ) as index_name,
    *
    FROM sys.dm_db_index_usage_stats as stats
    where object_id = object_id(@table)
    order by user_seeks, user_scans, user_lookups asc

    go

    -- 查找阻塞

    select 
    b.text, – sql执行计划
    b.dbid, 
    db_name(b.dbid) as dbname,
    a.session_id, – 执行会话id
    a.blocking_session_id, – 引起阻塞的会话id
    a.status,
    a.command,
    a.wait_time,
    a.wait_type,
    a.wait_resource,
    a.total_elapsed_time

    from sys.dm_exec_requests a
    cross apply sys.dm_exec_sql_text(a.sql_handle) b 
    where session_id in
    (
    select distinct request_session_id from sys.dm_tran_locks
    )

    通过 a.blocking_session_id 查看引起阻塞的进程,text是对应的sql脚本

  • 相关阅读:
    MVC3、如何应用EntityFramework 连接MySql 数据库 Kevin
    DEV EXPRESS Summary Footer 不显示 Kevin
    装饰模式 Kevin
    Dev 控件 GridControl 控件 二次绑定数据源的问题。 Kevin
    System.InvalidOperationException 异常 Kevin
    LINQ to XML Kevin
    代理模式——代码版“吊丝的故事” Kevin
    VS2012 中的设备 面板 Kevin
    maven 学习笔记(三)创建一个较复杂的 eclipse+android+maven 工程
    maven 学习笔记(一)eclipse+android+maven
  • 原文地址:https://www.cnblogs.com/gezifeiyang/p/3339062.html
Copyright © 2011-2022 走看看