【问题标题】:PreparedStatement on oracle and index usage关于 Oracle 和索引使用的 PreparedStatement
【发布时间】: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%'(无法从索引中获得性能优势)。

所以,我的问题是:

  1. 我应该改用Statement 吗?如果是这样,转义输入参数的最佳方法是什么(因为它将来自 AJAX 请求)
  2. 有什么东西可以让PreparedStatement 使用索引吗?

【问题讨论】:

    标签: performance oracle indexing escaping prepared-statement


    【解决方案1】:

    我认为准备好的语句更好,因为您将在服务器端进行更少的解析,从而减少“闩锁:库缓存”。

    SELECT /*+ INDEX suppliers(supplier_name_upper_idx) */ * from suppliers .... 应该使语句使用索引“supplier_name_upper_idx”。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-12
      • 2011-02-20
      • 1970-01-01
      • 2012-05-07
      • 2018-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多