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

MSSQL
详解SQL Server中数据库快照工作原理
VB应用程序访问SQL Server的常用方法
SQL Server特殊磁带备份及恢复设计
基于SQL Server的C/S数据库应用系统
如何实现SQL Server 2005快速Web分页
关于SQL Server数据库中转储设备分析
从外到内提高SQL Server数据库性能
准备SQL Server 2008透明数据加密
快速升级MySQL系统表
解析:在SQL Server下数据库链接的使用
细化解析:SQL Server 2005 数据库镜像
轻松接触SQL Server 2000实例的命名规则
SQL提供的进行数据传输的实用程序—BCP
SQL Server 2000 作数据库服务器的优点
解析:正确的理解SQL Server和XML支持
轻松了解数据库计算机的概念和发展方向
解析SQL Server数据体系和应用程序逻辑
SQL进行排序、分组、统计的10个新技巧
教你怎样打造SQL Server2000的安全策略
解析SQL Server 2005 溢用之:合并列值

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


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