开发者

Indices required when selecting data in accending and decending order

开发者 https://www.devze.com 2023-02-14 23:16 出处:网络
We have a table form which we would like to select data in both accending and decending order. Do we need to create 2 indices?

We have a table form which we would like to select data in both accending and decending order.

Do we need to create 2 indices?

How would SQL Server process a request for all rows in decending order if there was 开发者_StackOverflow中文版only an accending clustered index?


Short-Answer : No. If this is only for one column, then the same index should be usable in both cases. As for traversing, since indexes are doubly-linked lists, they can be traversed in either order.

http://msdn.microsoft.com/en-us/library/aa933132%28SQL.80%29.aspx

Other Details...

If you have a (say..ascending) index on emp_name, then both the queries below should be able to make use of it without aditional sorting.

select x,y,z,a,b,c,emp_name from <table_name> 
order by emp_name desc;

select x,y,z,a,b,c,emp_name from <table_name> 
order by emp_name asc;

The problem is when the query references more than one column and the order by for these two is in different order.

select x,y,z,a,b,c,emp_name from <tabble_name
order by emp_name asc, a desc.

In this case, if you use the desc clause when creating the index, an additional sort can be avoided.

Check this link. It is spcific to Oracle, but I believe SQL Server works pretty much the same way.

http://forums.oracle.com/forums/thread.jspa?messageID=4061884

Another useful link :

http://www.mssqltips.com/tip.asp?tip=1337

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号