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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T02:48:03+00:00 2026-05-22T02:48:03+00:00

I’m running SQL Server 2008. I’ve built a large search query (contained in a

  • 0

I’m running SQL Server 2008. I’ve built a large search query (contained in a user defined function) with many optional parameters. A simplified version of the result is something like:

Declare @optionalSubTableParameter as userDefinedTableType READONLY

select id
from table t

--here is optional parameter 1 (there are quite a few of these)
outer apply(
 select top (1) st.item
 from subTable st
 inner join @optionalSubTableParameter ostp
 on (ostp.value = st.item or ostp.value is null)
 where st.index = t.index 
 and ostp.value is not null
 -- also tried: (select top(1) * from @optionalSubTableParameter) is not null
)someParam

where (someParam.item is not null 
or (select top(1) * from @optionalSubTableParameter) is null)

So, the problem lies in the execution plan, I seem to be spending time on:

clustered index seek (clustered)

[subTable].[IX_subTableIndex..

Cost:8%

I know 8% isn’t much, but this gets repeated 6 times (and soon to be a few more), so its already 48% of my execution time.

I thought by having the check of (@optionalSubTableParameter is not null) within the outer apply, I would avoid computations like the clustered index seek on an unneeded table (when there is no parameter specified). If anyone can help explain if there is a way for me to avoid this computation, that would be great!

Thanks in advance, and let me know if I can clarify anything (this is a grossly simplified version of the query that I’m actually running).

I apologize if there are any duplicate posts, but I had no luck finding an answer on my own.

  • 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-22T02:48:04+00:00Added an answer on May 22, 2026 at 2:48 am

    First, this is only 8% of your execution cost. If you have performance issues, keep looking because this won’t be the fix.

    Second, you are still doing the index seek because of this line:

    where st.index = t.index

    You MAY be able to eliminate it by switching the order of the WHERE clause in that outer apply but I wouldn’t count on it.

    Since it’s an AND I think it may evaluate both components. Someone else could probably address if that short circuits or not.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I need a function that will clean a strings' special characters. I do NOT
I want to construct a data frame in an Rcpp function, but when I
I am currently running into a problem where an element is coming back from
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

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.