I need to write a store procedure to update link reference in my DB. Link can be contained in few nvarchar field that contains JSONs (in which may be there arethat could contains some urls).
To do so i update the tables in batches of 8129 items per iteration, so that the machine will not hang (in theory).
Here
But now the code seems to hang anyway, it will not print any message and the procedure keep running (without affecting any data) for many minutes, untill i have writtento kill the procedure (that in meanwhile seems to haven't affect any data).
If i try to use the same logic on a toy example, i get no issue, so i think my problem is due the fact the table is big (few hundred thousand of rows).
Here the minimal example that is working, the exact same code on bigger table hang apparently doing nothing (tested with SQL Server 2019).
Example :
ALTERCREATE PROCEDURETABLE [dbo].[SiteUrlChangeURL][tbl_ana_Articles](
@FullOldUrl[ID] nvarchar[int] IDENTITY(5001,1) NOT NULL,
@FullNewUrl[ID_BRAND] nvarchar[int] NOT NULL,
[CODE] [nvarchar](50040)
AS NOT NULL,
BEGIN [CODFOR] [nvarchar](40) NOT NULL,
--[COD_ALT01] SET[nvarchar](50) NOCOUNTNOT ONNULL,
added to prevent extra[COD_ALT02] result[nvarchar](50) setsNOT fromNULL,
--[COD_ALT03] interfering[nvarchar](50) withNOT SELECTNULL,
statements. [ID_UOM] [int] NOT NULL,
SET[IS_ACTIVE] NOCOUNT[bit] ON;NOT NULL,
[_ATTRIBUTES] [nvarchar](max) NOT NULL,
SET[_DOCUMENTS] @FullOldUrl[nvarchar](max) =NOT ISNULLNULL,
[_SEO] [nvarchar](@FullOldUrlmax) NOT NULL,''
[_TRANSLATIONS] [nvarchar](max); NOT NULL,
SET[_TAGS] @FullNewUrl[nvarchar](max) =NOT ISNULLNULL,
[_NOTES] [nvarchar](@FullNewUrlmax) NOT NULL,''
[_METADATA] [nvarchar](max); NOT NULL,
[IS_B2B] [bit] NOT NULL,
[IS_B2C] [bit] NOT NULL,
IF[IS_PROMO] [bit] NOT NULL,
[IS_NEWS] [bit] NOT NULL,
[CAN_BE_RETURNED] [bit] NOT NULL,
[IS_SHIPPABLE] [bit] NOT NULL,
[HAS_SHIPPING_COSTS] [bit] NOT NULL,
[IS_PURCHEASABLE] [bit] NOT NULL,
CONSTRAINT [PK_tbl_ana_articles] PRIMARY KEY CLUSTERED
(
LEN [ID] ASC
)WITH (@FullOldUrlPAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) <=ON 0[PRIMARY]
) ORON LEN[PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
INSERT INTO [dbo].[tbl_ana_Articles]
(@FullNewUrl)[ID_BRAND]
<= 0 ,[CODE]
,[CODFOR]
,[COD_ALT01]
,[COD_ALT02]
,[COD_ALT03]
,[ID_UOM]
,[IS_ACTIVE]
,[_ATTRIBUTES]
,[_DOCUMENTS]
,[_SEO]
,[_TRANSLATIONS]
,[_TAGS]
,[_NOTES]
,[_METADATA]
,[IS_B2B]
,[IS_B2C]
,[IS_PROMO]
,[IS_NEWS]
,[CAN_BE_RETURNED]
,[IS_SHIPPABLE]
,[HAS_SHIPPING_COSTS]
,[IS_PURCHEASABLE])
BEGIN VALUES
PRINT ('Invalid1
parameters' ,'COD1'
,'SUPPLIER1'
,'CATEGORY1'
,'CATEGORY1-BIS'
,'CATEGORY2'
,1
,1
,'{ "url" : "https://old.com" }'
,''
,''
,''
,''
,''
,''
,1
,0
,0
,0
,1
,1
,0
,1);
RETURN 1;
ENDDECLARE @FullOldUrl AS NVARCHAR(50) = 'https://old.com';
DECLARE @FullNewUrl AS NVARCHAR(50) = 'https://new.com';
--ARTICLE
PRINT('updating articles');
WHILE 1=1
BEGIN
UPDATE TOP (8196) [dbo].[tbl_ana_Articles]
SET [_ATTRIBUTES] = REPLACE([_ATTRIBUTES] , @FullOldUrl, @FullNewUrl)
,[_DOCUMENTS] = REPLACE([_DOCUMENTS] , @FullOldUrl, @FullNewUrl)
,[_SEO] = REPLACE([_SEO] , @FullOldUrl, @FullNewUrl)
,[_TRANSLATIONS] = REPLACE([_TRANSLATIONS] , @FullOldUrl, @FullNewUrl)
,[_TAGS] = REPLACE([_TAGS] , @FullOldUrl, @FullNewUrl)
,[_NOTES] = REPLACE([_NOTES] , @FullOldUrl, @FullNewUrl)
WHERE
[_ATTRIBUTES] like '%' + @FullOldUrl + '%' OR
[_DOCUMENTS] like '%' + @FullOldUrl + '%' OR
[_SEO] like '%' + @FullOldUrl + '%' OR
[_TRANSLATIONS] like '%' + @FullOldUrl + '%' OR
[_TAGS] like '%' + @FullOldUrl + '%' OR
[_NOTES] like '%' + @FullOldUrl + '%'
IF (@@ROWCOUNT <= 0)
BEGIN
BREAK;
END
END
RETURN 0
SELECT * FROM [dbo].[tbl_ana_Articles]
PRINT('Finished');
I used a very similar code previously (to update path) without any issues (to be fair the tables previously involved was very very small). But nowHere the code seems to hang, it will not print any message andexecution plan produced by the procedure keep runningtoy example (without affecting any datai can't get the execution plan of real scenario).
https://www.brentozar.com/pastetheplan/?id=SJhVTcMTo
I am trying to udpate to very different urlreally puzzled about what caused that issue
--EDIT (so one is not a substring of:
i have run the other)procedure again, and discovered that if i let run long enough (~30 min) i get the above storedright behaviour. So apparently i have a performance issue here. I am not really intereseted in performance here, because the procedure seems to hang doing nothing and can't understand whyis used only rarely (manually) during mantainance (only if the site change domain).
Eg : FullOldUrl = 'https://old.com'
FullNewUrl = 'https://new.com'But i am curious if I made some blunder here, to get such low performance