Is it true that ORDER BY is generally pretty slow? I am trying to run some sql statements where the WHERE clause is pretty simple, but then I am trying an 'ORDER BY' on a VARCHAR(50) indexed column.
I need to sort alphabetically for display reasons. I figured that getting the database to do it for me is the most efficient.
At this point, I am looking to either
- optimize the sql query
- sort the result set in code
Here is the actual query I am trying to run:
// B.SYNTAX is a TEXT/CLOB field
// Indexes on NAME, MODULENAME. PREVIOUS is a CHAR(1) with no index
"SELECT A.NAME, B.SYNTAX, B.DESCRIPTION, A.RATE1, A.RATE2,
A.RATE3, A.STARTDATE, A.ENDDATE, A.HIDE, A.CATEGORYNAME
FROM A, B WHERE A.MODULENAME='"+loadedModuleName+"'
AND A.NAME = B.NAME AND (A.PREVIOUS<>'N' OR A.PREVIOUS IS NULL)
ORDER BY A.NAME"
The size of table A is ~2000 rows and B is about ~500.
I should probably also mention that I cannot do much database specific optimization since we support multiple databases. Also, the application is deployed at a customer site.
I am expecting hundreds of records to be returned (less than 1000).
What would you do? Any tips are appreciated. Thanks.