Click here to Skip to main content
15,891,734 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created a query that inserts records into a [MatchingRowsOutput-RU] Table by joining an [SWBOM] table and a [Walls Routing Table].
In the first part of the query its gathers all the records based on an = condition using an inner join. In the second part, it gathers all the non exact records. On the inner join for that, the IN condition had to be used which is really slowing down the retrieval. It takes about 1.5 min to run.

I would like to optimize this query for better result times. Being quite new to sql or programming for that matter, I am not familiar with what syntax I could use to replace this IN condition. I had to use this IN condition because it is expecting many records to be returned.

Any help is greatly appreciated.

Below is the code I have written so far.

What I have tried:

Insert into [SSIS-POC].dbo.[MatchingRowsOutput-RU]

    (cprj, PartNumber, SWITEM, QTY, mitm, opno, tano, cwoc, mcno, sutm, rutm, prte, mnoc, mcoc, olap, exin, subr, indt, exdt, bfls, qpnt, nnts, desn, txta)
SELECT
 PartList.cprj,
 PartList.PartNumber,
 PartList.SWITEM,
 PartList.QTY,
 RUMaster.mitm,
 RUMaster.opno,
 RUMaster.tano,
 RUMaster.cwoc,
 RUMaster.mcno,
 RUMaster.sutm,
 RUMaster.rutm,
 RUMaster.prte,
 RUMaster.mnoc,
 RUMaster.mcoc,
 RUMaster.olap,
 RUMaster.exin,
 RUMaster.subr,
 RUMaster.indt,
 RUMaster.exdt,
 RUMaster.bfls,
 RUMaster.qpnt,
 RUMaster.nnts,
 RUMaster.desn,
 RUMaster.txta
 
 

 FROM 
    [SSIS-POC].dbo.SWBOM as PartList
INNER JOIN
    ItemMasterRoutingPOC.dbo.[Walls Routing Table] AS RUMaster ON RUMaster.mitm = PartList.PartNumber 

UNION ALL

SELECT 
 PartList.cprj,
 PartList.PartNumber,
 PartList.SWITEM,
 PartList.QTY,
 RUMaster.mitm,
 RUMaster.opno,
 RUMaster.tano,
 RUMaster.cwoc,
 RUMaster.mcno,
 RUMaster.sutm,
 RUMaster.rutm,
 RUMaster.prte,
 RUMaster.mnoc,
 RUMaster.mcoc,
 RUMaster.olap,
 RUMaster.exin,
 RUMaster.subr,
 RUMaster.indt,
 RUMaster.exdt,
 RUMaster.bfls,
 RUMaster.qpnt,
 RUMaster.nnts,
 RUMaster.desn,
 RUMaster.txta
 
FROM 
    [SSIS-POC].dbo.SWBOM as PartList

INNER JOIN
ItemMasterRoutingPOC.dbo.[Walls Routing Table] AS RUMaster ON 

RUMaster.mitm IN  --**********PROBLEM IS HERE WITH THE IN CONDITION

	(SELECT mitm FROM ItemMasterRoutingPOC.dbo.[Walls Routing Table]
	
	
	WHERE mitm LIKE (LEFT(PartList.PartNumber,8) + '-%')
	--WHERE mitm = PartList.itlu

)
LEFT JOIN
 
(    SELECT
        PartList.PartNumber as FoundPartNumber
    FROM 
       [SSIS-POC].dbo.SWBOM as PartList
    INNER JOIN
        ItemMasterRoutingPOC.dbo.[Walls Routing Table] AS RUMaster ON RUMaster.mitm = PartList.PartNumber 


) IT ON IT.FoundPartNumber = PartList.PartNumber
WHERE
  IT.FoundPartNumber IS NULL;
Posted
Updated 7-Apr-20 8:15am
Comments
CHill60 7-Apr-20 12:21pm    
Have a read of this SQL server performance - Death by correlated subqueries - SQL Service[^] - no need for a sub-query - just use another join
[no name] 7-Apr-20 12:24pm    
You optimize the "selects", then "unions" etc. Use "intermediate" tables so you can time and count. The SQL "optimizer" isn't magic and there are no shortcuts to performance.
Member 14778402 7-Apr-20 12:40pm    
i ran just that one section of code that deals with the non-exact with the IN condition with the one select and inner join and that is where all the time is being consumed. I'm not sure if that one particular section of the code is deemed correlated. There has to be some other syntax rather than using the IN condition in order to retrieve many records. The other sections treated individually run quick. One of the tables it retrieves records from only contains 231,000 records.

1 solution

So first you join two tables
Then you add the rows from a second join of those two tables using a like, where you have removed the rows from the first join.

I believe what you want is this:
SQL
INSERT INTO [SSIS-POC].dbo.[MatchingRowsOutput-RU] (
        cprj
       ,PartNumber
       ,SWITEM
       ,QTY
       ,mitm
       ,opno
       ,tano
       ,cwoc
       ,mcno
       ,sutm
       ,rutm
       ,prte
       ,mnoc
       ,mcoc
       ,olap
       ,exin
       ,subr
       ,indt
       ,exdt
       ,bfls
       ,qpnt
       ,nnts
       ,desn
       ,txta
        )
SELECT  PartList.cprj
        ,PartList.PartNumber
        ,PartList.SWITEM
        ,PartList.QTY
        ,RUMaster.mitm
        ,RUMaster.opno
        ,RUMaster.tano
        ,RUMaster.cwoc
        ,RUMaster.mcno
        ,RUMaster.sutm
        ,RUMaster.rutm
        ,RUMaster.prte
        ,RUMaster.mnoc
        ,RUMaster.mcoc
        ,RUMaster.olap
        ,RUMaster.exin
        ,RUMaster.subr
        ,RUMaster.indt
        ,RUMaster.exdt
        ,RUMaster.bfls
        ,RUMaster.qpnt
        ,RUMaster.nnts
        ,RUMaster.desn
        ,RUMaster.txta
FROM    [SSIS-POC].dbo.SWBOM AS PartList
JOIN    ItemMasterRoutingPOC.dbo.[Walls Routing Table] AS RUMaster
    ON  RUMaster.mitm = PartList.PartNumber
    OR  RUMaster.mitm LIKE (LEFT(PartList.PartNumber, 8) + '-%')
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900