4

I have been trying to update a column in a table and I am getting the below error:

The transaction log for database 'STAGING' is full due to 'ACTIVE_TRANSACTION'.

I am trying to run the below statement :

UPDATE [STAGING].[dbo].[Stg_Encounter_Alias]
    SET
        [valid_flag]            = 1

    FROM  [Stg_Encounter_Alias] Stg_ea
    where [ACTIVE_IND] = 1
        and [END_EFFECTIVE_DT_TM] > convert(date,GETDATE())

My table has approx 18 million rows. And the above update will modify all the rows. The table size is 2.5 GB. Also the DB is in simple recovery mode

This is something that I'll be doing very frequently on different tables. How can I manage this?

My Database size is as per below

enter image description here

Below are the database properties!!! I have tried changing the logsize to unlimited but it goes back to default.

enter image description here

Can any one tell me an efficient way to handle this scenario?

If I run in batches :

begin
DECLARE @COUNT INT
SET @COUNT = 0

SET NOCOUNT ON;      
DECLARE @Rows INT,
    @BatchSize INT; -- keep below 5000 to be safe

SET @BatchSize = 2000;

SET @Rows = @BatchSize; -- initialize just to enter the loop


WHILE (@Rows = @BatchSize)
BEGIN
  UPDATE TOP (@BatchSize) [STAGING].[dbo].[Stg_Encounter_Alias]
    SET
        [valid_flag]            = 1

    FROM  [Stg_Encounter_Alias] Stg_ea
    where [ACTIVE_IND] = 1
        and [END_EFFECTIVE_DT_TM] > convert(date,GETDATE())
  SET @Rows = @@ROWCOUNT;
END;
end
DhruvJoshi
  • 17,041
  • 6
  • 41
  • 60
Doodle
  • 481
  • 2
  • 7
  • 20
  • Thanks Mitch!! Can you suggest any memory changes as well which I can do to let it handle more updates/inserts – Doodle Nov 05 '18 at 04:15
  • No there is no index on END_EFFECTIVE_DT_TM. I'll try to run it in batches. Actually I had been doing batch truncates on the same table and they were running perfectly for the same number of rows. But somehow updates are failing!! And I did the same update on another DB with same configuration and it worked. – Doodle Nov 05 '18 at 04:20

1 Answers1

6

You are performing your update in a single transaction, and this causes the transaction log to grow very large.

Instead, perform your updates in batches, say 50K - 100K at a time.

Do you have an index on END_EFFECTIVE_DT_TM that includes ACTIVE_IND and valid_flag? That would help performance.

CREATE INDEX NC_Stg_Encounter_Alias_END_EFFECTIVE_DT_TM_I_ 
ON [dbo].[Stg_Encounter_Alias](END_EFFECTIVE_DT_TM) 
INCLUDE (valid_flag) 
WHERE ([ACTIVE_IND] = 1);

Another thing that can help performance drastically if you are running Enterprise Edition OR SQL Server 2016 SP1 or later (any edition), is turning on data_compression = page for the table and it's indexes.

Mitch Wheat
  • 295,962
  • 43
  • 465
  • 541
  • I have updated the code in the question above and it is running damn slow if I run it in batches!!! It was having a great speed in another DB when I had been running the same query in a different database!!! – Doodle Nov 05 '18 at 05:26
  • your batch size is 2K, much lower than I suggested. Running in batches should only be slightly slower....Did you create the index? Another DB might be on a server with a different hardware spec?? you've changed the query by adding the TOP... do you have a primary key? – Mitch Wheat Nov 05 '18 at 05:30
  • This was actually a staging database which had no indexes. I added an index on end_effective_dt_tm and it didn't help with the speed !!! The other DB has exactly the same configuration as this one. Not sure what's happening!! I have tried with 100k as well and the query had been running for 15 minutes now. with no signs of getting finished any sooner!!! – Doodle Nov 05 '18 at 05:38
  • Have you pre-sized your TLog ?? Is TLog file on fast storage? Does table have a primary key? Is it a heap or does it have a clustered index? – Mitch Wheat Nov 05 '18 at 06:00
  • I have the index you suggested and the batch size I used was 100000. The query ran for 15 hours and I manually stopped it. I have been using all default setting. I am not sure how to check the fast storage or pre size the Tlog. – Doodle Nov 05 '18 at 21:11
  • only thing I can suggest is post the estimated query plane for no batch and batch query. https://www.brentozar.com/pastetheplan/ – Mitch Wheat Nov 05 '18 at 22:43