查到有这样一段话,很耗CPU资源:
![ContractedBlock.gif](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
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](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
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来做时间排序,也能省掉很多开销。