Can I trust the optimizer to do the right thing every time?

Hello,
I have a Business Objects report that is performing poorly, I kind of found something akin to a solution but I'd like a second (and third, and...) opinion about is suitability. I don't know much about BO, so I'll stick to the SQL part.
So, the query looks something like that:
SELECT ..... FROM
-- this is the generic part of the report
< long list of ANSI joins here>
RIGHT JOIN some_table ON (conditions - non parametrizable from the report - not allowed by BO)
<some more joins for good measure>
-- end of the generic part, up to here nothing can take user parameters
WHERE
-- this is the specific part, from here on, the conditions can take user parameters
some_column = @some_param
....So far so simple. Now, I'm trying to find a way to push some user defined parameters in the generic part.
I can to this using package variables, something like that (very simplified form) :
CREATE OR REPLACE PACKAGE vars AS
my_filter VARCHAR2(100);
FUNCTION set_filter(filter_val IN VARCHAR2) return number;
END vars;
CREATE OR REPLACE PACKAGE BODY vars AS
FUNCTION set_filter(filter_val IN VARCHAR2) return number DETERMINISTIC IS
      my_filter := filter_val;
      return 1;
END set_filter;
END vars;And ask the developers to rewrite the report like that:
SELECT ..... FROM
-- this is the generic part of the report
< long list of ANSI joins here>
RIGHT JOIN some_table ON (conditions  - as above
                                                *AND my_varchar_column = vars.my_filter*)
<some more joins for good measure>
-- end of the generic part, up to here nothing can take user parameters
WHERE
-- this is the specific part, from here on, the conditions can take user parameters
some_column = @some_param
*AND vars.set_filter('some text here') = 1*
....The question is, can I trust the optimizer to evaluate my where clause (thus executing set_filter) before trying to enter the joins?
I know that this works nicely with conditions like "1=0", in this case it doesn't even bother to read the tables involved. But can I assume it will do the same with a pl/sql function? Always?
In my tests it seems to work but obviously they are not exhaustive. So I guess what I'm asking is, can anyone imagine a scenario where the above would fail?
Thanks,
Iulian
Edited by: Iulian J. Dragan on Feb 27, 2013 2:57 AM

Iulian J. Dragan wrote:
Even though I tell the optimizer, "look, this function is ridiculously expensive, plus it won't filter any rows" it keeps evaluating it first.
So it looks like not only can I rely on Oracle to evaluate a literal expression first, much as I try I can't make it behave otherwise.
Still, this is only empirical, I wouldn't use it in a production environment. Probably...Iulian,
I think I understand what you are trying to do here. Something like using cost to drive the optimizer to process the SQL statement "procedurely"...
But I believe SQL, by definition, is a set-based language and this means one can not rely on the way an SQL statement is executed by the oracle engine.
I believe this is good in most of the situations. However, I understand that it is possible that one may want to use his/her knowledge of the data to write
SQL so that it is more efficient. CBO, being a software, has its limitations and may not always make the right decision, depending upon complexity of rewrite.
I don't know enough about your original problem, which involves a third-party product i.e. Business Objects, but there is a way in your example to enforce
the sequence in which he functions are called.
Here is your code, as it is, producing the results that you have demonstrated:
SQL> select * from v$version ;
BANNER
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
PL/SQL Release 11.2.0.1.0 - Production
CORE     11.2.0.1.0     Production
TNS for Linux: Version 11.2.0.1.0 - Production
NLSRTL Version 11.2.0.1.0 - Production
SQL> create table testtab(n number) cache;
Table created.
SQL> insert into testtab values(1);
1 row created.
SQL> commit;
Commit complete.
SQL> exec dbms_stats.gather_table_stats(user, 'TESTTAB', METHOD_OPT=>'FOR ALL COLUMNS SIZE 1');
PL/SQL procedure successfully completed.
SQL> create or replace function testfunc(p in varchar2)
  2  return number
  3  as
  4  begin
  5    dbms_application_info.set_client_info('executed with argument ' || p);
  6    return 1;
  7  end testfunc;
  8  /
