I am working in SQL Server 2012. I have the following DML:
BEGIN TRAN
UPDATE A
SET
A.colx = B.coly
FROM DB1.dbo.Table1 A
INNER JOIN DB2.dbo.Table2 B
ON B.col1 = A.col1
AND B.col2 = A.col2
WHERE
A.colx <> B.coly
AND B.col3 IN ('a', 'b', 'c', 'd', 'e', 'f')
;
It takes an unacceptably long time to run. After an hour, I had to cancel the query. So, I need to tune it.
I tried to inspect the query plan, but I get a SHOWPLAN permission denied message on DB2. So, I ran the following query:
SELECT
COUNT(*)
FROM DB1.dbo.Table1 A
INNER JOIN DB2.dbo.Table2 B
ON B.col1 = A.col1
AND B.col2 = A.col2
WHERE
A.colx <> B.coly
AND B.col3 IN ('a', 'b', 'c', 'd', 'e', 'f')
This query returns in about 1 [min], and COUNT(*) = 60M. So, I don't think it's the read operation that is causing the first query to run so slow. It's the write operation that is the bottleneck.
Table A is an 800M-row, 75-[GB] table. I have been told that I don't have the disk space to use a SELECT INTO technique. It has a non-clustered index on its primary key. col1 and col2 are part of this primary key. It has other non-clustered indexes and a clustered index. Normally, because of the clustered index, I would follow the "non-simple" technique given here: Fastest way to update 120 Million records. However, I don't have other schemas available to me in DB1. So, I can't use the "non-simple" technique. Table B is a 10M-row table. It has no indexes and no keys.
What is the best way to tune this UPDATE statement? Perhaps batches?