|
Re: Need Static SQL [message #604056 is a reply to message #604054] |
Mon, 23 December 2013 06:47 |
ThomasG
Messages: 3211 Registered: April 2005 Location: Heilbronn, Germany
|
Senior Member |
|
|
With some pretty simple and/or for example:
select * from emp
where (p_type = 'JOB' and job = p_value)
or (p_type = 'DEPTNO' and debtno = p_value)
or (p_type = 'MGR' and mgr = p_value)
|
|
|
|
|
|
|
|
|
Re: Need Static SQL [message #604839 is a reply to message #604832] |
Sat, 04 January 2014 07:46 |
|
Kevin Meade
Messages: 2103 Registered: December 1999 Location: Connecticut USA
|
Senior Member |
|
|
It is hard to say what is best solution, and different versions of the database will likely give differing performance due to different optimizations and transformations they know how to do. For example, theoretically the query already provided:
select * from emp
where (p_type = 'JOB' and job = p_value)
or (p_type = 'DEPTNO' and debtno = p_value)
or (p_type = 'MGR' and mgr = p_value)
Could be transformed by the database into a "manually partitioned view" so to speak. This was common back in the day but had to be specifically coded by the developer. Today Oracle knows how to do this automatically.
select * from emp
where (p_type = 'JOB' and job = p_value)
union all
select * from emp
where (p_type = 'DEPTNO' and debtno = p_value)
union all
select * from emp
where (p_type = 'MGR' and mgr = p_value)
After the query was transformed to this form, the database would at runtime check the value of the parameter against each of the constants and essentially "remove" i.e. not execute those parts where the parameter=constant failed. This essentially turns the three part query into a one part query as only the surviving sub-query would actually be executed. This is a form or query leg pruning covered by the generalized label of short circuiting. For example, if the value of p_type was JOB then only the sub-query referencing 'JOB' would be executed. The other two would be short-circuited.
So the OR query can be the same as using only the correct single component query once shirt circuiting is applied. But the database has to decide that the transform is legal for your query first.
The answer to your question "which is better" could be DOES NOT MATTER because Oracle will change it anway. Oracle will figure out the best thing to do and do it.
Kevin
[Updated on: Sat, 04 January 2014 07:48] Report message to a moderator
|
|
|