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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:13:52+00:00 2026-05-22T12:13:52+00:00

I have a stored procedure which takes under a second to run normally. Users

  • 0

I have a stored procedure which takes under a second to run normally. Users wanted data from another table in that query, so I merged that data in with a UNION ALL and a bunch of dummy columns that were missing in the new table.

It worked fine in testing, but when we deployed it to an SQL 2000 Server, it started getting timeouts. The old query runs in under a second, and the 2 new queries both run in under a second, but when they’re combined using UNION ALL, the query times out.

Here’s the general idea of how the query looks. The real query has about 20 input parameters and returns about 30 or 40 columns, but this should give the basic idea:

CREATE PROCEDURE dbo.SearchHistory
(
    @Criteria1 bigint,
    @Criteria2 int,
    @Criteria3 varchar(10)
)
AS
BEGIN
    -- Part 1
    SELECT
        A,
        NULL AS B,
        0 AS C,
        D
    FROM TableA
    WHERE @Criteria1 IS NULL
    AND @Criteria3 IS NULL
    AND (A = @Criteria2 OR @Criteria2 IS NULL)

UNION ALL

    -- Part 2
    SELECT
        A,
        NULL AS B,
        0 AS C,
        E
    FROM TableA
    WHERE @Criteria1 IS NULL
    AND @Criteria3 IS NULL
    AND (A = @Criteria2 OR @Criteria2 IS NULL)

UNION ALL

    -- Part 3
    SELECT
        A,
        B,
        C,
        D
    FROM TableB
    WHERE (F = @Criteria1 OR @Criteria1 IS NULL)
    AND (A = @Criteria2 OR @Criteria2 IS NULL)
    AND (G = @Criteria3 OR @Criteria3 IS NULL)
END

In the above example, @Criteria1 is not null, so Part 1 and 2 will return 0 rows, and part 3 is only returning 3 rows. But if I comment out Part 1 and 2, it finishes immediately; if I leave them in, I get a timeout.

How do you convince SQL Server to not mess around with its execution plan in a situation like this?

  • 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-22T12:13:53+00:00Added an answer on May 22, 2026 at 12:13 pm

    I think your problem is because of SQL Server’s parameter sniffing.

    http://elegantcode.com/2008/05/17/sql-parameter-sniffing-and-what-to-do-about-it/

    http://blogs.msdn.com/b/queryoptteam/archive/2006/03/31/565991.aspx

    I have come across it a good few times. There are a few ways around it. E.g. use WITH RECOMPILE as @Biff MaGriff suggested. The easiest way I found to fix is to convert all your input parameters to local parameters, and use the local ones.

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

Sidebar

Related Questions

I have a stored procedure which takes an XML parameter and inserts the data
I have a stored procedure which executes another procedure inside it. The second one
I have an Oracle stored procedure which takes two parameters: a custom data type
I have a Stored procedure which schedules a job. This Job takes a lot
I have an insert stored procedure which takes many parameters - 2 of them
I have a stored procedure which takes as its parameter a varchar which needs
If I have a parametrized stored procedure which takes a varchar(10) value and converts
I have an Informix stored procedure which takes an int and a smallint as
I have a stored procedure in my database that takes a table value parameter,
I have created a stored procedure in SQL Server, which runs immediately from Management

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.