Subject PreparedStatement with LIKE ? extremly slow
Author Carsten Schäfer
I'm using Firebird 2.0.1 RC with Jaybird 2.1.1 on Win XP.

I have this PreparedStatement;
SELECT DISTINCT * FROM t_apos JOIN t_auftrag on f_id_auftrag =
id_auftrag WHERE f_liefer_nr LIKE ?

There is unique index on f_liefer_nr (varchar field).
Code is Standard:
PreparedStatement ps= con.prepareStatement("SELECT DISTINCT *
FROM t_apos JOIN t_auftrag on f_id_auftrag = id_auftrag WHERE
f_liefer_nr LIKE ?");
ps.setString(1,"2005-0119849-8");
ResultSet rs = ps.executeQuery();
if (rs.next()) {
Long id = new Long(rs.getLong(1));
}
}
rs.close();
ps.close();


This code is extremly slow (about 15 seconds on a large database),
but when i change the code to;
PreparedStatement ps= con.prepareStatement("SELECT DISTINCT *
FROM t_apos JOIN t_auftrag on f_id_auftrag = id_auftrag WHERE
f_liefer_nr LIKE '2005-0119849-8'");
ResultSet rs = ps.executeQuery();
if (rs.next()) {
Long id = new Long(rs.getLong(1));
}
}
rs.close();
ps.close();

It is fast (about 50 ms),
I think in the first case no index is used and in the second case the
index is used.
This is a big problem for us, because the second query should not be
used with Statement pooling turned on, because we are using this query a
lot.

mfg
Carsten