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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:14:05+00:00 2026-05-26T12:14:05+00:00

Original Query I’m trying to rewrite: SELECT Table1.* FROM Table1 INNER JOIN Table2 ON

  • 0

Original Query I’m trying to rewrite:

SELECT Table1.* 
FROM Table1
INNER JOIN Table2 ON Table2.[IDENTITY]=Table1.ID
WHERE Table2.Field1 = @value AND Table2.Field2 = '1' AND Table1.ID in
(
   select Table1.ID from Table1 where Table1.Number in
   (select Table1.Number from Table1 where Table1.ID=@ID) 
)

Note: In real query, I list all the columns instead of using Table1.*

This query is slightly confusing, especially since I changed the names etc for posting. In short it needs to take an ID that’s passed in and find all the Table1.Number fields that have that ID. There is a Many to 1 relationship between Number and ID. So then once all the Numbers are found I need to then find the total list of IDs that use any of those numbers.

When I look at the statistics when I run the query I get

Table 'Table1'. Scan count 3873, logical reads 135255, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.
Table 'Table2'. Scan count 0, logical reads 7995, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.

I feel the culprit is the nested subquery. I’ve been trying to write this a different way for a while now, but I just can’t seem to figure it out completely. I rewrote the 2nd nested query as:

WHERE Table2.Field1 = @value AND Table2.Field2 = '1' AND Table1.ID in
(
   select Table1.ID from Table1
   INNER JOIN Table1 AS Table3 ON Table1.Number = Table3.Number 
   where Table3.MessageID=@ID
)

Unfortunately this resulted in identical statistics. I can’t quite figure out how to remove the 2nd “in” statement.

Is this the best approach? Is there a better one? Am I right that this kind of subquery is very bad performance wise and thus causing the high scans and logical reads the IO statistics are showing me?

EDIT: Original Query used Table2.[IDENTITY]=Table1.MessageID. That should be Table2.[IDENTITY]=Table1.ID. I’ve updated the above query to reflect 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-26T12:14:05+00:00Added an answer on May 26, 2026 at 12:14 pm

    I believe you’re correct in that the nested subquery is causing the statistics that you’re seeing. When you have a value that is translating into a set like this I often find that the best performing solution is to shove the results of the subquery into a temp table and then join against that instead. This prevents the row-based execution of the subquery and should dramatically improve your performance.

    The contents of the subquery reference a static statement variable but do not reference any elements from the outer statement. This means that it is not a correlated subquery and that we’re simply performing the same operation over and over for every row. When you see a subquery like this it’s an easy optimization choice to move the operation outside of the select and reference the data in a more appropriate way.

    For the example you provided you would do something like

    select distinct Table1.ID 
    INTO #myTemp
    from Table1 where Table1.Number in
    (select Table1.Number from Table1 where Table1.ID=@ID) 
    

    This would turn your initial query into

    SELECT Table1.* 
    FROM Table1
    INNER JOIN Table2 ON Table2.[IDENTITY]=Table1.MessageID
    INNER JOIN #myTemp a on Table1.ID = a.ID
    WHERE Table2.Field1 = @value AND Table2.Field2 = '1' 
    

    I’m assuming you’re using SQL2008 R2 so your syntax may differ depending on your RDBMS.

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

Sidebar

Related Questions

The original query looks like this (MySQL): SELECT * FROM books WHERE title LIKE
So here is the original query SELECT SUM(PickQty), SUM(ReqQty), AssignmentID, StopID FROM PickRequest GROUP
Suppose we got a original query as follows: SELECT A, B, C FROM tblA
Here is my original query... SELECT `id` FROM `properties` LIMIT 10, 20 The LIMIT
There are a limit to use INNER JOIN? Original Query (Work just fine) I
I have the following query: select col1, sum( col2 ), count( col3 ) from
I have this query in T-SQL 2008: SELECT a.Amount / ( SELECT SUM(b.Amount) FROM
I am adding a 5th table to an existing join. The original query will
Here's the situation I'm dealing with. The original query I had was: SELECT c.CustID,
I've got a query in similar structure to below SELECT blah FROM A, B,

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.