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 252953
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:43:16+00:00 2026-05-11T21:43:16+00:00

I am writing a stored procedure to perform a dynamic search that spans 10+

  • 0

I am writing a stored procedure to perform a dynamic search that spans 10+ database tables. With millions of records in each table and a dynamic set of search parameters*, I am having some trouble optimizing the procedure.

Is there a “best practice” for building these kinds of queries? E.g. Use strings to build a dynamic query, use a huge list of IF THEN .. ELSE statements, etc? Can anyone provide a simple example or point me to some literature that will help? Here’s some psuedocode for the stored procedure I am developing, which accepts a collection of parameters and a ref cursor.

v_query = "SELECT .....";
v_name = ... -- retrieve "name" parameter from collection
if v_name is not null then
   v_query := v_query || ' AND table.Name = ' || v_name;
end if;
open search_cursor for v_query;
...

*By “dynamic set of search parameters,” I mean that I pass in a collection of parameters. I figured this would be easier than making the caller pass in 20 parameters if they only want to search on one.

  • 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-05-11T21:43:16+00:00Added an answer on May 11, 2026 at 9:43 pm

    There are problems with using the static query approach; also be very careful about using the CURSOR_SHARING=FORCE option – it can really raise hell with your system if you haven’t done a coverage test to ensure that all your other queries will work the way you want.

    Problems with static queries:

    1. The (x is null or x = col) predicates tend to kill any chance of using indexes. Since the query plan is computed at the time query is parsed the first time, the indexes you use will be based on the values for the first run of the query; later runs, which may not constrain on the same columns, will still use the same indexes.

    2. Having one static statement with substitution variables will prevent the optimizer from making an intelligent choice about which index to use based on the data distribution. In a dynamic query (or in the first run of a query with bind variables), Oracle will see how selective your constraint is; a highly selective constraint will become a prime candidate for index use. For example, if your table had a row for every person in the U.S., STATE=’Alaska’ will be much more likely to use the index on STATE than STATE=’California’.

    Of course, in both these cases, if the dynamic columns in your WHERE clause are not indexed anyway, it doesn’t matter, although I’d be surprised if that were the case in a database the size you’re talking about.

    Also, consider the real cost of all that hard parsing. Yes, hard parses serialize system resources, which makes them expensive, but only in the context of high volume queries. By their nature, ad-hoc queries do not get run very often. The cost you pay for all the hard parses you incur in an entire day will likely be hundreds of times less than the cost of a single query that uses the wrong indexes.

    In the past, I’ve implemented these systems pretty much like you’ve done here – a base query portion, then iterating over a constraint list and adding WHERE clause predicates. I don’t think it’s hard for someone to maintain or understand, especially if you’re talking about constraints that don’t involve adding a lot of subqueries or extra tables to the FROM clause.

    One thing to consider: If this system is primarily an offline one (in other words, not constantly being updated or inserted into – populated by periodic loads of bulk data), you may want to look into using BITMAP indexes. Bitmap indexes differ from regular b-tree indexes in that multiple indexes on a single table can be used simultaneously, and bitmap indexes are much, much smaller on disk than b-trees. They work very well for applications like this – where you will have a variety of constraints that can’t be defined at design time. You will only want to put bitmap indexes on columns that have relatively few distinct values – say, one value constitutes no less than 1/1000 of the table – so don’t use bitmaps on unique columns.

    However, the downside is that bitmap indexes will noticeably degrade the performance of inserts and updates. The best practice for bitmaps is to use them in data warehouse applications, and they are dropped prior to loads and recreated afterwards.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a general search Stored Procedure to search in a table based on
I'm writing a stored procedure that should sync a table from our production environment
I am writing a stored procedure that will get results from one table then
I'm writing a stored procedure to update multiple records based on a table variable
I'm writing a stored procedure to look in two tables PersonTbl, UserTbl. First search
I am writing a stored procedure that merges two tables with identical schema from
I am writing a stored procedure that when completed will be used to scan
I am using SQL Server. I'm writing a stored procedure that executes a series
i am writing stored procedures in MySQL that return values; CREATE PROCEDURE getCustomerById (id
I'm writing a stored procedure and I want to return 0 records when something

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.