📄 top n 子句和set rowcount n之比较.htm
字号:
<DIV id=msviLocalToolbar>
<TABLE height=19 cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD id=msviHomePageLink noWrap><A
href="http://www.microsoft.com/china/sql/">SQL Server 主页</A></TD>
<TD><SPAN class=ltsep>|</SPAN></TD>
<TD class=lt0 onmouseover="mhHover('msviLocalToolbar', 2, 'lt1')"
onmouseout="mhHover('msviLocalToolbar', 2, 'lt0')" noWrap><A
href="http://www.microsoft.com/sql/wwide.asp">SQL Server 全球站点 </A></TD>
<TD width="100%"></TD></TR></TBODY></TABLE></DIV></DIV>
<TABLE style="TABLE-LAYOUT: fixed" height="100%" cellSpacing=0 cellPadding=0
width="100%" border=0>
<TBODY>
<TR>
<TD vAlign=top align=left width=180 height="100%">
<TABLE style="TABLE-LAYOUT: fixed" height="100%" cellSpacing=0
cellPadding=0 border=0>
<TBODY>
<TR>
<TD vAlign=top></TD></TR>
<TR>
<TD vAlign=top height="100%">
<TABLE class=flyoutMenu cellSpacing=0 cellPadding=2 width=180
border=0 xmlns:msxsl="urn:schemas-microsoft-com:xslt"
menudata="/china/sql/menu.xml">
<TBODY>
<TR>
<TD>
<TABLE cellSpacing=0 cellPadding=0 width=175 border=0>
<TBODY>
<TR>
<TD class=flyoutLink handle="evaluation"><A
href="http://www.microsoft.com/china/sql/evaluation/default.asp">产品信息</A>
</TD></TR></TBODY></TABLE>
<TABLE cellSpacing=0 cellPadding=0 width=175 border=0>
<TBODY>
<TR>
<TD class=flyoutLink handle="howtobuy"><A
href="http://www.microsoft.com/china/sql/howtobuy/choosing/default.asp">购买指南</A>
</TD></TR></TBODY></TABLE>
<TABLE cellSpacing=0 cellPadding=0 width=175 border=0>
<TBODY>
<TR>
<TD class=flyoutLink handle="techinfo"><A
href="http://www.microsoft.com/china/sql/techinfo/default.asp">技术资源</A>
</TD></TR></TBODY></TABLE>
<TABLE cellSpacing=0 cellPadding=0 width=175 border=0>
<TBODY>
<TR>
<TD class=flyoutHeading></TD></TR></TBODY></TABLE>
<TABLE cellSpacing=0 cellPadding=0 width=175 border=0>
<TBODY>
<TR>
<TD class=flyoutLink handle=""><A
href="http://www.microsoft.com/china/sql/downloads/default.asp">下载</A>
</TD></TR></TBODY></TABLE>
<TABLE cellSpacing=0 cellPadding=0 width=175 border=0>
<TBODY>
<TR>
<TD class=flyoutLink handle="partner"><A
href="http://www.microsoft.com/china/sql/partner/default.asp">SQL
增值伙伴社区</A> </TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE>
<TABLE class=flyoutMenu cellSpacing=0 cellPadding=2 width=180
border=0 xmlns:msxsl="urn:schemas-microsoft-com:xslt"
menudata="/china/sql/menu.xml">
<TBODY>
<TR>
<TD>
<TABLE cellSpacing=0 cellPadding=0 width=175 border=0>
<TBODY>
<TR>
<TD class=flyoutLink handle="family"><A
href="http://www.microsoft.com/china/servers/">Windows
Server System</A>
</TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE>
<TABLE class=adbar height="100%" cellSpacing=0 cellPadding=0
width="100%" border=0>
<TBODY>
<TR>
<TD> </TD></TR>
<TR>
<TD vAlign=top align=middle><A
href="http://www.microsoft.com/china/newsletter/case/technet.asp"><IMG
src="TOP N 子句和SET ROWCOUNT N之比较.files/TechNet_newsletter.gif"
border=0></A> </TD></TR>
<TR>
<TD> </TD></TR>
<TR>
<TD
height="100%"> </TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE></TD>
<TD vAlign=top align=left><!-- Begin Content -->
<TABLE cellPadding=20 width="100%">
<TBODY>
<TR>
<TD>
<TABLE cellSpacing=0 cellPadding=0 width="100%" border=0>
<TBODY>
<TR>
<TD vAlign=top width="100%"><FONT style="COLOR: #0066cc"><A
href="http://www.microsoft.com/china/sql/using/default.asp">使用SQL
Server</A> > <A
href="http://www.microsoft.com/china/sql/using/tips/default.asp">窍门和技巧</A> </FONT>
<BR><FONT size=4><B>TOPN 子句与SET ROWCOUNTN 之对比</B></FONT>
<BR>2001年5月21日
<P><B>问:为了从查询中返回指定数量的行,使用 <FONT face=arial>TOPN 子句比使用<FONT
face=arial>SET ROWCOUNTN</FONT> 语句要快吗? </FONT></B>
<P><B>答:</B>在正确进行了索引的情况下,TOP N 子句和SET ROWCOUNT N
语句是一样快的,但是如果数据未经过排序,TOP N 要快一些。在输入未排序的情况下,TOP N
操作时使用一个经过排序的小的中间临时表,而且操作时仅仅替换该表的最后一行。如果输入是近似排序的,TOP N
引擎必须删除或插入最后行,但只需几次操作即可。近似排序意味着您正在处理的堆集在初始构建时可进行有序的插入操作,并且不需要进行很多的更新、删除、向前移动指针等操作。
</FONT>
<P>排序一个近似排序的堆集比排序一个巨大的表要更有效率。在一次测试中,使用TOP N
来对一个由无序插入操作构建的并且含有同样的行数的表进行排序,发现TOP N
的效率也不高。通常,在进行过索引和未进行过索引的情况下,I/O时间都是一样的;但是如果没有进行过索引,SQL Server
必须要进行一次全表扫描。处理器时间和实耗时间说明近似排序的堆集要更有效率一些。但I/O时间是相同的,因为不管怎样SQL
Server都要读取所有的行。
<BR></P></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><!-- END TOTAL PAGE CONTENT --></TD></TR></TBODY></TABLE><BR
style="OVERFLOW: hidden; LINE-HEIGHT: 1px" clear=all>
<TABLE id=msviFooter cellSpacing=0 cellPadding=0 width="100%">
<TBODY>
<TR vAlign=bottom>
<TD id=msviFooter2
style="FILTER: progid:DXImageTransform.Microsoft.Gradient(startColorStr='#FFFFFF', endColorStr='#ADADAD', gradientType='1')">
<DIV id=msviLocalFooter><NOBR><A
href="https://profile.microsoft.com/RegSysProfileCenter/Infodefault.aspx?LCID=2052">个人信息中心</A>
|</NOBR><WBR><NOBR><A
href="http://register.microsoft.com/contactus30/contactus.asp?domain=generic&lcid=2052"
target=_parent>与我们联系</A> |</NOBR><WBR><NOBR><A
href="javascript:window.navigate('mailto:?subject=一个来自 Microsoft 的网页链接 &amp;body=这是一篇您感兴趣的来自微软 SQL Server 的文章 : '+this.window.location);"
target=_parent>发送本页给朋友</A></NOBR></DIV>
<DIV id=msviGlobalFooter><SPAN dir=ltr>©2004 Microsoft Corporation. 版权所有.
</SPAN><NOBR><A
href="http://www.microsoft.com/china/misc/cpyright.htm"
target=_parent>保留所有权利</A> |</NOBR><WBR><NOBR><A
href="http://www.microsoft.com/library/toolbar/3.0/trademarks/zh-cn.mspx">商标</A>
|</NOBR><WBR><NOBR><A
href="http://www.microsoft.com/info/cn/privacy.mspx">隐私权声明</A></NOBR></DIV></TD>
<TD width=105 bgColor=#adadad><IMG title="" height=29 alt=Microsoft
src="F:\程序书籍\SQL_资料\TOP N 子句和SET ROWCOUNT N之比较.files\text(1).jpg"
width=105 border=0></TD></TR></TBODY></TABLE>
<SCRIPT language=javascript>var msviFooter2;if (document.getElementById){msviFooter2 = document.getElementById("msviFooter2");msviFooter2.style.filter = "";}</SCRIPT>
<TABLE height=58 cellSpacing=0 cellPadding=0 width="100%">
<TBODY>
<TR vAlign=top>
<TD>
<DIV style="WIDTH: 200px"></DIV></TD>
<TD
style="FILTER: progid:DXImageTransform.Microsoft.Gradient(startColorStr='#FFFFFF', endColorStr='#ADADAD', gradientType='1')"
width="100%"></TD></TR></TBODY></TABLE>
<SCRIPT language=javascript>footerjs(document);</SCRIPT>
</BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -