sql server execution plan - 执行计划的诡异 ( 二 )

1) 创建这次实验的表结构

create table dbo.fctOrderSales
( ordermonthkey int not null, orderdatekey int not null, orderkey int not null, 
customerkey int not null, currencykey int not null, partitionid int,
salesamount bigint, salesprofit numeric(9,2))
go 

alter table dbo.fctordersales
add constraint PK_FctOrderSales primary key clustered (ordermonthkey,orderdatekey,
orderkey,customerkey,currencykey)
go

create index IX_FctorderSales_Partitionid on dbo.FctOrderSales(partitionid)
go

2) 给表灌 6000 万条数据, 开 10 个 session, 来灌表

declare @partitionid int = 0 
declare @increaseNumber int = 1 + @partitionid * 6000000
declare @maxbucketnumber int =(@partitionid + 1)*6000000
declare @orderdatekey int = convert (int, convert(varchar,getdate(),112))
declare @ordermonthkey int = convert(int, left(convert(varchar,@orderdatekey),6))
declare @orderkey int = 1
declare @customerkey int = 1
declare @currencykey int  = 2

while ( @increasenumber <= @maxbucketnumber)
begin 
 insert into dbo.fctordersales(ordermonthkey,orderdatekey,orderkey,customerkey,currencykey,partitionid,
 salesamount,salesprofit) values(@ordermonthkey,@orderdatekey,@orderkey,@customerkey,@currencykey,
 @partitionid,@increasenumber,@increasenumber)
 set @orderdatekey = convert (int, convert(varchar,dateadd(dd,@increasenumber,getdate()),112))
 set @ordermonthkey = convert(int, left(convert(varchar,@orderdatekey),6))
 set @increasenumber = @increasenumber + 1
 set @orderkey = @increasenumber
end

3) 灌表的时候顺便查下 table 与 index 的同步情况

select * from dbo.fctordersales with(nolock) 
where partitionid = 1 

---------
41674 rows

select count(1) from dbo.fctordersales with(nolock)
where partitionid = 1 
group by partitionid 

--------
48963 

两者的 execution plan 分别是

这里写图片描述

这里写图片描述

不同的执行计划带来的数据集不同步的情况。 在这里,的确是 index 先于 table insert 了记录,至于为什么会有如此差异,得看看 table, index 的创建方式。 下回分解。那么删除时候, table, index 各自的更新情况呢?

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

dbLenis

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值