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

MSSQL
优化SQL Server索引的小技巧
SQL Server的空值处理策略
Windows上的MySQL UDF开发
MS SQLSERVER中如何快速获取表的记录总数
MS SQLSERVER 中如何得到表的创建语句
使用索引调节向导调整应用程序的性能
使用查询分析器调整SQL服务器脚本
SQL Server到Oracle连接服务器的实现
数据库查询结果的动态排序(6)
数据库查询结果的动态排序(7)
保持Oracle数据库优良性能的若干诀窍
数据库管理员制胜之宝
在Linux下访问MS SQL Server数据库
关于如何在查询结果中添加自动编号
如何提取除最新十条记录之外的所有记录?
一个有关DISTINCT的问题解答
关于MSSQL Server中DATETIME类型数据的处理
TOP N 和SET ROWCOUNT N 哪个更快?
金额阿拉伯数字转换为中文的存储过程
谈谈数据从sql server数据库导入mysql数据库的体验

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


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