用与不用聚集索引的区别 sql用例

对数据表创建了聚集索引后,当向表中插入数据时,数据库会对数据按照索引进行重新排序存储。 所以,对于频繁执行insert操作的表,在对聚集索引的创建上,需要做权衡,有时需考虑不建立聚集索引。

如下用例说明了使用与不使用聚集索引时,数据的存储情况。

if not object_id('test_no_cx') is null
drop table test_no_cx
if not object_id('test_has_cx') is null
drop table test_has_cx

--创建一个不含索引的表test_no_cx
create table test_no_cx(id int, name varchar(20), createtime datetime default(getdate()))


--插入若干条记录(注意:这里故意打乱id的顺序)
insert test_no_cx(id, name) values(100,'100''s value')

insert test_no_cx(id, name) values(5,'5''s value')
insert test_no_cx(id, name) values(90,'90''s value')

--创建含索引的表test_has_cx
create table test_has_cx(id int, name varchar(20), createtime datetime default(getdate()))

create clustered index ix_test_has_cx on test_has_cx(id)

--将test_no_cx中的记录批量插入到test_has_cx中
insert test_has_cx select * from test_no_cx


--此时比较两表的记录如下:


--向两表中插入一条同样的记录
insert test_no_cx(id, name) values(50,'50''s value')

insert test_has_cx(id, name) values(50,'50''s value')

--此时比较两表的记录如下:

 注:以上sql可直接运行。

 

转载于:https://www.cnblogs.com/buguge/archive/2011/11/17/2253057.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值