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

MSSQL
修改自定义数据类型精度
SQL Server 2000之日志传送功能(1)
SQL Server 2000之日志传送功能(2)
建立安全的MSSQL SERVER启动账号
用T-SQL导入文件数据到SQL Server
SQL Server CHARINDEX和PATINDEX详解
如何同时对多个表或列操作
让数据库产生一张详细的日历表
用DTS导入多个文件数据到SQL Server中
SQL SERVER2000备份和恢复存储过程
将Excel中的数据导入到SQL Server 2000数据库中
SQL Server数据库文件恢复技术
SQL邮件自动应答
SQL Sever 2000的系统数据库和索引
SQLServer2000中UNION与UNION ALL的区别
用SQL语句来建立跟踪的问题
关于SQL Server 2000的安全配置
备份服务器端SQL SERVER数据库至本地目录
Sqlserver中的一些技巧
在SQL Server存储过程中执行DTS包

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-10-30   浏览: 92 ::
收藏到网摘: 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.