【问题标题】:Spring Jdbc binding java.sql.timestamp to oracle date issueSpring Jdbc 将 java.sql.timestamp 绑定到 oracle 日期问题
【发布时间】:2023-03-27 20:00:01
【问题描述】:

我目前正在使用 Spring 的 NamedParameterJdbcTemplate 将一些值注入到 sql 中。当我将两个java.sql.Timestamp 值注入到sql 中时,我遇到了性能问题,该sql 的oracle 列的类型为DATE 并正在执行。

它非常慢(大约 4 分钟),但是当我通过 sql developer 运行它时,它会在不到一秒的时间内执行,因为我在 DATE 列上有一个索引。这是我的调试日志的 sn-p:

select * from test_table.test_column where eventts >= :startDate and eventts <= :endDate and loginname= :loginname and channelind= :channelind
2014-04-21 15:02:50 48416 [http-8080-1] DEBUG org.springframework.jdbc.core.JdbcTemplate  - Executing prepared SQL query
2014-04-21 15:02:50 48417 [http-8080-1] DEBUG org.springframework.jdbc.core.JdbcTemplate  - Executing prepared SQL statement [select * from test_table.test_column where eventts >= ? and eventts <= ? and loginname= ? and channelind= ?]
2014-04-21 15:02:50 48417 [http-8080-1] DEBUG org.springframework.jdbc.datasource.DataSourceUtils  - Fetching JDBC Connection from DataSource
2014-04-21 15:02:50 48438 [http-8080-1] DEBUG org.springframework.jdbc.core.StatementCreatorUtils  - Setting SQL statement parameter value: column index 1, parameter value [2014-04-21 12:02:38.0], value class [java.sql.Timestamp], SQL type 93
2014-04-21 15:02:50 48439 [http-8080-1] DEBUG org.springframework.jdbc.core.StatementCreatorUtils  - Setting SQL statement parameter value: column index 2, parameter value [2014-04-22 00:00:00.0], value class [java.sql.Timestamp], SQL type 93
2014-04-21 15:02:50 48439 [http-8080-1] DEBUG org.springframework.jdbc.core.StatementCreatorUtils  - Setting SQL statement parameter value: column index 3, parameter value [MY_LOGIN], value class [java.lang.String], SQL type unknown
2014-04-21 15:02:50 48439 [http-8080-1] DEBUG org.springframework.jdbc.core.StatementCreatorUtils  - Setting SQL statement parameter value: column index 4, parameter value [WEB], value class [java.lang.String], SQL type unknown

我在这里缺少什么? Oracle 是否将值转换为 TIMESTAMP 导致丢失我在“eventts”DATE 列上的索引?

【问题讨论】:

    标签: java sql spring oracle jdbc


    【解决方案1】:

    我认为你的猜测是正确的。 当你有

    select something from some_table where column1 = :val1
    

    而 va1 与 column1 的类型不同,Oracle 会这样做:

    select something from some_table where to_val1type(column1) = :val1
    

    函数 to_val1type 不存在,由上下文决定(TO_DATE、TO_NUMBER 等)

    要么使你的参数为同一类型,要么自己使用 TO_XXX 函数。

    【讨论】:

      猜你喜欢
      • 2018-10-29
      • 1970-01-01
      • 2012-08-31
      • 1970-01-01
      • 2022-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-10
      相关资源
      最近更新 更多