2

I googled and read a lot but couldn't find a definitive answer to the simple question: Does adding an empty column to a table increase the transaction log size in SQL Server?

I'm asking because we want to add a new int/bigint column to an existing table. The database runs on servers provided by our customers so we want to be prepared. Some customers will have billions of records in their tables.

DDL statement:

ALTER TABLE example
ADD id_new int;

Bonus question: Is it possible to calculate the required transaction log size knowing the data type and the number of rows in advance?

Thanks in advance!

2 Answers 2

7

Does adding an empty column to a table increase the transaction log size in SQL Server?

Sometimes.

If you are adding a NOT NULL column to a table with a default constraint, it might be metadata-only and not use significant log size. If you are using SQL Server 2012+, and Enterprise Edition, then adding the column may be an online metadata-only operation.

Similarly, adding a NULL column without a default constraint may be an online meta-data-only operation.

Sometimes not.

Prior to SQL Server 2012 when you add a new NOT NULL column with a default constraint to an existing table, every row in the table is updated to add the default value of the new column--resulting in a fully logged size-of-data operation for the table. This is also true for 2012+ if not using Enterprise or Developer editions when adding a NOT NULL column.

If the table contains any SPARSE columns, that will prevent the add column from being online in any circumstance (even if normally allowed in a scenario described above). Certain other deprecated features/data types may also prevent adding the column as metadata-only--the nature of deprecated features means they aren't necessarily considered when adding new features, so they may or may not be compatible.

Similarly, if you are using Replication or Change Tracking, the configuration of those "keep track of all my changes" technologies may prevent the change from being online. If you're using any of them, you'll want to check & test.

Is it possible to calculate the required transaction log size knowing the data type and the number of rows in advance?

All schema changes will use some transaction log.

If adding the column qualifies for a metadata-only operation, then the amount of transaction log use would be minimal.

If adding the column does not qualify for a metadata-only operation, then the transaction will be a size-of-data operation--meaning SQL Server will essentially re-write the table, fully logged, in a single transaction, and require the transaction log space to do that.

Additionally, because the size-of-data operation may be long-running, SQL Server will not be able to free for reuse any portion of the transaction log while that transaction is running. If adding the column takes 2 hours, your transaction log will need to be large enough to hold both the data related to that schema change, and also every other transaction that runs concurrently with your schema change, following the normal log reuse rules. Note that even in Simple Recovery, SQL Server will only clear & reuse the transaction log prior to the start of the oldest open transaction--the DDL and all concurrent activity will be retained in the log until the DDL statement finishes.

Therefore, the amount of transaction log needed is the sum of both your size-of-data schema change AND everything else running at the same time. This means that testing in a non-production environment can cause you to under-estimate transaction log usage, because the lack of concurrent workload can affect both the amount of logged work, as well as the total duration.

If you are only going to be performing the schema change on a database with zero concurrency, you will find testing to provide a decent estimate--but even then an exact calculation is difficult due to the number of factors that can vary for logically identical data (size on disk, fragmentation, low page fill/page density, etc).

4
  • Thank you very much for your comprehensive answer. I added a concrete DDL statement to my question. SQL Server may be any version and edition starting from 2012 (but not Express). Let's assume the Recovery Model SIMPLE. There are no SPARSE or other special columns. Under these circumstances can we change your may be and might to probably? :-)
    – Manuel K
    Commented Dec 19, 2022 at 19:55
  • @ManuelK Since you are targeting both Enterprise & Standard Editions, the "sometimes" section would only apply to your Enterprise Edition servers, and the rest of the answer would apply to the Standard Edition Servers. Recovery Model does not affect my answer at all.
    – AMtwo
    Commented Dec 20, 2022 at 15:21
  • Thanks. But if we leave out your "Sometimes" section, the only thing remaining is "not"...so you're saying in SQL Server standard edition adding the column to a large table will always result in a huge transaction log?
    – Manuel K
    Commented Dec 20, 2022 at 20:44
  • 1
    @ManuelK Looking at your last edit, you're adding a NULLable column, without a default constraint. In most cases this is metadata-only, but you'll still need to pay attention to the caveats I mention in the "sometimes not" section. Sparse columns, deprecated features, and data replication/change tracking features should all be identified & tested to confirm. Just make sure you're testing on Standard or Express Editions--Developer Edition behaves like Enterprise Edition.
    – AMtwo
    Commented Dec 21, 2022 at 18:58
3

Does adding an empty column to a table increase the transaction log size in SQL Server?

It depends on how you do it and what version you are running. You should test.

Is it possible to calculate the required transaction log size knowing the data type and the number of rows in advance?

Yes. By testing.

5
  • Thanks for your answer. Can you please clarify what you mean by "how you do it"? I'm running a DDL statement in a transaction using Liquibase. "what version you are running" Good point. Our customers may be running any version since SQL Server 2012, so it's hard for us to test. You are giving the easy answer "try it yourself". Are there no clear rules or docs on what ends up in the transaction log and under what conditions?
    – Manuel K
    Commented Dec 19, 2022 at 6:32
  • @ManuelK "I'm running a DDL statement" - Yes, and what is that statement? Depending on what that statement does, will result in a different answer to your question. Depending on the version of SQL Server may also result in a different answer. For example, if you're adding the column as nullable, that will have much less impact on the Transaction Log than adding the column with a default value. Whether the Transaction Log will grow in either case actually depends a lot more on the current fullness and size of the Log, the Recovery Model of the DB, and when your Transaction Log backups run.
    – J.D.
    Commented Dec 19, 2022 at 13:08
  • @ManuelK, in addition to version, Edition also matters. DDL in Enterprise Edition may be a meta-data only change but not in Standard or lessor (e.g. adding the bigint NOT NULL column with a default value). Add the actual script to your question for a more complete answer.
    – Dan Guzman
    Commented Dec 19, 2022 at 14:47
  • @J.D. I added the DDL statement. It's very simple. SQL Server may be any version and edition starting from 2012 (but not Express). Let's assume the Recovery Model SIMPLE.
    – Manuel K
    Commented Dec 19, 2022 at 19:52
  • @DanGuzman I added the DDL statement. It's very simple. SQL Server may be any version and edition starting from 2012 (but not Express). Let's assume the Recovery Model SIMPLE.
    – Manuel K
    Commented Dec 19, 2022 at 19:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.