0

Is there any better way to write the below query for optimize the performance.

INSERT INTO [dbo].[MBQ_All] ([Uniq], [StoreClass], [Store], [code], [ExtendedDescription], [ITEM_CLASS], [SUPPLIER], [Brands], [Min], [Max],[ADS]
           ,[Store+whMBQ])

    SELECT [Uniq], [StoreClass], [Store], [code], [ExtendedDescription], [ITEM_CLASS], [SUPPLIER], [Brands], [Min], [Max],[ADS]
           ,[Store+whMBQ]
    FROM dbo.MBQ_All_1
    WHERE MBQ_All_1.Uniq NOT IN (
            SELECT UNIQ
            FROM dbo.MBQ_All
            );

The table MBQ_All has more than 4,00,000 Rows and MBQ_All_1 has 2,00,000.

I'm using SQL Server 2008.

4
  • can you please share a schema and an EXPLAIN? Is there an index on uniq? Commented Apr 5, 2017 at 13:26
  • There is one non clustered index in the table no uniq index exists.. Commented Apr 5, 2017 at 14:57
  • You will almost certainly get a significant improvement from putting an index on the uniq column on both tables. Commented Apr 5, 2017 at 15:17
  • Yes along with following Query fix added unique column to the table . Thank you for the tip Commented Apr 9, 2017 at 10:41

3 Answers 3

2

Maybe try using NOT EXISTS:

INSERT INTO [dbo].[MBQ_All] ([Uniq], [StoreClass], [Store], [code], [ExtendedDescription], [ITEM_CLASS], [SUPPLIER], [Brands], [Min], [Max],[ADS]
       ,[Store+whMBQ])

SELECT [Uniq], [StoreClass], [Store], [code], [ExtendedDescription], 
[ITEM_CLASS], [SUPPLIER], [Brands], [Min], [Max],[ADS] ,[Store+whMBQ]
FROM dbo.MBQ_All_1
WHERE NOT EXISTS (SELECT *
              FROM dbo.MBQ_All
              WHERE dbo.MBQ_All.UNIQ = MBQ_All_1.Uniq)
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much, now it is taking only 4 secs, before it was more than 1 minute
No worries, glad I could help :)
2

It's a clear case for MERGE statement:

MERGE [dbo].[MBQ_All] Tgt
USING [dbo].[MBQ_All_1] Src ON Tgt.Uniq = Src.Uniq
WHEN NOT MATCHED THEN
    INSERT (    [Uniq],     [StoreClass],     [Store],     [code],     [ExtendedDescription],     [ITEM_CLASS],     [SUPPLIER],     [Brands],     [Min],     [Max],     [ADS],     [Store+whMBQ])
    VALUES (Src.[Uniq], Src.[StoreClass], Src.[Store], Src.[code], Src.[ExtendedDescription], Src.[ITEM_CLASS], Src.[SUPPLIER], Src.[Brands], Src.[Min], Src.[Max], Src.[ADS], Src.[Store+whMBQ]);

Comments

1

One of the options would be to have LEFT JOIN, but I am not sure if it will be faster:

INSERT INTO [dbo].[MBQ_All] 
     ([Uniq], [StoreClass], [Store], [code], [ExtendedDescription], [ITEM_CLASS], [SUPPLIER], [Brands], [Min], [Max],[ADS]               ,[Store+whMBQ])

    SELECT [Uniq], [StoreClass], [Store], [code], [ExtendedDescription], [ITEM_CLASS], [SUPPLIER], [Brands], [Min], [Max],[ADS]
           ,[Store+whMBQ]
    FROM dbo.MBQ_All_1 mbqN
    LEFT JOIN MBQ_All mbqO ON mbqN.Uniq =  mbqO.Uniq
    WHERE mbqO.Uniq IS NULL

1 Comment

It is still better than the query I wrote , thank you !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.