Function created.
SQL> select * from testtab where n=5 and testfunc('no worries')=1;
no rows selected
SQL> select sys_context('USERENV', 'CLIENT_INFO') from dual;
SYS_CONTEXT('USERENV','CLIENT_INFO')
executed with argument no worries
SQL> create or replace function testfunc(p in varchar2)
  2  return number
  3  as
  4  begin
  5    dbms_application_info.set_client_info(sys_context('USERENV', 'CLIENT_INFO') || '|testfunc with argument ' || p);
  6    return 1;
  7  end testfunc ;
  8  /
Function created.
SQL> create or replace function testfunc2(p in varchar2)
  2  return number
  3  as
  4  begin
  5    dbms_application_info.set_client_info(sys_context('USERENV', 'CLIENT_INFO') || '|testfunc2 with argument ' || p);
  6    return p;
  7  end testfunc2;
  8  /
Function created.
SQL> EXEC dbms_application_info.set_client_info('');
PL/SQL procedure successfully completed.
SQL> select * from testtab where testfunc2(n)=5   and testfunc('am I first?')=1;
no rows selected
SQL> select sys_context('USERENV', 'CLIENT_INFO') from dual;
SYS_CONTEXT('USERENV','CLIENT_INFO')
|testfunc with argument am I first?|testfunc2 with argument 1
SQL> EXEC dbms_application_info.set_client_info('');
PL/SQL procedure successfully completed.
SQL> select * from testtab where testfunc2(n)=1 and testfunc('so lonely...') = 0;
no rows selected
SQL> select sys_context('USERENV', 'CLIENT_INFO') from dual;
SYS_CONTEXT('USERENV','CLIENT_INFO')
|testfunc with argument so lonely...I have left out the statistics association part above as I believe it does not affect.
And below is the rewritten queries that result in functions being called in specific order
SQL> EXEC dbms_application_info.set_client_info('');
PL/SQL procedure successfully completed.
SQL> select * from testtab where decode(testfunc2(n),5,testfunc('am I first?'))=1;
no rows selected
SQL> select sys_context('USERENV', 'CLIENT_INFO') from dual;
SYS_CONTEXT('USERENV','CLIENT_INFO')
|testfunc2 with argument 1
SQL> EXEC dbms_application_info.set_client_info('');
PL/SQL procedure successfully completed.
SQL> select * from testtab where decode(testfunc2(n), 1, testfunc('so lonely...')) = 0;
no rows selected
SQL> select sys_context('USERENV', 'CLIENT_INFO') from dual;
SYS_CONTEXT('USERENV','CLIENT_INFO')
|testfunc2 with argument 1|testfunc with argument so lonely...One can also use the more flexible CASE expressions in place of DECODE.
Hope this helps.

