简体   繁体   中英

SQL - Update query performance for large table (900 million) records

I have a database table that has 900 million records. I am in a situation where I need to update 4 different keys in that table, by joining them to a dimension and setting the key of the fact table to the key of the dimension. I have written 4 different SQL scripts (see example below) to perform the update, however problem is it is taking way too long to execute. The query has been running for more than 20 hours and I am not even sure how far it go and how long this will take. Is there any way I can do to improve this so it only takes few hours to complete. Would adding Indexes improve this?

UPDATE f
SET f.ClientKey = c.ClientKey
FROM dbo.FactSales f
JOIN dbo.DimClient c
ON f.ClientId = c.ClientId
  1. Script foreign keys. Drop them.
  2. Script indexes on updated columns (which are not part of condition). Drop them.
  3. Disable triggers if exist.
  4. Disable all processes which can make locks (=all, include selects).
  5. Update your keys.
  6. Recreate your foreign keys, indexes, enable triggers.
  7. Be happy.

And comment for 5 - prepare your primary key from destination table with all new source code only and do one statement. It means lesser cost on joins and it will be only one join.

Can use this to not fill up the transaction log

select 1 
while(@@rowcount > 0)
begin 
    UPDATE f
    SET top (100000) f.ClientKey = c.ClientKey
    FROM dbo.FactSales f
    JOIN dbo.DimClient c
    ON  f.ClientId   = c.ClientId 
    AND f.ClientKey != c.ClientKey
end

If you need to update 4 different keys then do them all at once
Most of the cost is acquiring lock

Disable f.ClientKey, run the update, and then rebuild it

If you are sure DimClient is not going to change with (nolock) but need to be sure

If you are the only process that need to update FactSales take a tablock holdlock

Create a new table with the correct values. Add indexes, constraints afterwards. Drop the existing table and rename the new one to existing one in one transaction if that is possible .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM