Print message comes out in batches
-
02-11-2019 - |
Question
I have simple query
WHILE ( @counter < 50 )
BEGIN
BEGIN TRANSACTION
PRINT 'Counter = ' + CONVERT(VARCHAR(4), @counter);
WITH CTE
AS (
SELECT TOP 50000 * FROM MyTable
)
DELETE FROM CTE OPTION ( MAXDOP 1 )
SET @counter += 1
COMMIT TRAN
END
When I look at messages, the PRINT
command does not return row every loop but shows up as multiple rows at ones. I have added one line and now it shows up 1 line at the time and the the whole process is much faster.
WHILE ( @counter < 50 )
BEGIN
WAITFOR DELAY '00:00:00.5' --< This line
BEGIN TRANSACTION
PRINT 'Counter = ' + CONVERT(VARCHAR(4), @counter);
WITH CTE
AS (
SELECT TOP 50000 * FROM MyTable
)
DELETE FROM CTE OPTION ( MAXDOP 1 )
SET @counter += 1
COMMIT TRAN
END
so why is it when I have added .5 second wait time to each loop iteration it actually runs faster and allows the print message to come out one at the time?
No correct solution
Licensed under: CC-BY-SA with attribution
Not affiliated with dba.stackexchange