SQL Server中的联合主键、聚集索引、非聚集索引

我们都知道在一个表中当需要2列以上才能确定记录的唯一性的时候,就需要用到联合主键,当建立联合主键以后,在查询数据的时候性能就会有很大的提升,不过并不是对联合主键的任何列单独查询的时候性能都会提升,但我们依然可以通过对联合主键中的首列除外的其他列建立非聚集索引来提高性能。
本文将对联合主键、聚集索引、非聚集索引对查询性能的影响举例说明。
步骤一,建立一个测试表,并且插入350万条以上的数据。
 
/*创建测试数据表*/
create table MyTestTable
(
id varchar(10)not null,
parent varchar(40) not null,
addtime datetime default(getdate()),
intcolumn int default(10),
bitcolumn bit default(1)
)
go
/*添加万条随机字符串测试数据耗时分钟*/
declare @count int=3557643
declare @i int =0
declare @id varchar(10),@parent varchar(40)
while(@i<@count)
begin
select @id=left(newid(),10)
if(@i % 20=0)
begin
select @parent=left(newid(),40)
end
insert MyTestTable(id,parent) values(@id,@parent)
select @[email protected]+1
end
go
 
步骤二,不建立任何索引查询测试
/*未建立索引时的查询*/
declare @beginTime datetime =getdate()
declare @elapsedSecond int =0
select * from MyTestTable where parent=‘DD7D9F34-3A9C-43CA-836B-F2BABD78CE70‘ and id=‘103ACE5C-7‘
select @elapsedSecond=DATEDIFF(MICROSECOND,@beginTime,GETDATE())
print ‘未建立索引时查找数据消耗微秒数‘
print @elapsedSecond
select @beginTime=GETDATE()
select * from MyTestTable where parent=‘F535C18F-BD48-4D45-88DF-9653BB9B422D‘
select @elapsedSecond=DATEDIFF(MICROSECOND,@beginTime,GETDATE())
print ‘未建立索引时查找第二列数据消耗微秒数‘
print @elapsedSecond
--(1 row(s) affected)
--未建立索引时查找数据消耗微秒数
--530000
--(20 row(s) affected)
--未建立索引时查找第二列数据消耗微秒数
--500000
从执行结果我们可以看出,当没有索引的时候,SQL Server会遍历整个表,因此需要很长的时间。
步骤三,建立联合主键(会自动创建聚集索引)并查询测试
go
/*建立联合主键*/
alter table MyTestTable add constraint PK_id_parent primary key(id asc,parent asc)
/*建立索引后的查询*/
declare @beginTime datetime =getdate()
declare @elapsedSecond int =0
select * from MyTestTable where parent=‘DD7D9F34-3A9C-43CA-836B-F2BABD78CE70‘ and id=‘103ACE5C-7‘
select @elapsedSecond=DATEDIFF(MICROSECOND,@beginTime,GETDATE())
print ‘建立索引时查找数据消耗微秒数‘
print @elapsedSecond
 
select @beginTime=GETDATE()
select * from MyTestTable where parent=‘F535C18F-BD48-4D45-88DF-9653BB9B422D‘
select @elapsedSecond=DATEDIFF(MICROSECOND,@beginTime,GETDATE())
print ‘建立索引后查找第二列数据消耗微秒数‘
print @elapsedSecond
 
go
--(1 row(s) affected)
--建立索引时查找数据消耗微秒数
--0
 
--(20 row(s) affected)
--建立索引后查找第二列数据消耗微秒数
--500000
从上面看出,建立联合主键后,查询第一列或者同时查询两列(and关系)速度会非常的快,小于1微妙,但查询联合主键的第二列的时候却特别的慢,因为无法通过索引查询。
步骤四,给联合主键的第二列建立非聚集索引,并且测试
go
/*给第二列创建非聚集索引*/
create index index_parent on MyTestTable(parent asc)
declare @beginTime datetime =getdate()
declare @elapsedSecond int =0
select * from MyTestTable where parent=‘DD7D9F34-3A9C-43CA-836B-F2BABD78CE70‘ and id=‘103ACE5C-7‘
select @elapsedSecond=DATEDIFF(MICROSECOND,@beginTime,GETDATE())
print ‘为第二列建立索引时查找数据消耗微秒数‘
print @elapsedSecond
 
select @beginTime=GETDATE()
select * from MyTestTable where parent=‘9A75DC47-DDF7-4922-9179-E87B91FE3921‘
select @elapsedSecond=DATEDIFF(MICROSECOND,@beginTime,GETDATE())
print ‘为第二列建立索引后查找第二列数据消耗微秒数‘
print @elapsedSecond
 
--(1 row(s) affected)
--为第二列建立索引时查找数据消耗微秒数
--0
 
