Skip to main content
added 1 character in body
Source Link
paparazzo
  • 5.1k
  • 1
  • 19
  • 32

You are totally trashing the index on NewTable.C2
Just drop / disable the index and thethen build / rebuild it after the insert is complete

And you need to break this up as you are putting a massive load on the transaction log

If you want to optimize the select then just run the select alone

INSERT INTO NewTable (C1, C2, C3, C4, C5, C6, C7) 
SELECT OldTable1.C1, OldTable1.C2, OldTable1.C3, OldTable1.C4
     , OldTable1.C5, OldTable1.C6, OldTable1.C7 
FROM OldTable1 
JOIN OldTable2.C5 
  ON OldTable2.C5 = OldTable1.C5 
 AND OldTable2.C8 >= 70 
 AND OldTable2.C8 <= 89;

You are totally trashing the index on NewTable.C2
Just drop / disable the index and the build / rebuild it after the insert is complete

And you need to break this up as you are putting a massive load on the transaction log

If you want to optimize the select then just run the select alone

INSERT INTO NewTable (C1, C2, C3, C4, C5, C6, C7) 
SELECT OldTable1.C1, OldTable1.C2, OldTable1.C3, OldTable1.C4
     , OldTable1.C5, OldTable1.C6, OldTable1.C7 
FROM OldTable1 
JOIN OldTable2.C5 
  ON OldTable2.C5 = OldTable1.C5 
 AND OldTable2.C8 >= 70 
 AND OldTable2.C8 <= 89;

You are totally trashing the index on NewTable.C2
Just drop / disable the index and then build / rebuild it after the insert is complete

And you need to break this up as you are putting a massive load on the transaction log

If you want to optimize the select then just run the select alone

INSERT INTO NewTable (C1, C2, C3, C4, C5, C6, C7) 
SELECT OldTable1.C1, OldTable1.C2, OldTable1.C3, OldTable1.C4
     , OldTable1.C5, OldTable1.C6, OldTable1.C7 
FROM OldTable1 
JOIN OldTable2.C5 
  ON OldTable2.C5 = OldTable1.C5 
 AND OldTable2.C8 >= 70 
 AND OldTable2.C8 <= 89;
Source Link
paparazzo
  • 5.1k
  • 1
  • 19
  • 32

You are totally trashing the index on NewTable.C2
Just drop / disable the index and the build / rebuild it after the insert is complete

And you need to break this up as you are putting a massive load on the transaction log

If you want to optimize the select then just run the select alone

INSERT INTO NewTable (C1, C2, C3, C4, C5, C6, C7) 
SELECT OldTable1.C1, OldTable1.C2, OldTable1.C3, OldTable1.C4
     , OldTable1.C5, OldTable1.C6, OldTable1.C7 
FROM OldTable1 
JOIN OldTable2.C5 
  ON OldTable2.C5 = OldTable1.C5 
 AND OldTable2.C8 >= 70 
 AND OldTable2.C8 <= 89;