sql优化实战:排序字段——到底是time还是ID

查到有这样一段话,很耗CPU资源:

 

ContractedBlock.gif ExpandedBlockStart.gif Code
set statistics io on
set statistics time on 
   
SELECT TOP 10 FeedBackID,UserID,ContentID,[Content],  
   Time,AddType,IP  
FROM   CYZoneFeedBack  
   
where contentid in (select articleid from cyzonearticle where userid=@user and delflag=0 and publishtype<>'b')
    
and CYZoneFeedBack.DelFlag =0 order by CYZoneFeedBack.Time desc

 

分析是这样的:

 

原来是排序造成了这么多开销。罪魁祸首在于 order by CYZoneFeedBack.Time 这句话,后改成:

 

ContractedBlock.gif ExpandedBlockStart.gif Code
set statistics io on
set statistics time on 
   
SELECT TOP 10 FeedBackID,UserID,ContentID,[Content],  
   Time,AddType,IP  
FROM   CYZoneFeedBack  
   
where contentid in (select articleid from cyzonearticle where userid=107and delflag=0 and publishtype<>'b')
    
and CYZoneFeedBack.DelFlag =0 order by CYZoneFeedBack.feedbackid desc

 

执行计划变为:

 

很明显省掉了排序的操作。有时候,排序和时间是有相关性的,而聚集索引,没有建在时间上,会导致排序成本的增加,恰当的利用自增ID来做时间排序,也能省掉很多开销。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值