[英]PreparedStatement on oracle and index usage
I have a table like this one in Oracle: 我在Oracle中有一张这样的表:
create table suppliers(name varchar2(100));
With its corresponding index on upper(name)
: 在
upper(name)
具有相应的索引:
create index supplier_name_upper_idx on suppliers(upper(name));
I would like to populate an autocomplete through AJAX, getting the information from a Servlet running JDBC queries. 我想通过AJAX填充自动完成功能,从运行JDBC查询的Servlet获取信息。
This works: 这有效:
PreparedStatement ps =
conn.prepareStatement(
"select * from suppliers where upper(name) like ?"
);
ps.setString(1, 'something%');
The problem is, as far as I know, PreparedStatement
won't use the index because it can't know, at statement compile time, whether the parameter is going to be 'something%'
(being able to get performance advantage from index) or '%something%'
(not being able to get performance advantage from index). 据我所知,问题在于,
PreparedStatement
将不使用索引,因为它无法在语句编译时知道参数是否将达到'something%'
(能够从索引中获得性能优势)或'%something%'
(无法从索引获得性能优势)。
So, my question is: 所以,我的问题是:
Statement
instead? Statement
吗? If so, what's the best way to escape the input parameter (since it will come from an AJAX request) PreparedStatement
to use the index? PreparedStatement
使用索引的东西吗? I think prepared statement is better because you will have fewer parses on server side, and as a result fewer 'latches : library cache'. 我认为,准备好的语句会更好,因为服务器端的解析将更少,因此“闩锁:库缓存”也会更少。
SELECT /*+ INDEX suppliers(supplier_name_upper_idx) */ * from suppliers ....
should make statement use index "supplier_name_upper_idx". SELECT /*+ INDEX suppliers(supplier_name_upper_idx) */ * from suppliers ....
应该使语句使用索引“ supplier_name_upper_idx”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.