I have a table containing over 15 million records in oracle. its kind of a log table that has a created_ts column of type date. I have a simple custom type index in the created_ts column.
I have a simple range request:
select * from table1 where created_ts >= ? and created_ts <= ?;
when I run this query from SQLPlus or SQL Developer, etc., for example:
select * from table1 where created_ts >= TO_DATE( '2009-11-10 00:00:00', 'YYYY-MM-DD HH24:MI:SS') and created_ts <= TO_DATE( '2009-11-10 23:59:59', 'YYYY-MM-DD HH24:MI:SS');
the request is returned within 1-2 seconds max.
but when I run the same request in java via JDBC and set the appropriate "?" params using java.sql.Timestamp object. The request takes a lot of time. Analyzing the oracle process, it goes for a full table scan and does not use an index.
The jdbc driver I'm using is ojdbc5 11.1.0.7.0
Can anyone help ... how to properly create an index so that it uses the index.
My problem was solved when I used oracle.sql.DATE objects to set the binding variables instead of "java.sql.timestamp". The query used the index and ran for almost 1-2 seconds.
Thanks to everyone who answered and helped.
But for me this is a problem, since this solution depends on the database, and my application receives the database connection and the request as parameters, loads and process data in a general way. A DB connection can be any RDBMS, such as oracle, mysql, etc.