Derby/SQL – How to increase performance when left joining and retrieving varchar columns

derbyjavajdbcperformance

Through trial and error I found that if I do a left join such as:

SELECT 
    firsttable.id,
    secondtable.id,
    secondtable.varcharColumn
FROM 
    firsttable
LEFT JOIN 
    secondtable
ON 
    firsttable.id=secondtable.id

The performance is terrible for larger tables. If I EITHER remove secondtable.varcharColumn as a column in the resultset OR change the type then the performance is about an order of magnitude better. The column is a varchar 255, so it should not make that much of a difference. If I change the type of column to integerColumn or dateColumn than I also see an order of magnitude difference in the performance. Even a varchar of a few characters seems to result in the same performance degradation.

Please note that the WHERE clause is NOT the issue here. I've omitted it because the issue is the same whether or not a WHERE clause is included. The issue has to do with the type of data in the column and not the quantity of data returned. I could also use LIMIT 1000 and have the very same issue.

Indexing the column will not help as the column is not used for joining. It is also not part of the where clause if there was one.

Any suggestions on how to improve the performance would be greatly appreciated!!

Best Answer

The lack of a WHERE clause or an ORDER BY clause probably means that your query is not using an index (and is most certainly selecting all rows in the table).

So, I'd suggest:

  1. Add a "WHERE" clause
  2. Add an "ORDER BY" clause
  3. Make sure that you have appropriate indexes on the two tables for the ids and the columns in the WHERE and ORDER BY clauses.
  4. Look at the query execution plan to make sure that your indexes are used.