SQL Server table is sorted by default

I have a simple SSIS package where I import data from a flat file into a SQL Server table (SQL Server 005). The file contains 70 thousand lines, and the table does not have a primary key. The import is successful, but when I open the SQL Server table, the row order is different from the file order. After careful observation, I see that the data in the table is sorted by default in the first column. Why is this happening? and how can I avoid default sorting? Thank.

+3
source share
2 answers

You cannot rely on ordering unless you specify order byin your SQL query. SQL is relational algebra that works with collections. These sets are unordered. Database tables do not have a built-in order.

It is possible that the sets are ordered because of how data is retrieved from the tables. This can be based on the primary key, insertion order, cluster key, apparently random order based on the query execution plan or the actual data in the table or even on the moon phase.

The bottom line, if you want to receive a specific order, use order by. If you don’t need a specific order, the DBMS can deliver your rows in any order for free, including on the basis of the first column.

, , , , . order by, . , , , , ( , /, , ).

( ), .

+11

, .

, , gaurenteed.

, , , .

+7

Source: https://habr.com/ru/post/1741209/


All Articles