I have this SQL query:
SELECT * FROM IMAGES WHERE
IMAGENAME in ('IMG1', 'IMG2', 'IMG3', 'IMG4', 'IMG5', 'IMG6')
ORDER BY CASE IMAGENAME
WHEN 'IMG1' THEN 1
WHEN 'IMG2' THEN 2
WHEN 'IMG3' THEN 3
WHEN 'IMG4' THEN 4
WHEN 'IMG5' THEN 5
WHEN 'IMG6' THEN 6
ELSE 7
END
I cannot guarantee that the list of IMAGENAMEs will be in alphabetical order, hence the case statement, but I would prefer to sort in the DB rather than in code because I trust their sorting code better than mine :)
SQL server analyses that 78% of the execution time is spent sorting - can I reduce this?
It needs to be fairly vanilla SQL as we target SQL Server and Oracle.
Any tuning advice would be fantastic.