当前位置: 首页 > 图文教程 > 数据库 > MSSQL > TOP N 和SET ROWCOUNT N 哪个更快?

MSSQL
SQL0290N表空间状态问题:停顿的独占处理
实例讲解启动mysql server失败的解决方法
SQL Server 2005数据转换服务设计问题集锦
不能安装SQL Server 2005的问题及解决方法
SQL中Groupby和Having的使用方法
SQL Server中Update的用法
MSSQL中部分字段重复数据的删除方法
mssql 大小写区分方法
Sql server中时间查询的一个比较快的语句
机器异常关闭重起后出现ora-19809错误
快速解决日志文件满造成的无法写入问题
启用数据库复制时为什么会出现18483错误
实例讲解Ora-12514和Ora-12514解决方法
实例讲解MSDB数据库置疑状态的解决方法
实例讲解SQL查询连续号码段的巧妙解法
用一个案例讲解应用程序越来越慢的原因
一则DB2数据库重定向表空间的恢复案例
SCN不一致将会导致ORA-00600 2662错误
Vista下安装SQL Sever 2005报错的解决办法
用一条SQL实现:一行多个字段数据的最大值

MSSQL 中的 TOP N 和SET ROWCOUNT N 哪个更快?


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-10-30   浏览: 128 ::
收藏到网摘: n/a

  懒得翻译了,大意:
在有合适的索引的时候,Top n和set rowcount n是一样快的。但是对于一个无序堆来说,top n更快。
原理自己看英文去。

Q. Is using the TOP N clause faster than using SET ROWCOUNT N to return a specific number of rows from a query?

A. With proper indexes, the TOP N clause and SET ROWCOUNT N statement are equally fast, but with unsorted input from a heap, TOP N is faster. With unsorted input, the TOP N operator uses a small internal sorted temporary table in which it replaces only the last row. If the input is nearly sorted, the TOP N engine must delete or insert the last row only a few times. Nearly sorted means you're dealing with a heap with ordered inserts for the initial population and without many updates, deletes, forwarding pointers, and so on afterward.

A nearly sorted heap is more efficient to sort than sorting a huge table. In a test that used TOP N to sort a table with the same number of rows but with unordered inserts, TOP N was not as efficient anymore. Usually, the I/O time is the same both with an index and without; however, without an index SQL Server must do a complete table scan. Processor time and elapsed time show the efficiency of the nearly sorted heap. The I/O time is the same because SQL Server must read all the rows either way.