Home » SQL & PL/SQL » SQL & PL/SQL » function based index on date column (11g, 11.2.0.3.0)
|
Re: function based index on date column [message #661182 is a reply to message #661180] |
Fri, 10 March 2017 05:13   |
cookiemonster
Messages: 13967 Registered: September 2008 Location: Rainy Manchester
|
Senior Member |
|
|
Well you're using the wrong function for the job, that's what trunc is for.
To_date doesn't accept a date parameter, so oracle has to implicitly to_char the date first - and it's obviously making implicit conversion explicit in the FBI.
You should never use to_date for this purpose because if the user changes their sessions nls_date_format you can end up getting the wrong result or an error.
Trunc is explicitly designed for this task and changing nls_date_format won't affect what it does.
The way to do it without using a function on the column is:
date_col >= trunc(date_parameter)
AND date_col < trunc(date_parameter) + 1
|
|
|
|
Re: function based index on date column [message #661197 is a reply to message #661185] |
Fri, 10 March 2017 06:52   |
cookiemonster
Messages: 13967 Registered: September 2008 Location: Rainy Manchester
|
Senior Member |
|
|
trunc is no different to to_date in that regard.
If you wrap a column with a function call (any function, what so ever) then oracle will not use that column for index look-ups unless you have a function-based index that exactly matches the function call in the query.
So if you have:
trunc(date_col) = date_paramater
then oracle will not use a normal index on date_col, but will happily use a function based index on trunc(date_col).
If you use the code format from my previous post then this problem goes away as no function is being applied to the date column.
Is the advice to use trunc what caused you to try to_date instead? Cause if it is you missed the point, any function causes that problem, it's just that trunc is the correct function for the task.
|
|
|
Re: function based index on date column [message #661199 is a reply to message #661197] |
Fri, 10 March 2017 07:05   |
cookiemonster
Messages: 13967 Registered: September 2008 Location: Rainy Manchester
|
Senior Member |
|
|
The specific reason why your to_date index was behaving the way it was is because the optional 2nd parameter to to_Date is the current sessions nls_date_format.
When oracle creates a function based index it computes the result of the function and stores that in the index. But in this case the result is session specific, so it needs to actually add the missing parameter using the settings of the session that created the index.
For most oracle functions with optional parameters the defaults for those parameters are hard-coded, but date and number conversion functions (to_char, to_number, to_date) are obvious exceptions.
|
|
|
|
|
|
|
|
Goto Forum:
Current Time: Mon May 19 13:14:14 CDT 2025
|