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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:50:21+00:00 2026-05-13T16:50:21+00:00

I have this SQL query but its running soo slow, SELECT wr.wr_re_id as ReID,

  • 0

I have this SQL query but its running soo slow,

SELECT 
    wr.wr_re_id as ReID, 
    sum(wr.wr_total) as PRTotal 
FROM 
    Workorder2 w
    INNER JOIN 
    Workorder_Row2 wr ON wr.wr_wo_id = w.wo_id
WHERE 
    (w.wo_type = 1 or w.wo_type = 2)
    AND wr.wr_row_type = 2
    AND w.wo_lo_id like '%'
    AND w.wo_date_time >= '2010-01-01' 
    AND w.wo_date_time <= '2010-01-31'
    AND wr.wr_wo_id IN
        (SELECT 
            wr2.wr_wo_id 
        FROM 
            Workorder_Row2 wr2 
            INNER JOIN Workorder2 w2 ON w2.wo_id = wr2.wr_wo_id 
            AND w2.wo_date_time >= '2010-01-01' 
            AND w2.wo_date_time <= '2010-01-31' 
        WHERE 
            wr2.wr_row_type = 1)
GROUP BY 
    wr.wr_re_id

any advice how I can speed it up? it takes almost 1min to execute.
I think the problem is with the AND wr.wr_wo_id IN (SELECT ... but i need this to know if there are product sales on the same workorder that contains a threatment.

  • 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-13T16:50:21+00:00Added an answer on May 13, 2026 at 4:50 pm

    Firstly, have a look at your execution plan. It’s hard for us to optimize it as we don’t know your data.

    The thing that jumps out the most is the wr.wr_wo_id IN (SELECT...) part. This would be far more efficient as a join, like this:

    SELECT wr.wr_re_id as ReID, sum(wr.wr_total) as PRTotal 
    FROM Workorder2 w
        INNER JOIN Workorder_Row2 wr on wr.wr_wo_id = w.wo_id
        INNER JOIN (
            SELECT DISTINCT wr2.wr_wo_id
            FROM Workorder_Row2 wr2 
                INNER JOIN Workorder2 w2 on w2.wo_id = wr2.wr_wo_id 
            WHERE w2.wo_date_time >= '2010-01-01' 
                AND w2.wo_date_time <= '2010-01-31'
                AND wr2.wr_row_type = 1
        ) T ON T.wr_wo_id = wr.wr_wo_id
    WHERE (w.wo_type = 1 or w.wo_type = 2)
        AND wr.wr_row_type = 2
        AND w.wo_lo_id like '%'
        AND w.wo_date_time >= '2010-01-01' AND w.wo_date_time <= '2010-01-31'
    GROUP BY wr.wr_re_id
    

    It is worth considering whether it would help to add indices. It depends on how often you update/insert into/delete from those tables, and how selective each column is.

    ADDITIONAL:

    To do the reverse, i.e. to replace WHERE wr.wr_wo_id NOT IN (SELECT...), you would use:

    SELECT wr.wr_re_id as ReID, sum(wr.wr_total) as PRTotal 
    FROM Workorder2 w
        INNER JOIN Workorder_Row2 wr on wr.wr_wo_id = w.wo_id
        LEFT JOIN (
            SELECT DISTINCT wr2.wr_wo_id
            FROM Workorder_Row2 wr2 
                INNER JOIN Workorder2 w2 on w2.wo_id = wr2.wr_wo_id 
            WHERE w2.wo_date_time >= '2010-01-01' 
                AND w2.wo_date_time <= '2010-01-31'
                AND wr2.wr_row_type = 1
        ) T ON T.wr_wo_id = wr.wr_wo_id
    WHERE T.wr2.wr_wo_id IS NULL
        AND (w.wo_type = 1 or w.wo_type = 2)
        AND wr.wr_row_type = 2
        AND w.wo_lo_id like '%'
        AND w.wo_date_time >= '2010-01-01' AND w.wo_date_time <= '2010-01-31'
    GROUP BY wr.wr_re_id
    

    However, it is more readable and (I would guess, though try it) more efficient to use:

    SELECT wr.wr_re_id as ReID, sum(wr.wr_total) as PRTotal 
    FROM Workorder2 w
        INNER JOIN Workorder_Row2 wr on wr.wr_wo_id = w.wo_id
    WHERE (w.wo_type = 1 or w.wo_type = 2)
        AND wr.wr_row_type = 2
        AND w.wo_lo_id like '%'
        AND w.wo_date_time >= '2010-01-01' AND w.wo_date_time <= '2010-01-31'
        AND NOT EXISTS (
            SELECT NULL
            FROM Workorder_Row2 wr2 
                INNER JOIN Workorder2 w2 on w2.wo_id = wr2.wr_wo_id 
            WHERE w2.wo_date_time >= '2010-01-01' 
                AND w2.wo_date_time <= '2010-01-31'
                AND wr2.wr_row_type = 1
                AND wr2.wr_wo_id = wr.wr_wo_id
        )
    GROUP BY wr.wr_re_id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Right now I have this SQL query which is valid but always times out:
I have this SQL query select case when AllowanceId is null then 2 else
I have this sql query: SELECT S.SEARCH, S.STATUS, C.TITLE AS CategoryName, E.SEARCH_ENGINES AS Engine,
I have this SQL query: SELECT DISTINCT tb.SERIAL_NUM, tb.REVISION_NUM, tb.JOB_NUM, tb.JOB_TOTAL, tb.TEST_PROC, tb.BOX_NUM, tb.TEST_BENCH,
I have a SQL query running using something like this: PreparedStatement pstmt = dbConn.prepareStatement(sqlQuery);
I have this query that's running too slow. I'm not sure what all info
I have this SQL query (in T-SQL): --DECLARE @strTerm varchar(300) --SET @strTerm = 'c'
I have this SQL update query: UPDATE table1 SET table1.field1 = 1 WHERE table1.id
I have built a T-SQL query like this: DECLARE @search nvarchar(1000) = 'FORMSOF(INFLECTIONAL,hills) AND
I have a SQL query in my ASP.net web app that looks like this:

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.