SQLSERVER索引介绍

一、SQLSERVER索引类型?

1、聚集索引;

2、非聚集索引;

3、包含索引;

4、列存储索引;

5、无索引(堆表);

二、如何创建索引?

索引示例:

建表

create table t_test

(

id int identity(1,1),

name nvarchar(50),

[no] varchar(50),

[score] int,

created datetime

)

数据初始化

declare @i int = 1

while(@i <= 10000)

begin

insert into t_test(name,no,created,score)

select 'name_' + CAST(@i as varchar),'20190101-'  + CAST(@i as varchar),DATEADD(day,@i,'2019-01-01'),CAST( rand() * 100 as int)

set @i = @i + 1

End

堆表

sp_helpindex t_test

select * from sysindexes where id = OBJECT_ID('t_test') -- indid = 0 堆表,1 聚集索引,2 列存储索引,大于等于3 常规索引;

查看执行计划

select * from t_test where id = 5000

添加主键(聚集索引)

alter table t_test add constraint PK_t_test primary key(id)

查看执行计划

select * from t_test where id = 10

非聚集索引

create index ix_created on t_test(created)

select * from t_test where created between '2019-01-08' and '2019-01-15'

包含索引

create unique index uix_no on t_test(no) include(name)

查看和对比执行计划

select * from t_test where no = '20190101-100'

select name,no from t_test where no = '20190101-100'

排序字段加入索引

查看执行计划

select * from t_test where created between '2019-01-08' and '2019-02-01'

select * from t_test where created between '2019-01-08' and '2019-02-01'

order by score desc

创建索引

create index ix_created_score on t_test(created,score)

三、如何检查索引是否被用到?是否还有索引未创建?

1、当前指定表的索引使用情况

declare @table as nvarchar(100) = 'crm_customer';

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 desc, user_scans desc, user_lookups desc

2、当前表可能缺失的索引

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

and object_id = object_id('SCM_Loan_Loan')

order by s.avg_user_impact desc

字段说明:

avg_total_user_cost:可通过组中的索引减少的用户查询的平均成本

avg_user_impact:该值表示如果实现此缺失索引组,则查询成本将按此百分比平均下降。

unique_compiles:将从该缺失索引组受益的编译和重新编译数

四、SQL Trace查看实时数据

五、扩展一Profiler的常用功能

列筛选:ClientProcessID,Duration,Reads,TextData

六、扩展二通过DMV分别找出最耗时、最耗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_worker_time,a.avg_time_ms,a.execution_count,a.cache_count,

replace(replace(t.text,CHAR(10),' '),CHAR(13),' ') as text

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),' ') as text

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),' ') as text

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

