Quote:
quote:Originally posted by joefawcett
I agree with Jeff about the unknown nature of the row order.
My one point is that if you want a view that always delivers records in a particular order you can add an ORDER BY but you also need to use SELECT TOP 100 PERCENT before the actual selection.
|
It's not so much that the ordering is unknown, as it is that there
isn't any.
I stuck that comment about views in my post wondering if I would elicit this TOP 100 PERCENT ... abomination.
If you want a
query that returns its rows in a particular order, say so by specifying an ORDER BY clause. It can be really cheap, if the query processor was going to return the rows in that order anyway (via a clustered or other index).
But a view is really just a virtual table. Thus, I am free to JOIN the view to other views, tables, and/or other queries (derived tables). If I have specified an ORDER BY clause via the (nonstandard) TOP clause in the view, then this ordering will most likely result in suboptimal behavior, since the query processor is forced by the view definition to construct the JOIN in ways that may not be able to take full advantage of other information the query processor has, such as indexes, data distribution statistics, etc.
Once I start specifying JOINs in a query, I'm sure you would agree that all bet's are off regarding any inferences I can make about the ordering of rows (not that I should be making any anyway :D). If you have JOINs, and you need the results in a particular order, you
must specify an ORDER BY clause. Given that, any ordering I have imposed in the view will be performed because of its definition, then destroyed due to the JOIN operations.
I believe that if you think you need to order a view, you don't understand what they really are.
Tables aren't ordered, resultsets are (or may be).
Jeff Mason
Custom Apps, Inc.
www.custom-apps.com