简体   繁体   中英

Mssql query sequence is changing every time

For Erp version upgrade we copied our database and restored to new one. 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.

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. 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. 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. 在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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