技术分享会(二):SQLSERVER索引介绍的更多相关文章

  1. DockOne技术分享(二十):Docker三剑客之Swarm介绍

    [编者的话]Swarm项目是Docker公司发布三剑客中的一员,用来提供容器集群服务,目的是更好的帮助用户管理多个Docker Engine,方便用户使用,像使用Docker Engine一样使用容器 ...

  2. 【转】apache kafka技术分享系列(目录索引)

    转自:  http://blog.csdn.net/lizhitao/article/details/39499283   估计大神会不定期更新,所以还是访问这个链接看最新的目录list比较好 apa ...

  3. 腾讯技术分享:GIF动图技术详解及手机QQ动态表情压缩技术实践

    本文来自腾讯前端开发工程师“ wendygogogo”的技术分享,作者自评:“在Web前端摸爬滚打的码农一枚,对技术充满热情的菜鸟,致力为手Q的建设添砖加瓦.” 1.GIF格式的历史 GIF ( Gr ...

  4. 美团技术分享:深度解密美团的分布式ID生成算法

    本文来自美团技术团队“照东”的分享,原题<Leaf——美团点评分布式ID生成系统>,收录时有勘误.修订并重新排版,感谢原作者的分享. 1.引言 鉴于IM系统中聊天消息ID生成算法和生成策略 ...

  5. 关于启用 HTTPS 的一些经验分享(二)

    转载: 关于启用 HTTPS 的一些经验分享(二) 几天前,一位朋友问我:都说推荐用 Qualys SSL Labs 这个工具测试 SSL 安全性,为什么有些安全实力很强的大厂家评分也很低?我认为这个 ...

  6. 认识SQLServer索引以及单列索引和多列索引的不同

     一.索引的概念 索引的用途:我们对数据查询及处理速度已成为衡量应用系统成败的标准,而采用索引来加快数据处理速度通常是最普遍采用的优化方法. 索引是什么:数据库中的索引类似于一本书的目录,在一本书中使 ...

  7. [知识库分享系列] 二、Web(高性能Web站点建设)

    知识库分享系列: [知识库分享系列] 二..NET(ASP.NET) [知识库分享系列] 一.开篇 分享介绍 此知识库之所以为 Web 系列,因为和 .NET 没有完全的关系,其中的技术和实践实用于各 ...

  8. iOS开发技术分享(1)— iOS本地数据存储

    iOS开发技术分享(1)— iOS本地数据存储 前言: 我本是一名asp.net程序员,后来加入了iOS游戏开发队伍,到现在也有一年多的时间了.这一年来,每天都干到2.3点钟才睡觉,不为别的,只为了学 ...

  9. UWP 手绘视频创作工具技术分享系列 - 文字的解析和绘制

    本篇作为技术分享系列的第二篇,详细讲一下文字的解析和绘制,这部分功能的研究和最终实现由团队共同完成,目前还在寻找更理想的实现方式. 首先看一下文字绘制在手绘视频中的应用场景 文字是手绘视频中很重要的表 ...

随机推荐

  1. 小结:单调栈 & 单调队列

    概要: 对于维护信息具有单调性的性质或者问题可以转化为具有单调性质的模型的题,我们可以考虑用单调栈或单调队列. 技巧及注意: 技巧很多,只要能将问题转化为单调性问题,就好解决了. 当维护固定长度的单调 ...

  2. hdu 1232:畅通工程(数据结构,树,并查集)

    畅通工程 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  3. Oracle 10g 数据库手动创建步骤

    Oracle 数据库手动创建步骤 编写初始化参数文件 设置操作系统环境变量 创建实例 以管理员身份连接数据库 启动实例 create database 创建数据库 运行数据字典脚本 Oracle的网络 ...

  4. log4net写txt日志

    1.配置: <configSections>节点下添加: <section name="log4net" type="log4net.Config.Lo ...

  5. C++ 访问控制 public, protected, private, 友元

    1. 变量属性与继承之间的关系 #include <iostream> using namespace std; class A { public: int x; protected: i ...

  6. ArcGIS ArcMap 与 ArcServer关于Python的冲突

    一.问题描述 1.ArcMap 是32位,运行的Python也是32位: 2.ArcGIS Server 是64位,运行的Python是64位: 3.这样就导致注册表和环境变量起冲突,即如果Serve ...

  7. linux 命令集合收集(ubuntu)

    1.查看ubuntu版本号:cat /etc/issue  或者sudo lsb_release -a 2.查看内核版本信息:uname -a ,显示为4.4.0 x86_64版本内核

  8. 面试题思考:解释一下什么叫AOP(面向切面编程)

    这种在运行时,动态地将代码切入到类的指定方法.指定位置上的编程思想就是面向切面的编程. AOP是Spring提供的关键特性之一.AOP即面向切面编程,是OOP编程的有效补充. 使用AOP技术,可以将一 ...

  9. 第一次广搜!HDU1548--A Strange Lift

    一上来看见题目就用了深搜(因为只会深搜)果断内存超限(据说时间也会超限)无奈只好开始用广搜 其实广搜的思路和深搜有很多类似的地方 不过实现的过程中用到了队列 因此有点难以理解(好吧我个人认为) 这题是 ...

  10. 【BZOJ2287】【POJ Challenge】消失之物 背包动规

    [BZOJ2287][POJ Challenge]消失之物 Description ftiasch 有 N 个物品, 体积分别是 W1, W2, ..., WN. 由于她的疏忽, 第 i 个物品丢失了 ...