简体   繁体   中英

Spring Jdbc binding java.sql.timestamp to oracle date issue

I am currently using Spring's NamedParameterJdbcTemplate to inject some values into an sql. I am having performance issues when I inject two java.sql.Timestamp values into the sql who's oracle column is of type DATE and executing.

It is extremely slow (approx. 4 minutes) but when I run it through sql developer it executes in less than a second since I have an index on that DATE column. Here is a snippet of my debug log:

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

What am I missing here? Is Oracle casting the values to a TIMESTAMP resulting in losing the index I have on the "eventts" DATE column?

I think your guess is right. When you have

select something from some_table where column1 = :val1

and va1 is not of the same type as column1, Oracle will do:

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

Function to_val1type doesn't exists, it will decided by context (TO_DATE, TO_NUMBER, etc..)

either make your parameter of the same type, or use TO_XXX fuctnion yourself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM