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 can’t guarantee that the IMAGENAME list will be in alphabetical order, so the case statement, but I would prefer to sort in the database and not in the code, because I trust their sort code better than mine :)
The SQL server analyzes that 78% of the runtime is spent sorting - can I reduce this?
It should be pretty vanilla SQL, as we focus on SQL Server and Oracle.
Any tuning advice would be fantastic.
source
share