Same query ran on the same database on two servers returns different order -
I have a query that I run on two databases, restores two SQL Server 2008R2 instances and they are different Returns with orders (on one server they are ordered by ID and others are not ordered on them)
Select as CID, format format BankCode C Join Inner Bank Formatted F On (c. Format = F.ID) < / Code>
As you can see that there is no order in the query, but the results are being displayed, it is different.
I have tried to recreate the indexes for two tables, but it is not working.
The order of the above queries is done on the server, where the code is based on the SELECT * bank code
, the result is not ordered (by the primary key column), but After joining ING with
bank format
table, results are ordered.
I do not know what else it is to explain.
Can someone suggest what may be the reason for this?
Can this be anything related to database collation, indexed or primary / foreign key?
Why data is returned from the database in a particular order, When you have many elements in the order
section:
- Index
- Data
- the order in which the data was inserted (Which may have an effect on which data is stored on disk, is divided into pages.)
Then there are many other things when querying:
- gets added (in both order The order that the specified order and database engine decides to consider / execute them)
- How does the decision to join the database engine or things like the
WHERE
section Hash tables, hash sets, mergezens, table lookups, etc. "Natural" orders will be affected
The problem is that these reasons are not absolute, and will never happen. When you do not specify the order, the database is "licensed to reorder", and it will use it, its functions can not be explained apart from it.
In this way, if you want to know about some reason in some specific way, you may be look at the execution plan, but This whole picture can not tell whether this picture is a (potentially potential) snapshot of the current execution plan. When data or indexed changes happen, it can change at any time.
In this way, you will not get a definite answer here.
There is only one rule to follow here:
P>If you care about the order of the returned data, then
ORDER BY
Add to that rule:
If you do not add the
ORDER BY
section, you (wrong) order Lose all rights to care / complain about
Comments
Post a Comment