Here is my update statement:
Update childtbl C
set C.chldFld=Select P.ParentFld
from ParentTbl P
where P.ID=C.ID
It is taking hours and hours to update.
There are indexes for both the parent and child tables.
1) Check if you have an uncommited transaction.
2) How many records in childtbl ? How many in ParentTbl? Is there an index on ParentTbl.ID?
An index on ParentTbl.ID will help you find the right record accelerating the update.
3) Is there an index on childtbl.chldFld? How many indexes use chldFld?
Having indexes on cldFld will slow the update.
Too many (million) record in childtable will cause the update to take long time.
If you have a foreign key relationship between the tables you can update on a key-preserved view of the two tables joined.
The general pattern would be:
Update (
select parent_table.key parent_key,
child_table.key child_key,
parent_table.value parent_value,
child_table.value child_value
from parent_table
join child_table on child_table.key = parent_table.key)
set
child_value = parent_value;
This gives the optimiser more choices in joining the two tables efficiently (eg a hash join).
First check for any locking and blocking on the tables. Check the indexes used and if they are poorly written. Hope you can post the results so that we can have a better take on it.
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.