For SQL select returning more than 1 value, how are they sorted when the GUID is?
I am wondering how SQL Server orders the data that is returned from the query and the Id columns of the respective tables are all of type uniqueidentifier.
I am using NHibernate GuidComb when creating all GUIDs and doing things like:
Sheet sheet = sheetRepository.Get(_SheetGuid_); // has many lines items
IList<SheetLineItem> lineItems = sheet.LineItems;
I'm just trying to figure out how they'll be ordered when I do something like:
foreach (SheetLineItem lineItem in lineItems)
I can't see to find a good article on how GUIDs are compared by SQL when ordering , if that's the case .
a source to share
If you do not enter an ORDER BY clause, SQL Server does not guarantee any ordering on the results. It can sem return in some order sequentially (e.g. clustered index order), but you cannot be sure that this will always be the case (e.g. if a query is split and executed across multiple threads, when the results are merged, the order may be different on each execution since threads can execute in different orders).
The only way to get a specific order is by using the ORDER BY clause. In NHibernate, this will be achieved by specifying order-by = "..." on your bags (or equivalent) in your mapping files.
For more information on "ordering" see the NHibernate docs: http://nhibernate.info/doc/nh/en/index.html
a source to share
GUIDs are sorted this way using ORDER BY. Citing the article ...
- 0..3 are evaluated in order from left to right and less important, then
- 4..5 are evaluated in order from left to right, then
- 6..7 are scored in order from left to right, then
- 8..9 are scored in order from left to right, then
- A..F are ranked in order from left to right and are most important
a source to share