<p>I have this SQL query:</p>

<pre><code>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
</code></pre>

<p>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 :)</p>

<p>SQL server analyses that 78% of the execution time is spent sorting - can I reduce this?</p>

<p>It needs to be fairly vanilla SQL as we target SQL Server and Oracle.</p>

<p>Any tuning advice would be fantastic.</p>

Could you strip the
```
IMG
```
(or non-numeric) part of the
```
IMAGENAME
```
to a new column when writing to the database, then sort on the new numeric-only column?
For example:
```
```
SELECT * FROM IMAGES WHERE
IMAGENAME in ('IMG1', 'IMG2', 'IMG3', 'IMG4', 'IMG5', 'IMG6')
ORDER BY IMAGENO
END
```
```
where
```
IMAGENO
```
would contain
```
1, 2, 3, 4, 5, 6
```
for each row in your example, respectively.