--(20 row(s) affected)
--为第二列建立索引后查找第二列数据消耗微秒数
--0
从执行结果可以看出,建立索引后,查询第二列的速度也非常的快了。
总结
一般情况下,对于一个表T,联合主键(A,B),下列情况的查询时,SQL Server 可以从索引中查询,速度较快:
select * from T where A=Value and B=Value
select * from T where B=Value and A=Value 
select * from T where A=Value
下面的查询不会经过索引,速度会比较的慢
select * from T where A=Value or B=Value
select * from T where B=Value

转载:http://www.cnblogs.com/fjchenqian/archive/2011/09/22/2184656.html

时间: 2024-08-01 19:59:51

SQL Server中的联合主键、聚集索引、非聚集索引的相关文章

sql的注释和联合主键

1.添加注释的sql comment on table tableName is '表注释'; comment on  column tableName.columnName is '字段注释说明'; comment on table t_cif_ehrlog is 'ehr同步日志表';comment on column t_cif_ehrlog.id is '流水号';comment on column t_cif_ehrlog.syn_date is '同步年月(yyyy-mm)';com

SQL Server 创建表 添加主键 添加列常用SQL语句

--删除主键 alter table 表名 drop constraint 主键名 --添加主键 alter table 表名 add constraint 主键名 primary key(字段名1,字段名2……) --添加非聚集索引的主键 alter table 表名 add constraint 主键名 primary key NONCLUSTERED(字段名1,字段名2……) 新建表: create table [表名] ( [自动编号字段] int IDENTITY (1,1) PRIM

SQL Server 创建表 添加主键 添加列常用SQL语句【转】

--删除主键alter table 表名 drop constraint 主键名--添加主键alter table 表名 add constraint 主键名 primary key(字段名1,字段名2……)--添加非聚集索引的主键alter table 表名 add constraint 主键名 primary key NONCLUSTERED(字段名1,字段名2……) 新建表: create table [表名] ( [自动编号字段] int IDENTITY (1,1) PRIMARY K

JPA学习---第十二节:JPA中的联合主键

1.定义实体类,代码如下: (1).将联合主键放到一个类中,代码如下: package learn.jpa.entity; import java.io.Serializable; import javax.persistence.Column; import javax.persistence.Embeddable; /** * * 1.必须要有无擦得构造函数 * 2.必须要实现序列接口 * 3.必须重写 equals() 和 hashCode() 方法 * @Embeddable 告诉 jp

Hibernate 查询排序与联合主键映射

1.查询排序 (1)数据库排序(推荐) <map order-by="name ase" > <!--name的升序,降序desc--> session.createQuery(" ").uniqueResult() //返回唯一的对象,前台对象只有一个 <set order-by="name asc"> (2)内存排序 <set sort="natural" > sort属性值

hybris impex导入 联合主键对象

hybris impex 中的联合主键 对象插入语句 INSERT_UPDATE SomeStatus;code[unique=true];key[unique=true] ;01;approvedStatus ;02;approvedStatus ;01;sendStatus ;02;sendStatus

SQL Server中建立外键的方法

在SQL中建立外键约束,可以级联查询表中的数据,在C#代码生成器中,也能根据外键关系生成相应的外键表数据模型.外键也可防止删除有外键关系的记录,一定程度上保护了数据的安全性. 步骤: 1.要建立外键关系,首先要保证用来建立外键关系的列具有唯一性,即具有 UNIQUE 约束通常是某表的主键作为另外一个表的外键 2.打开数据库表,找到要建立外键的表.并确保其中要建立外键关系的列与主键表中的数据类型完全一致 3.在要建立外键关系的表中,在任意列上右击,选择[关系] 4.在外键关系对话框中,点击左下角的

SQL联合主键 查重

2014年最后一天,今天在给数据库导入数据的时候,遇到一个问题,就是联合主键去重. 事情是这样的,现有一个表M,我想找个表中导入了许多数据,并需要将字段A(int)和B(int)联合设置为主键. 但是在设置主键时,告知我有重复的内容,无法设置主键. 正常情况下,这两个字段的组合是唯一的,不应该重复,为了找到重复数据,看看是什么方面出了问题. 采用distinct方式 但是发现 select count(distinct name, id) from A 这句话在SQL里不适用,http://ww

Hibernate 中 联合主键映射 组合关系映射 大对象映射(或者说文本大对象,二进制数据大对象)

Clob:文本大对象,最长4G Blob:二进制数据大对象,最长4G util: public class HibUtil { private static SessionFactory sessionFactory; static{ //获取配置信息 hibernate.cfg.xml Configuration configuration = new Configuration().configure(); //创建一个 ServiceRegistry的实例 //首先获得其标准建造器,此处用