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

MSSQL
如何让新安装的MySQL数据库变得更安全
考虑SQL Server安全时所应注意的几个方面
快速解决SQL server 2005孤立用户问题
比较一下看看自己掌握了多少SQL快捷键
怎样在SQL Server 2005中用证书加密数据
讲解使用SQL Server升级顾问的详细步骤
讲解设计应用程序时避免阻塞的八个准则
配置SQL Server文件组对应不同的RAID系统
讲解数据库管理系统必须提供的基本服务
讲解SQL Server2005数据项的分拆与合并
SQL Server数据库动态交叉表的参考示例
SQL SERVER 2005中的同步复制技术
SQL Server查询速度慢的原因及优化方法
减少SQL Server死锁的方法
sql server 视图作用
扩展数据库系统选项实现更高的可扩展性
SQL Server开发过程中的的常见问题总结
对跨多个表格的数据组合时需要用到的SQL
SQL Server 2005 FOR XML嵌套查询使用详解
另类解读SQL Server中的DateTime数据类型

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


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