Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

I have added indexes to table which are used for searching result. I am showing results by ASC or DESC order. So that column should have index or not? I have 2 more indexes on that table. How performance will affect by making or not making index to that column?

share|improve this question
1  
Please post the query –  RolandoMySQLDBA Jan 20 '12 at 17:25

1 Answer 1

up vote 10 down vote accepted

Yes, MySQL can use an index on the columns in the ORDER BY (under certain conditions). However, MySQL cannot use an index for mixed ASC,DESC order by (SELECT * FROM foo ORDER BY bar ASC, pants DESC). Sharing your query and CREATE TABLE statement would help us answer your question more specifically.

For hints on how to optimize ORDER BY clauses: http://dev.mysql.com/doc/refman/5.1/en/order-by-optimization.html

Edit 2012-01-21 8:53AM

There were questions about the source of my statement about using an index with mixed ASC/DESC in the ORDER BY. From the ORDER BY Optimization documentation:

In some cases, MySQL cannot use indexes to resolve the ORDER BY, although it still uses indexes to find the rows that match the WHERE clause. These cases include the following:

...

You mix ASC and DESC:

SELECT * FROM t1 ORDER BY key_part1 DESC, key_part2 ASC;

...

Also, the DESC keyword is meaningless in CREATE INDEX

An index_col_name specification can end with ASC or DESC. These keywords are permitted for future extensions for specifying ascending or descending index value storage. Currently, they are parsed but ignored; index values are always stored in ascending order.

share|improve this answer
    
"MySQL cannot use an index for mixed ASC,DESC". Reference for this? –  ypercube Jan 21 '12 at 9:26
    
CREATE INDEX idx_foo ON foo(bar ASC, pants DESC); should work. –  Frank Heikens Jan 21 '12 at 10:38
    
My source is the mysql documentation linked in my answer. Search for "mix." –  Aaron Brown Jan 21 '12 at 12:43
2  
MySQL parses asc & Desc in CREATE INDEX, but ignores them. Indexes are always stored in ascending order. dev.mysql.com/doc/refman/5.5/en/create-index.html –  Aaron Brown Jan 21 '12 at 12:54

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.