|
Posted by Erland Sommarskog on 09/11/06 10:30
bozzzza@lycos.co.uk (bozzzza@lycos.co.uk) writes:
> We have a big table connected to a web server, and I want the number of
> rows to be limited that get returned.
>
> So I could do something like this:
>
> select top 10 *
> from object
> order by code
>
> I then realised looking at the help file, because the "order by" clause
> is set the SQL Server has to build the complete dataset, do the order
> by and then filter it to the first ten rows.
Logically, it would that yes. But if there is an index on Code, SQL Server
would use that index, why this could be a fast operation.
> So I was wondering if I put the query into a view like so :
>
> create view vw_object as
> select top 10 *
> from object
>
> Then ran the query like so:
>
> select *
> from vw_object
> order by code
>
> would the SQL server just get the top 10 rows from the view first, then
> apply other order by on it afterwards?
Yes. That would mean that SQL Server would first get any 10 rows, and then
sort the 10 rows on Code. That does not appear as a very useful operation
to me....
--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se
Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
Navigation:
[Reply to this message]
|