MS SQL 查询数据库中所有索引以及对应的表字段 SQL Server Profiler性能跟踪

新项目上线,需要对库里的表进行相关索引检查。这儿首先需要查看一下库里的表那些有索引,然后用SQL Server Profiler进行跟踪,检查SQL语句性能,查询频率等。

查询库里所有索引相关信息这儿给出一个SQL,使用了CTE查询:

with temp as(
    select  a.name as tabname,a.object_id
    ,h.name as indexName,h.index_id,ic.column_id,c.name colName
    from  sys.objects    as  a
    right join sys.indexes  as h  on  a.object_id=h.object_id
    left join sys.index_columns ic on h.index_id=ic.index_id and ic.object_id=a.object_id
    left join sys.columns c on ic.column_id=c.column_id and c.object_id=a.object_id
    where  a.type<>'s' and a.type='U'
    --and isnull(h.name,'')='' --没有索引的表
    and a.is_ms_shipped<>1 --排除 dtproperties
)
 
select distinct temp.tabname,indexName
,stuff((
        select ',' + tc.name from sys.columns tc
        left join sys.index_columns ic on ic.column_id=tc.column_id and ic.object_id=temp.object_id
        where tc.object_id=temp.object_id and ic.index_id=temp.index_id and tc.column_id in (
            select column_id from temp where object_id=tc.object_id and ic.index_id=temp.index_id
            ) for xml path('')
    ),1,1,''
) columnName
from temp
order by temp.indexName


性能跟踪是需要花费一些时间的,首先建立跟踪:


建立跟踪后对站点相关功能进行浏览测试,最后根据跟踪结果(可使用sql server profiler打开保存的.trc文件)。如图:


对SQL耗时以及调用频率高的SQL进行优化。索引不是什么字段什么情况都加的 具体的相关SQL,索引优化参考:http://www.cnblogs.com/zengxiangzhan/archive/2009/12/04/1617186.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值