TOPN和SETROWCOUNTN哪个更快

来源:互联网 发布:伴游网站 源码 编辑:程序博客网 时间:2024/06/10 07:52
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
懒得翻译了,大意:
在有合适的索引的时候,TOPN和setrowcountn是一样快的。但是对于一个无序堆来说,TOPN更快。
原理自己看英文去。

Q.IsusingtheTOPNclausefasterthanusingSETROWCOUNTNtoreturnaspecificnumberofrowsfromaquery?

A.Withproperindexes,theTOPNclauseandSETROWCOUNTNstatementareequallyfast,butwithunsortedinputfromaheap,TOPNisfaster.Withunsortedinput,theTOPNoperatorusesasmallinternalsortedtemporarytableinwhichitreplacesonlythelastrow.Iftheinputisnearlysorted,theTOPNenginemustdeleteorinsertthelastrowonlyafewtimes.Nearlysortedmeansyou'redealingwithaheapwithorderedinsertsfortheinitialpopulationandwithoutmanyupdates,deletes,forwardingpointers,andsoonafterward.

Anearlysortedheapismoreefficienttosortthansortingahugetable.InatestthatusedTOPNtosortatablewiththesamenumberofrowsbutwithunorderedinserts,TOPNwasnotasefficientanymore.Usually,theI/Otimeisthesamebothwithanindexandwithout;however,withoutanindexmustdoacompletetablescan.Processortimeandelapsedtimeshowtheefficiencyofthenearlysortedheap.TheI/OtimeisthesamebecauseSQLServermustreadalltherowseitherway.

<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
原创粉丝点击