Delay sorting until you're actually
about to use the data.
If you end up sorting differently
than the default sort specified by the ORDER BY in the view, it comes
as an expensive performance hit. Witness a stored procedure that was
breaking a unit test due to a SqlClient.SqlTimeout Exception (took 2.5
minutes to run). Removing the ORDER BY statements from the underlying
views brought execution time down to 6 seconds.
简单的翻译如下:
不要在视图里面使用ORDER BY 语句,把排序延迟到你实际需要使用数据的时候。
对此,《Microsoft SQL Server 2005 技术内幕:T-SQL 程序设计》 一书对此进行了详细阐述,可见:http://book.youkuaiyun.com/bookfiles/441/10044115644.shtml
SQL Server 2005联机丛书有一段对该行为的描述:“在视图、内联函数、派生表或子查询的定义中使用 ORDER
BY 时,子句只用于确定 TOP 子句返回的行。ORDER BY 不保证在查询这些构造时得到有序结果,除非在查询本身中也指定了 ORDER
BY。”
通过
UPDATE STATISTICS IcStockProInEntry pk_IcStockProInEntry
with full scan 或者ALTER INDEX PK_IcStockProInEntry ON IcStockProInEntry
REBUILD 重建索引就可以保证排序的正确。