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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T02:56:53+00:00 2026-06-08T02:56:53+00:00

I have a query that takes about 3 minutes to run. The Query is

  • 0

I have a query that takes about 3 minutes to run. The Query is being used for reporting and i would like it to be more efficient. I think the bottle neck is the inner joins, I could be wrong just want to know if you guys had any solutions.

    SELECT DISTINCT 
    SUBSTRING(T1.DateTime, 1, 4) AS Year, 
    SUBSTRING(T1.DateTime, 5, 2) AS Month, 
    SUBSTRING(T1.DateTime, 7, 2) AS Day, 
    T1.PipeNr, T1.SalesOrder, T1.JobNr, SIST.DefectCode AS DefectRef, 
    DEFCODES.DefectCode, DEFCODES.DefectDesc, SIST.ODYes, SIST.LocationWrap, 
    T1.OWS0601, T1.OWS0602, T1.OWS0603, T1.CrossWeld

    FROM PIPEDB.dbo.SIST INNER JOIN PIPEDB.dbo.MPIPEID T1 ON PIPEDB.dbo.SIST.PipeNr = T1.PipeNr INNER JOIN 
    PIPEDB.dbo.DEFCODES ON PIPEDB.dbo.SIST.DefectCode = PIPEDB.dbo.DEFCODES.DefectRef

    WHERE PIPEDB.dbo.SIST.DefectCode
    IN (
    SELECT Top (10) PIPEDB.dbo.SIST.DefectCode

    FROM PIPEDB.dbo.SIST INNER JOIN PIPEDB.dbo.MPIPEID T2 ON PIPEDB.dbo.SIST.PipeNr = T2.PipeNr INNER JOIN 
    PIPEDB.dbo.DEFCODES ON PIPEDB.dbo.SIST.DefectCode = PIPEDB.dbo.DEFCODES.DefectRef

    WHERE SUBSTRING(T2.DateTime, 1, 4) = SUBSTRING(T1.DateTime, 1, 4) AND SUBSTRING(T2.DateTime, 5, 2) = SUBSTRING(T1.DateTime, 5, 2) AND 
    SUBSTRING(T2.DateTime, 7, 2) = SUBSTRING(T1.DateTime, 7, 2)

    GROUP BY PIPEDB.dbo.SIST.DefectCode

    ORDER BY COUNT(PIPEDB.dbo.SIST.PipeNr) DESC) 

    AND (PIPEDB.dbo.DEFCODES.DefectDesc IN ("Cut To Remove Defect")) AND 
    ((CASE WHEN T1.CrossWeld = 1 THEN 1 WHEN T1.CrossWeld = 0 THEN 2 END) = @Crossweld OR @Crossweld = 0)
  • 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-06-08T02:56:54+00:00Added an answer on June 8, 2026 at 2:56 am

    The issue is that you are making IN subquery as a correlated subquery which in turn is causing the performance issue.For each rows from the main clause (or outer from) the IN subquery is being executed and thus it is taking time.Each execution of Inner subquery could be expensive based on size of tables and most probably it will use a nested loop join for in subquery. Also if number of rows from outer query are quite high then the expensive IN subquery is being executed lots of time and thus degrading the performance of query.I would suggest that have a look at the logic once again and make sure that it is giving correct data.

    See below.I have tried to reproduce your issue.The cost for this query is 2045 which is very huge and off course it is going to take time. It clocks around 10 million io’s 176 sec of CPU and 48 seconds elapsed time.

    That’s not it.I expected to have just 10 productid(I am sure you alos expect to have just 10 defectcodes) but i could see lots of other productids just because of the correlated subquery.

    set statistics io,time on
    
    go
    
    select * from Sales.SalesOrderHeader soh inner join sales.SalesOrderDetail sod
    on sod.SalesOrderID=soh.SalesOrderID
    where sod.ProductID in (
    select top 10 ProductID
    from Sales.SalesOrderHeader soh1 inner join sales.SalesOrderDetail sod
    on sod.SalesOrderID=soh1.SalesOrderID
    where datepart(yy,soh.OrderDate) = DATEPART(yy,soh1.OrderDate)
    and datepart(mm,soh.OrderDate) = DATEPART(mm,soh1.OrderDate)
    

    )

       order by sod.ProductID
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this query that takes about 5 minutes to run in Oracle: select
I have a query that looks like this: it takes a list of IDs
I have a query that's used in a reporting system of ours that sometimes
I run a procedure that takes about 20 minutes to complete, I just wonder
I have a sql query (stored proc) that takes about 8-10seconds to return before
I have a query that takes about a minute to complete since it deals
I have an SQL query (below) that essentially takes a student from tbStudents, and
I have a complex Query (Without any locking hints) that takes data from many
I have an SQL Query (For SQL Server 2008 R2) that takes a very
I have a mysql db query that I'd like to take and condense into

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.