In a SQL SELECT
statement, I'd like to execute a function that is deterministic for the scope of that SELECT
statement (or transaction would be ok, too):
select t.x, t.y, my_function(t.x) from t
Many values of t.x
are the same so Oracle could omit calling the same function again and again, to speed things up. But if I label the function as DETERMINISTIC
, the results may be cached between several executions of this query. The reason why I can't use DETERMINISTIC
is because my_function
uses a configuration parameter that is changed from time to time.
Is there any other keyword I could use? Are there any catches that I should be aware of (memory issues, concurrency, etc)? Or maybe any other tricks, such as analytic functions to call the function only once per t.x
value (without major performance impact)?
A possible simplistic workaround would be to create a second, DETERMINISTIC function that calls the first one; but have the second function take an additional, meaningless parameter, for which you provide a different literal value in each query that uses the function.
Another method is to put the function in a package and set the result as a global variable. Then when you call the function check whether the input variables are the same as before and quickly return the global variable if so:
This is not an answer to your question, but can be a solution for you. This configuration parameter that you've mentioned, can't be added as a parameter to function? In this case,
my_function(t.x, val1)
is a different thing vsmy_function(t.x, val2)
.If you do this:
then Oracle can use subquery caching to achieve reduced function calls.