Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 使用ORDER BY执行SELECT TOP查询-我应该使用索引还是“排序视图”来提高性能_Sql_Sql Server_Indexing_Sql Order By - Fatal编程技术网

Sql 使用ORDER BY执行SELECT TOP查询-我应该使用索引还是“排序视图”来提高性能

Sql 使用ORDER BY执行SELECT TOP查询-我应该使用索引还是“排序视图”来提高性能,sql,sql-server,indexing,sql-order-by,Sql,Sql Server,Indexing,Sql Order By,存储过程的此查询部分将大量执行: SELECT TOP (@numRecords) BlogPost.postId,BlogPost.creationDate, BlogPost.header,BlogPost.markedupContentAbstract FROM dbo.BlogPost ORDER BY BlogPost.creationDate DESC 我应该在BlogPost表的“creationDate”字段中添加索引吗? 我是否应该查看BlogPo

存储过程的此查询部分将大量执行:

SELECT TOP (@numRecords)  BlogPost.postId,BlogPost.creationDate,
        BlogPost.header,BlogPost.markedupContentAbstract
     FROM dbo.BlogPost ORDER BY BlogPost.creationDate DESC
我应该在BlogPost表的“creationDate”字段中添加索引吗? 我是否应该查看BlogPost记录的排序位置,然后从中选择TOP


详细信息:使用SQL Server 2008。

创建日期索引使其按降序排列是最佳路径

为清楚起见:

CREATE NONCLUSTERED INDEX [IX_BlogPost_CreationDate] 
ON BlogPost
( CreationDate DESC )

仅仅创建一个视图不会带来任何好处。您可以查看索引视图,但我不记得是否可以在视图上按顺序排序,然后在其上放置索引。我不推荐这种方法,因为你只是从一张桌子上拉


您可以添加索引,这样可以避免将要执行的排序操作。

除非在该视图中添加顶部,否则无法创建具有ORDER BY的视图

您可以在creationDate上创建索引,并覆盖选择列表中使用的所有其他列:


DESC在这里有什么区别?在索引中包含返回的列可能也会有好处,但要以写操作为代价performance@Quassnoi当前位置他希望首先列出最近发布的帖子,如果使用了索引的默认顺序,则在查询数据时仍必须指定顺序依据。@Tony:索引可用于双向排序。在任何情况下都需要指定ORDER BY。显式指定索引方向的唯一潜在好处是避免对已加载到内存中的后续行的页面进行“回溯”。始终向前阅读可能会带来一些微小的性能优势。磁盘访问没有任何好处,因为“下一页”的物理位置无法保证。最好考虑覆盖索引的写入开销是否是合理的。
CREATE INDEX ix_blogpost_creationdate__other ON BlogPost (creationDate) INCLUDE (postId, header, markedupContentAbstract)