简体   繁体   English

当我在SQL Server 2005表中插入行时,将保留顺序吗?

[英]When I Insert Rows into a SQL Server 2005 Table will the order be preserved?

When I select a series of rows from a sql server 2005 table using an "order by" clause and then insert them into a different (and empty) sql server 2005 table can I count on the rows staying in the same order. 当我使用“ order by”子句从sql server 2005表中选择一系列行,然后将它们插入到不同(且为空)的sql server 2005表中时,我可以指望保持相同顺序的行。

The reason I am asking is that I want to manipulate the rows using ADO.net. 我问的原因是我想使用ADO.net操作行。 I want the rows in a specific order to improve the speed of the calculations I do using ADO.net. 我希望按特定顺序排列行,以提高使用ADO.net进行计算的速度。

Thanks for the help. 谢谢您的帮助。

Relational databases work on unordered sets. 关系数据库适用于无序集合。 These sets do not have an order unless you explicitly apply an order to them. 这些集合没有顺序,除非您对它们明确应用了顺序。 If you want the same order in the second table as when you selected from the first, then you need to apply the equivalent ORDER BY clause. 如果希望第二张表中的顺序与从第一张表中选择的顺序相同,则需要应用等效的ORDER BY子句。

While the order that you insert will be preserved, it can't be depended on - a number of events in SQL Server can re-order a table, including adding a clustered index, or some other user doing a number of things (for example: insert into # select * where something, truncate table, insert into table select * from #, all without your knowledge). 尽管插入的顺序将被保留,但不能依赖于它-SQL Server中的许多事件都可以对表进行重新排序,包括添加聚簇索引,或者其他一些用户在做很多事情(例如:将#select *插入到#select *处,截断表,然后从#中将select * *插入到表中,所有操作都不知道。

As John stated, the only way to ensure a specific order is to specify the ORDER BY clause on some identifier. 如John所述,确保特定顺序的唯一方法是在某些标识符上指定ORDER BY子句。

Just be mindfull that Primary Keys default to being clustered indexes. 请注意,主键默认为聚簇索引。 This clustered index used as the physical order that the data is stored down on the disk drive. 该聚集索引用作数据在磁盘驱动器上向下存储的物理顺序。

If you want to improve the speed and have complete control over the entire system (and understand all the repercussions of doing this change) then you can either look at indexing the order by clause or (in extreme cases only) apply a clusted index on those ordered fields. 如果您想提高速度并完全控制整个系统(并了解进行此更改的所有影响),则可以查看按子句对order进行索引,或者(仅在极端情况下)对那些子句应用聚集索引有序字段。

There are many websites that provide good insight into clustered indexes and what they actually mean underneath. 有许多网站可以很好地了解聚簇索引及其下的实际含义。

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

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