473,385 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Use of ASC & DESC indexes

itiger
12
Expand|Select|Wrap|Line Numbers
  1. CREATE INDEX emp_first_name_idx
  2. ON copy_emp(first_name DESC); 
  3.  
Does the following query uses the above created index

Expand|Select|Wrap|Line Numbers
  1. SELECT * 
  2. FROM copy_emp
  3. ORDER BY first_name DESC;
  4.  
What are other possibilities (i mean queries) where ASC or DESC indexes can be used.
Feb 13 '11 #1
1 10648
hi!

You query will most likely not use the index because you select *.

The following query will most likely use the index:


Expand|Select|Wrap|Line Numbers
  1. SELECT first_name
  2.   FROM copy_emp
  3.  ORDER BY first_name DESC;
However, it will also use the same query declared with DESC if you query with ASC:

Expand|Select|Wrap|Line Numbers
  1. SELECT first_name
  2.   FROM copy_emp
  3.  ORDER BY first_name ASC;
Typically, indexes are not explicitly ASC/DESC. There is only one particular reason to do so, that is if you have mixed ASC/DESC combination in you ORDER BY:

Expand|Select|Wrap|Line Numbers
  1. CREATE INDEX t_idx ON t (c1 ASC, c2 desc);
The following two queries might benefit from the index:

Expand|Select|Wrap|Line Numbers
  1. SELECT c1, c2
  2.   FROM t
  3.  ORDER BY c1 ASC, c2 DESC;
  4.  
  5. SELECT c1, c2
  6.   FROM t
  7.  ORDER BY c1 DESC, c2 ASC;
The first query can read in index order, the second query against index order. But an index without explicit ASC/DESC could not support those ORDER BY clauses.

However, all of that is only if the optimizer decides that an INDEX FULL SCAN is best for that query. Recent Release often prefer a FULL scan followed by an SORT. By selecting * you will probably get a FULL TABLE SCAN with a SORT.

Covering the ORDER BY clause with the index is most useful in FIRST_ROWS mode or for indexed Top-N queries.

Have a look at my e-Book SQL Performance Explained if you like to learn more about SQL Indexing.
Mar 28 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Talis | last post by:
I have a reasonably large table (over 2 million rows) and my select results need to be sorted in descending order. I've noticed, however, that my queries ORDER'ed by DESC are at least twice as...
0
by: K Finegan | last post by:
I have an archival process on a large database that runs once a month. At the beginning of the process the triggers and indexes on the tables whose data is moved are dropped, the data is moved and...
8
by: Mike | last post by:
Hello, I have a few rather urgent questions that I hope someone can help with (I need to figure this out prior to a meeting tomorrow.) First, a bit of background: The company I work for is...
8
by: Jernej Kos | last post by:
I have a multicolumn index on two columns. If i use the columns in ORDER BY like this: ORDER BY col1, col2; The index is used. But, if one column is sorted DESC it is not used: ORDER BY col1...
1
by: Primoz | last post by:
Hello, is there any way to speedup queryes like select ... from where = order by desc limit ; with index on field b I get something, but pgsql afterwards resolves to sorting manually. ...
2
by: mattdfong | last post by:
I am using mysql to receive stock market rate data, and I have a rate feed which tells me when the rate has changed. I input the data as it comes in, into a mysql database that has both a...
2
by: umair.cheema | last post by:
Hi! 1-I read in MS Access help that Indexes are automatically made when we declare a primary key and they are used for fast searching and sorting purpose. But i am still confused that how can i...
1
by: aRTx | last post by:
<? /* Directory Listing Script - Version 2 ==================================== Script Author: Artani <artan_p@msn.com>. www.artxcenter.com REQUIREMENTS ============ This script requires...
16
by: Okonita via DBMonster.com | last post by:
Hi all, I am comming along with all this Linus/DB2/scripting business...I am no longer scared of it!! (LOL). But, I need to create a .ksh script that does a REORGCHK and output only tables...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.