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.

With regards to the usage of the IF statement, which is better to use?

Query #1:

SELECT
    whatever
FROM
    auctions AS a
LEFT JOIN
    ...............
WHERE
    ...............
ORDER BY IF(a.max_bid > a.start_price, a.max_bid, a.start_price) DESC, a.end_time ASC, start_time DESC, a.auction_id ASC;

Query #2:

SELECT
    IF(a.max_bid > a.start_price, a.max_bid, a.start_price) AS current_price
FROM
    auctions AS a
LEFT JOIN
    ...............
WHERE
    ...............
ORDER BY current_price DESC, a.end_time ASC, start_time DESC, a.auction_id ASC;

Or is there a better way to order it?

share|improve this question
    
Have you looked at the EXPLAIN statement for both? –  Kermit Feb 19 at 18:30
    
@FreshPrinceOfSO Yes and did some tests, the first seems slightly faster, but just was looking for a general consensus in-case things change on larger data sets. –  Brett Feb 19 at 18:43
    
I doubt you'll get a general consensus. Too much is "it depends." You may want to add those types of things (EXPLAIN statements) to your question. –  Kermit Feb 19 at 18:58

Your Answer

 
discard

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

Browse other questions tagged or ask your own question.