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

  • Home
  • SEARCH
  • 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 6018107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:13:38+00:00 2026-05-23T03:13:38+00:00

I have run into a rather weird problem. I have created the following query

  • 0

I have run into a rather weird problem. I have created the following query in SQL Server

SELECT * FROM leads.BatchDetails T1
INNER JOIN leads.BatchHeader h ON T1.LeadBatchHeaderId = h.ID
WHERE 
 T1.LeadBatchHeaderId = 34  
  AND (T1.TypeRC = 'R' OR h.DefaultTypeRC = 'R')    
 AND EXISTS (SELECT ID FROM leads.BatchDetails T2 where 
            T1.FirstName = T2.FirstName AND 
            T1.LastName = T2.LastName AND 
            T1.Address1 = T2.Address1 AND
            T1.City = T2.City AND                     
            T1.[State] = T2.[State] AND                     
            T1.Zip5 = T2.Zip5 AND                     
            T1.LeadBatchHeaderId = T2.LeadBatchHeaderId 
            and t2.ID < t1.ID
            AND (T2.TypeRC = 'R' OR h.DefaultTypeRC = 'R' )
     )

It runs decently fast in 2 seconds. When formatting the code I accidently added an additional SPACE between AND + EXISTS so the query look like this.

SELECT * FROM leads.BatchDetails T1
INNER JOIN leads.BatchHeader h ON T1.LeadBatchHeaderId = h.ID
WHERE 
 T1.LeadBatchHeaderId = 34  
  AND (T1.TypeRC = 'R' OR h.DefaultTypeRC = 'R')    
 AND  EXISTS (SELECT ID FROM leads.BatchDetails T2 where 
            T1.FirstName = T2.FirstName AND 
            T1.LastName = T2.LastName AND 
            T1.Address1 = T2.Address1 AND
            T1.City = T2.City AND                     
            T1.[State] = T2.[State] AND                     
            T1.Zip5 = T2.Zip5 AND                     
            T1.LeadBatchHeaderId = T2.LeadBatchHeaderId 
            and t2.ID < t1.ID
            AND (T2.TypeRC = 'R' OR h.DefaultTypeRC = 'R' )
     )

All of a sudden the query takes 13 seconds to execute.
I am running SQL Server in an isolated sandbox environment and I have even tested it on a different sandbox. I also checked the executed query in profiler, the reads are virtually the same, but CPU time is way up

If this is not weird enough, it’s getting weirder. When I change SELECT * FROM to SELECT Field1, ... FROM at the top of the query the execution takes over 3 minutes.

I have been working with SQL Server for 10 years and never seen anything like this.

Edit:
After following the suggestions below it appears that queries are “white-space-sensitive”. However I still have no idea why the SELECT * FROM is so much faster than SELECT Field1, ... FROM

  • 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-23T03:13:40+00:00Added an answer on May 23, 2026 at 3:13 am

    I would guess that you’re dealing with two different cached query plans:

    • You you ran the query once, with a certain set of parameters. SQL Server determined an appropriate query plan, and stored that query plan “Auto-parametrized”, in other words replacing the values you provided with variables, for the purposes of the query plan.
    • You then ran the same query again, with different parameters. The query gets auto-parameterized, and matches the existing cached query plan (even though that query plan may not be optimal for the new parameters provided!).
    • You then run this second query again, with your extra space. This time, the auto-parametrized query does NOT match anything in the cache, and therefore gets its own plan based on THIS set of parameters (remember, the first plan was for a different set of parameters). This query plan happens to end up faster (or slower).

    If this is truly the explanation, you should be able to make the effect go away, by running DBCC FREEPROCCACHE: http://msdn.microsoft.com/en-us/library/ms174283.aspx

    There’s lots of stuff on auto-parameterization out there, I personally liked Gail Shaw’s series:

    http://sqlinthewild.co.za/index.php/2007/11/27/parameter-sniffing/
    http://sqlinthewild.co.za/index.php/2008/02/25/parameter-sniffing-pt-2/
    http://sqlinthewild.co.za/index.php/2008/05/22/parameter-sniffing-pt-3/

    (for the record, I have no idea whether SQL Server eliminates/normalizes whitespace before storing an auto-parameterized query plan; I would have assumed so, but this entire answer asssumes that it doesn’t!)

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

Sidebar

Related Questions

I have run into a rather weird little problem. In the following code I
I've just started to use linq to sql and have run into a problem
I have run into a rather odd problem, my app crashes if I try
I have run into a common, yet difficult problem. I do not use Photoshop
I have run into a bit of a tricky problem in some C++ code,
it seems I have run into a problem with Internet Explorer 7. I have
I am working on a web application and I have run into the following
I'm using SWFUpload to handle file uploads and have run into a problem where
I have run into a problem w/ my model for databinding in WPF. I
NOTE: I have solved the majority of this problem but have run into a

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.