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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:58:28+00:00 2026-05-23T17:58:28+00:00

I am writing a stored procedure that works with a heavy select statement. The

  • 0

I am writing a stored procedure that works with a heavy select statement. The stored procedure takes in about 15 parameters to act as a filter, all of which are NULLable.

There are two things the parameters generally do – check if x is between high and low or check if column value is in y.

My main concern is how I write the where clause.

Example: Dynamic SQL is notoriously slow, so I don’t want to write the where clause and then pass it to exec.

I don’t want to do if High = null then High = max because then I’ll still have a between statement that takes up processing power and has no use.

I don’t want to do an (if High = null or X <= High) because the null check would still be processed for every row and I heard rumors that would mess with indexes.

In short, I’m looking for guidance in the best practice that takes performance into account.

  • 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-23T17:58:28+00:00Added an answer on May 23, 2026 at 5:58 pm

    Dynamic SQL used to be slow because execution plans for dynamically generated SQL weren’t cached. This is no longer the case and execution plans for dynamic SQL queries will be cached as long as the query text is identical. This means that you should:

    • Use parameters to avoid your parameter values altering the query text
    • Try to sort your where clauses so that they appear in the same order

    As long as you do this then your query plans should be cached (one for each possible query variation) and dynamic SQL won’t be any slower than any normal query.


    Your other suggestions (setting various parameters to NULL) is to be avoided and may in fact perform quite badly – statements can only have one cached query plan, however the optimal query plan will depend on the parameters and may be very different depending on the values supplied.

    For example one set of parameters might result in most of the table being returned, in which case a table scan might be optimal. Another set of parameters might result in a single row being returned in which case a row lookup might be optimal. SQL Server must choose to cache one of these two plans (probably the most optimal one based on the parameters supplied the first time the query is run), however whatever plan it chooses the query will probably perform badly in the opposite scenario. (this is an over simplification, however I have seen variations of this happen and it can have a very significant performance impact).

    Ultimately this will mean that either:

    • Most of the time the query plan chosen probably wont be optimal, or
    • You can force SQL Server to generate a new plan for each execution which always results in a reasonable execution plan, however eliminates the advantages of plan caching

    The other disadvantage of your alternative aproaches is that it will result in a more complex query, which can make it difficult for the query optimiser to optimise the query properly.

    For these reasons I’d say that dynamic SQL is definitely the better choice.

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

Sidebar

Related Questions

I am writing a stored procedure that takes one input argument and can return
Say I've got a function or stored procedure that takes in several VARCHAR parameters.
I have been tasked with writing a stored procedure that takes a user specified
Writing a stored procedure that will have multiple input parameters. The parameters may not
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 am writing an install script that installs all tables, stored procedures, views, full
I am writing a stored procedure with 3 parameters and my where clause changes
I'm writing a stored procedure that should sync a table from our production environment

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.