Assume you have the following update:
Update table set col1 = func(col2)
where col1<>func(col2)
The func function is evaluated two times for every row, or once for every row?
Thanks,
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
This is the kind of situation where some experimentation is useful (this was conducted on 10g). Using the following query, we can tell that normal functions, using the same parameters (in this case, none) will be executed each time they are called:
This is because Oracle assumes that a function will not return the same value consistently unless you tell it otherwise. We can do that by creating a function using the
deterministickeyword:Using this function instead of
dbms_randomin the first query tells us that the query is being executed only once, despite the many calls. But this only clarifies theselectsection. What if we use the same deterministic function in both aselectand awhereclause. We can test that using the following query:You may have to run this several times to see our proof, but, eventually, you’ll see a list of values less than 0.5. This provides us with evidence that even the deterministic function is being executed twice: once for each section it appears in. As an alternative, you can modify our deterministic function as follows, then run the subsequent query, which will reveal 2 lines written to
DBMS_OUTPUT.