简体   繁体   English

mssql查询顺序每次都在变化

[英]Mssql query sequence is changing every time

For Erp version upgrade we copied our database and restored to new one.对于 Erp 版本升级,我们复制了我们的数据库并恢复到新的。 We altered tables.我们改变了桌子。 (Added new columns or changed Width etc. We dropped indexes and recreated). (添加新列或更改宽度等。我们删除索引并重新创建)。 After all we tested new database, caught a situation like this.毕竟我们测试了新数据库,遇到了这样的情况。

we have a query like我们有一个类似的查询

select top 5 * from table. 

old database: when we run the query continuously, result order not changing旧数据库:当我们连续运行查询时,结果顺序不变

new database: when we run, order sequence of results changing.新数据库:当我们运行时,结果的顺序发生变化。

I think there is a clustered index issue but how can I determine and solve it I don't know.我认为存在聚集索引问题,但我不知道如何确定和解决它。 Any help?有什么帮助吗?

order is not guaranteed unless you use order by.除非您使用 order by,否则无法保证订单。

Old database may have clustered index created on table.旧数据库可能在表上创建了聚集索引。 Please check it.请检查一下。 But even clustered index does not always guarantee ordering.但即使是聚集索引也不能总是保证排序。

If you don't use the ORDER BY statement after the TOP statement the database engine does not guarantees the order of the resultset.如果在 TOP 语句之后不使用 ORDER BY 语句,则数据库引擎不保证结果集的顺序。 SQL Server wants to return qualified rows quickly in some cases and this mechanism called Row Goals and most probably your query execution plan uses this mechanism. SQL 服务器希望在某些情况下快速返回合格的行,这种机制称为行目标,很可能您的查询执行计划使用这种机制。 On the other hand, the clustered index operator includes an ordered attribute and this shows us the retrieved rows are fetched in the ordered manner or not.另一方面,聚集索引运算符包含一个有序属性,这向我们展示了检索到的行是否以有序方式获取。 在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM