简体   繁体   中英

Modify XML Data in SQL Server table

I need to modify XML data stored in a SQL Server table containing approx 62m rows.

I am using the following query to do this and here is the query plan for it.

UPDATE c
SET     XMLContent.modify('delete //account/correspondence/contact[1]')
FROM   [Customer].[CorrespondenceLog] c

It has been running for over 24 hours and doesn't appear to be showing any progress. Is there a better method to modify XML in SQL Server (apart from obviously not storing XML in SQL Server! :))

Could an XML index help here?

If there's about 62 million rows, then updating all of them will obviously take some time.

But you could make the UPDATE query repeatable by adding a WHERE clause that checks if the tag exists in the XML field.
Then it won't update the XML if there's nothing to change in that XML.

And you don't actually need to use that FROM, although that probably won't make a difference performance wise.

UPDATE [Customer].[CorrespondenceLog]
SET 
 XMLContent.modify('delete //account/correspondence/contact[1]')
WHERE    XMLContent.exist('//account/correspondence/contact[1]') = 1

And index on another non-XML field could be usefull if you would do this in batches.
For example if there's an index (or partioning?) on some date field, and you update for ranges of dates.

Adding an XML index could help to some extend to find XML's that contain the tag.
But not sure it's worth it just to remove a tag.

CREATE PRIMARY XML INDEX PIdx_CorrespondenceLog_XMLContent 
ON [Customer].[CorrespondenceLog]([XMLContent]);

CREATE XML INDEX PIdx_CorrespondenceLog_XMLContent_PATH 
ON [Customer].[CorrespondenceLog]([XMLContent])
USING  XML INDEX PIdx_CorrespondenceLog_XMLContent FOR PATH; 

db<>fiddle here

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