Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9074213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T18:35:02+00:00 2026-06-16T18:35:02+00:00

I have a common problem with ORACLE in following example code: create or replace

  • 0

I have a common problem with ORACLE in following example code:

create or replace procedure usp_test
(
  p_customerId number,
  p_eventTypeId number,
  p_out OUT SYS_REFCURSOR
)
as
begin

  open p_out for
    select e.Id from eventstable e
    where 
      (p_customerId is null or e.CustomerId = p_customerId)
      and
      (p_eventTypeId is null or e.EventTypeId = p_eventTypeId)
    order by Id asc;

end usp_test;

The “OR” in “(p_customerId is null or e.CustomerId = p_customerId)” kills procedure performance, because optimizer will not use index (i hope for index seek) on “CustomerId” column optimally, resulting in scan instead of seek. Index on “CustomerId” has plenty of distinct values.

When working with MSSQL 2008 R2 (latest SP) or MSSQL 2012 i can hint the query with “option(recompile)” which will:

  1. Recompile just this query
  2. Resolve values for all variables (they are known after sproc is called)
  3. Replace all resolved variables with constants and eliminate constant
    predicate parts

For example: if i pass p_customerId = 1000, then “1000 is null” expression will always be false, so optimizer will ignore it.
This will add some CPU overhead, but it is used mostly for rarely called massive reports procedures, so no problems here.

Is there any way to do that in Oracle? Dynamic-SQL is not an option.

Adds

Same procedure just without “p_customerId is null” and “p_eventTypeId is null” runs for ~0.041 seconds, while the upper one runs for ~0.448 seconds (i have ~5.000.000 rows).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T18:35:03+00:00Added an answer on June 16, 2026 at 6:35 pm
    CREATE INDEX IX_YOURNAME1 ON eventstable (NVL(p_customerId, 'x'));
    CREATE INDEX IX_YOURNAME2 ON eventstable (NVL(p_eventTypeId, 'x'));
    
    create or replace procedure usp_test
    (
      p_customerId number,
      p_eventTypeId number,
      p_out OUT SYS_REFCURSOR
    )
    as
    begin
    
      open p_out for
        select e.Id from eventstable e
        where 
            (NVL(p_customerId, 'x') = e.CustomerId OR NVL(p_customerId, 'x') = 'x')
        AND (NVL(p_eventTypeId, 'x') = e.EventTypeId OR NVL(p_eventTypeId, 'x') = 'x')
        order by Id asc;
    end usp_test;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following (maybe common) problem and it absolutely puzzles me at the
I have a common problem that I never know how to solve with CSS:
This is a fairly common problem to which I have yet to find an
This must be a common problem with a common solution. I have a lot
This is a slightly original variation on a common problem. I have a fairly
I'm trying to change my code to have Oracle do all the heavy lifting.
I have the very common problem of creating an index for an in-disk array
I know this is a common problem but I have stared, read googled and
It must be a very common problem. I have a chained many-to-many relationship like
I think this is a common problem... yet i have not been able to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.