【发布时间】:2012-12-01 15:27:13
【问题描述】:
我在 Oracle 中有一张这样的表:
create table suppliers(name varchar2(100));
在upper(name)上有对应的索引:
create index supplier_name_upper_idx on suppliers(upper(name));
我想通过 AJAX 填充自动完成功能,从运行 JDBC 查询的 Servlet 中获取信息。
这行得通:
PreparedStatement ps =
conn.prepareStatement(
"select * from suppliers where upper(name) like ?"
);
ps.setString(1, 'something%');
问题是,据我所知,PreparedStatement 不会使用索引,因为它在语句编译时不知道参数是否将是'something%'(能够获得性能来自索引的优势)或'%something%'(无法从索引中获得性能优势)。
所以,我的问题是:
- 我应该改用
Statement吗?如果是这样,转义输入参数的最佳方法是什么(因为它将来自 AJAX 请求) - 有什么东西可以让
PreparedStatement使用索引吗?
【问题讨论】:
标签: performance oracle indexing escaping prepared-statement