Similar Messages

  • HT201493 How can I bypass the password screen every time open the app?

    How can I bypass the login screen when I open the app?

    Welcome to the Apple Community.
    open what app?

  • I want to stop my printer from printing the same thing every time I turn it on.

    HP 6500A: how do I cancel printing jobs so that the printer doesn't continue to reprint them?

    Hi Mjs51,
    Try this - Assumes Windows 7 (you didn't say what OS your are using):
    Turn Off the printer.
    Start > search / type, then click on "Control Panel" > icon view > Administrative Tools >
    Services > Click on "Name" to sort by name > Scroll down, look for "Print spooler" > Stop
    Leave that Services window open...
    Open a Windows Explorer window:
    Start > search / type, then click on "Windows Explorer" > type / paste in the title bar (to the right of the <--> arrows    C:\Windows\System32\spool\PRINTERS
    Alternatively, you can poke at each folder name (Windows > System32...) until you get to the folder named PRINTERS
    Remove (delete) all the files you see in that folder directory. 
    (Highlight > Right-Click > Delete)
    Go back to the Services window:
    "Restart" the Print spooler service
    NOTE:  If you cannot stop / start the spoolsv service, try Right-Click on services and "Run as Administrator" to give you permission to control the services.
    Unplug the printer, wait a bit (One minute), plug it back in...turn it on.
    NOTE:  There may be a buffer cache clear selection on the Printer - you could check on the front panel of the Printer Menu.  Try this in the event that you cannot get to the outlet to unplug the printer OR try it in place of unplugging the printer.  Choice.
    I hope this helps solve your printer problem.
    Say “Thanks” for the Help!  Click the Kudos Star whenever you find a Post that helps you.
    Mark the Post “Accepted Solution” to say the solution provided worked for you.
    Kind Regards,
    Dragon-Fur

  • How can I change the fact that every time I open firefox, I get redirected to a website promising a free ibook?

    OK. So in October I made the mistake of opening a website offering a free iBook (I'm really not gullible; I thought I could figure out who was sending it too me). Ever since then, I cannot open a new Firefox session without being redirected to the site (If I open up more than one webpage and save them before closing, I don't get redirected to the ibook site).
    I actually keep my web-surfing software on thumb drives, and I tried two different thumb drives doesn't matter. I purchased and ran an anti-virus program. No dice. Can you help me?
    I have a MacBook, which runs on system OSX 10.5.8

    Hi revbennyd,
    So it sounds like your home page has been hijacked? You should take a look at the Knowledge Base article [[How to set the home page]].
    There are steps in that article to reset your homepage preferences to default. I would try that first to clear out any strangeness. If that works, you can try setting your homepage again to your personal preference.
    If that doesn't work you should look at the article [[Preferences are not saved]].
    Finally, your issue may be because you've been hit with some [https://support.mozilla.org/en-US/kb/Is%20my%20Firefox%20problem%20a%20result%20of%20malware?s=malware&r=0&e=sph&as=s#w_how-do-i-get-rid-of-malware Malware].
    Hopefully this helps!

  • I have the problem that, every time i open the browser, all the contence is concentrated to the right. Every time when i want to fix it i have to open a new tab. What can i do to permantly fix this?

    www.basspics.be/firefox02.png
    www.basspics.be/firefox01.png

    This issue can be caused by the Babylon Toolbar 1.1.8 extension
    Start Firefox in <u>[[Safe Mode]]</u> to check if one of the extensions is causing the problem (switch to the DEFAULT theme: Firefox (Tools) > Add-ons > Appearance/Themes).
    *Don't make any changes on the Safe mode start window.
    *https://support.mozilla.com/kb/Safe+Mode
    *https://support.mozilla.com/kb/Troubleshooting+extensions+and+themes

  • The History tab at the top of the screen. When I click it it shows history but it shows the same thing every time and wont change. Even when i click "all history" and delete it it is still there. How do i delete it?

    Could you be specific where the files to fix this are?

    A possible cause is a problem with the file places.sqlite that stores the bookmarks and the history.
    *http://kb.mozillazine.org/Bookmarks_history_and_toolbar_buttons_not_working_-_Firefox

  • Can i trust the registry cleaner Error Doctor?

    can i trust the registry cleaner Error Doctor?

    I don't think it is a good place for this question. Try searching a bit in a search engine like google or bing.
    On a personal side, I use CCleaner and it is ok.

  • Can we trust the Adobe's promise of making the Folio format Open Source by 2014.Q1?

    Hi!
    We have been following the Adobe Digital Publishing blogs and we got pretty excited about the posibility of having the Folio format as a Open Source standard, specially if this release comes with a .folio viewer we could use in mobile apps.
    There are a couple of links that claim that Adobe will release the Folio format really soon:
    http://blogs.adobe.com/digitalpublishing/2013/12/readership-metrics-open-folio-format.html
    http://eon.businesswire.com/news/eon/20131210005400/en
    The question is, can we trust the Adobe's promise and get more and more excited about this release? Is this for real?

    You should really be posting this in the Digital Publishing Suite forum here (where the folks who care are hanging out):
    http://forums.adobe.com/community/dps

  • Sent iphone 5 3x for repair ( 1 repair and 2 replacement ) in a span of 2 months.. And i just ask apple care to extend the warranty of my replacement iphone 5 for 6 month, i mean how can you trust the 2nd replacement if the 1st replacement didnt last long

    Sent iphone 5 3x for repair ( 1 repair and 2 replacement ) in a span of 2 months.. And i just ask apple care to extend the warranty of my replacement iphone 5 for 6 month and they reject it,  i mean how can you trust the 2nd replacement if the 1st replacement didnt last even for just 1 month? Really dissapointed!!

    No, really just want to voice out my dissapointments with apple care.. Im just really ****** off..

  • How can I save to the same map every time when printing pdfs?

    How can I save to the same map every time when printing pdfs?
    Finder points to the document map even when I chose a different map recently.
    I often print series of pdfs from the print dialog box, I'd like to choose the map to save to and then have all subsequent pdf prints automatically directed to the same map until I decide otherwise.

    that link seems to be broken right now:
    403 Error - Forbidden  - No cred, dude.

  • I have downloaded the most recent version of Iphoto and it no longer works period. I lost all photos, and the app crashes every time I try to open it. How can this be fixed?

    I have downloaded the most recent version of Iphoto and it no longer works period. I lost all photos, and the app crashes every time I try to open it. How can this be fixed?

    Post the first 50 lines only of the crash report
    Regards
    TD 

  • Can't locate the right iPhoto Library

    I have a friend who can't find the right iPhoto Library.  Mac OS X 10.7 or 8...  latest iPhoto.  Searching in Finder for "iPhoto Library" produces no results, even though we see an iPhoto Library item in her Pictures folder (not the right one).
    So this leads me to believe that for some reason Finder won't display that....not sure why it would do that...but anyway.  She's a professional photographer, and doesn't know the name of most of the photos, because they are photos name by the camera.  She tried to find some with just the .jpg extension but everything was email or web based results that we found.  Thousands upon thousands.  This has happened before, and it was fixed, but we can't remember how. 
    Any help on how to locate the right version would be great.  Also, her Time Machine only seems to have the same one as in Pictures.  I think the iPhoto Library she uses is located somewhre else on the drive. 
    I also tried Locate and Find in the terminal, but neither brought up anything.
    Thanks in advance.

    Download and use  Find Any File to search for "Library6.iPhoto" which is a file in every iPhoto Library.  If there's another library on your hard drive FAF will find it as it can search areas that Spotlight can't.
    OT

  • I have a iphone 4s with ios 8.3 and I used to be able to sync only a week or two of emails from outlook email.  Now it won't let me do that and it syncs all my inbox.  How can I change it back to sync less.  I can't find the right place.

    I have a iphone 4s with ios 8.3 and I used to be able to sync only a week or two of emails from outlook email.  Now it won't let me do that and it syncs all my inbox.  How can I change it back to sync less.  I can't find the right place.

    I understand, as that was the place I would change it before 8.3 but now that option of Mail Days to Sync is not available?  Any idea why that would be?

  • Can I gift the same app multiple times to multiple users?

    Can I gift the same app multiple times to multiple users?
    Like, if I wanted to buy iMovie for 3 friends, could I buy the app as a gift 3 times?

    You should be able to gift it multiple times
    Gifting content : http://support.apple.com/kb/HT2736

  • Save password? how the **** can I get rod of this function!!! every time I sign to a place if I wanted to safe a bloody password I would do that!!!

    save password? how the **** can I get rod of this function!!! every time I sign to a place if I wanted to safe a bloody password I would do that!!!

    It is the same in the latest version 7.0.2. You can shut off the saving and auto filling of passwords by clicking in the menu bar at the top Safari > Preferences and then select Autofill and uncheck the passwords box. Also check the next pane as well.

Maybe you are looking for

  • How to merge two Apple IDs into one?

    Is it possible to merge two IDs into one?  I don't want to use the 'hotmail.com' extension anymore.

  • Using a SQL user-defined function in Crystal Reports XI

    Post Author: JoannKarp CA Forum: Formula Is it possible to use a user defined function in SQL and use this in multiple Crystal reports? JoannKarp

  • How Can I add a object to the scene Graph ??

    Hi all! I have set up a 3d scene Graph. Now I want to add a object (for example,a cone) to the scene Graph. How can I do ??? thanks in advance!

  • Pubkey with expired Accounts

    Hello, I mentioned that a SSH-Login with Pubkey-Auth doesn't work in Solaris with expired Passwords. It just askes for a password. For example an SSH-Login with Pubkey doesn't work with.... grep userxy /etc/shadow userxy1:$2a$04$mymegahash:0:0:90::::

  • AD/DC/DNS From 2008 R2 to 2012

    Hello I have a Domain Controller (2008 R2), a secondary DC (2008 R2). My new server is a Proliant Gen8 380p. My new server I would like to put 2012 server on it ... it will be replacing the main domain controller (2008 R2). It currently seconds as a