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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:12:51+00:00 2026-05-27T18:12:51+00:00

What is better in a WHERE clause? I have a large table, with a

  • 0

What is better in a WHERE clause?

I have a large table, with a FK to a small table. I can search on the FK directly, or I can join to the FK table and set the WHERE restriction on the joined table. What is better / preferable?

So this:

SELECT lt.* FROM LargeTable lt 
WHERE lt.SomeId in(12,55)

Or this:

SELECT lt.* FROM LargeTable lt 
INNER JOIN SmallTable st ON lt.SomeId=st.ItemId
WHERE st.Id in(12,55)

I tested this with Set statistics time on, but I didn’t expect this as a result. Who can explain what happens here?

First test without join:

(946 row(s) affected)
 SQL Server Execution Times:
   CPU time = 1544 ms,  elapsed time = 1580 ms.

Second test with the join

(946 row(s) affected)
 SQL Server Execution Times:
   CPU time = 2636 ms,  elapsed time = 366 ms.

EDIT: When i do SELECT Id instead of SELECT *, then the first query without join has a lower elapsed time, and the query cost in the execution plan is 25% for no join vs 75% for the query with the join.

  • 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-27T18:12:51+00:00Added an answer on May 27, 2026 at 6:12 pm

    Based on your execution plans, both queries are essentially scanning every record in the entire large table… the second query is simply finding a small set of records from the small table before scanning the large table, which is why the relative cost is 50% for both.

    I’d recommend considering an index on largeTable.SomeId, and then go with the first query:

    SELECT lt.* FROM LargeTable lt 
    WHERE lt.SomeId in(12,55)
    

    EDIT:

    So the big question is why did the query with the join have a shorter duration than the query without the join.

    I think Martin Smith gave the answer to this question:

    Your second one gets a parallel plan the first one doesn’t

    You’ll notice that your first query had a shorter CPU time, but a longer elapsed time. To roughly summarize, your first query took less effort for the server to complete, but your second query used a parallel plan, and enlisted multiple processors to perform the query, so it took less time to complete, but more overall effort.

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

Sidebar

Related Questions

I have some large files (hundreds of MB) that I need to search for
What kind of problems might this cause? Is it better to install in a
Better yet, how can I make My Computer always open in Explorer as well?
any better way to write this ? $(this).parent().parent().find( dd ul).toggle(); update.. I am trying
Is there a better way to flash a window in Java than this: public
Which is better? Or use and OR mapper with SP's? If you have a
I read on MySQL Performance Blog that when tables are large, it is better
I have two T-SQL scalar functions that both perform calculations over large sums of
I have to transfer large files between computers on via unreliable connections using WCF.
I have a web app that allows you to paste a large number of

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.