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

 

 
   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

 

分析是这样的:

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

 

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

 

 
   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

 

执行计划变为:

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

 

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

相关文章:

  • 2021-04-22
  • 2021-11-12
  • 2022-02-21
  • 2022-12-23
  • 2021-07-04
  • 2021-09-13
  • 2021-08-12
  • 2021-10-08
猜你喜欢
  • 2021-12-02
  • 2021-12-06
  • 2021-06-04
  • 2021-10-24
  • 2022-12-23
  • 2021-05-24
  • 2021-04-18
相关资源
相似解决方案