Data Lake and Data Warehouse 5 min read

Extracting Changes from Billion-Row Tables Without Slowing Production

A watermark query that runs in milliseconds on a test database can bring a production server to its knees when the table holds a billion rows. The pipeline scans the table, takes locks, fills tempdb, and customer-facing requests start timing out.

This is why many teams only run extraction overnight, and why their warehouse is always a day behind. It doesn’t have to be that way. Three design choices make extraction light enough to run hourly or more often: the right index, chunked reads, and the right isolation level.

This article builds on the gap-free watermark described in The Silent Data Loss Bug in Incremental ETL.

1. Index the rowversion column

SQL Server does not index a rowversion column automatically. Without an index, every WHERE RowVer > @LastProcessedVer query scans the entire table, even if only a hundred rows changed.

A non-clustered index turns that scan into a seek that touches only the changed rows:

CREATE NONCLUSTERED INDEX IX_LargeInventoryTable_RowVer
ON dbo.LargeInventoryTable (RowVer);

If the table’s primary key is also its clustered index, you don’t need to add it as an included column. SQL Server already stores the clustering key in every non-clustered index, so the index can find the matching rows without extra lookups for the chunking step.

Don’t make rowversion the clustered index

It can be tempting to cluster the table on rowversion, since that’s the column the pipeline reads by. Avoid this. The clustered index determines where each row physically lives, and rowversion changes on every update. Clustering on it would force SQL Server to move a row to a new location every time it changes, causing heavy fragmentation and extra write load.

Keep the clustered index on a stable key, such as an INT IDENTITY or a business key, and keep rowversion in its own non-clustered index.

2. Read in chunks, not one huge query

Even with an index, a single query that pulls every change since the last run can return millions of rows after a busy day or a bulk update. Large reads hold resources for a long time and can trigger lock escalation, where SQL Server swaps many row locks for a single table lock.

Reading in fixed-size chunks keeps each step short. The pattern below fixes the upper bound once at the start, then walks through the changes in order of rowversion, committing each chunk together with its watermark.

DECLARE @LastProcessedVer BINARY(8);
DECLARE @MaxSafeVer       BINARY(8);
DECLARE @ChunkMaxVer      BINARY(8);
DECLARE @ChunkSize        INT = 50000;  -- tune to row width; 10k–50k is typical

SELECT @LastProcessedVer = LastVersion
FROM dbo.EtlWatermark
WHERE TableName = 'LargeInventoryTable';

SET @LastProcessedVer = ISNULL(@LastProcessedVer, 0x0000000000000000);

-- Fixed, gap-free upper bound for this whole run
SET @MaxSafeVer = MIN_ACTIVE_ROWVERSION();

WHILE 1 = 1
BEGIN
    SET @ChunkMaxVer = NULL;

    -- Find the upper edge of the next chunk using the RowVer index
    SELECT @ChunkMaxVer = MAX(RowVer)
    FROM (
        SELECT TOP (@ChunkSize) RowVer
        FROM dbo.LargeInventoryTable
        WHERE RowVer > @LastProcessedVer
          AND RowVer < @MaxSafeVer
        ORDER BY RowVer
    ) AS chunk;

    IF @ChunkMaxVer IS NULL
        BREAK;  -- nothing left to extract

    BEGIN TRANSACTION;

        INSERT INTO staging.LargeInventoryTable (ItemID, ItemName, Quantity, RowVer)
        SELECT ItemID, ItemName, Quantity, RowVer
        FROM dbo.LargeInventoryTable
        WHERE RowVer >  @LastProcessedVer
          AND RowVer <= @ChunkMaxVer;

        -- Advance the watermark in the same transaction as the data
        UPDATE dbo.EtlWatermark
        SET LastVersion = @ChunkMaxVer
        WHERE TableName = 'LargeInventoryTable';

    COMMIT TRANSACTION;

    SET @LastProcessedVer = @ChunkMaxVer;
END

Because each chunk and its watermark commit together, a failure part-way through loses at most one chunk of work. The next run restarts from the last committed chunk instead of from the beginning.

When the destination is a file in object storage rather than a staging table, the same idea applies: write and confirm the file, then advance the watermark. Python Extracts, dbt Transforms shows that version in Python.

3. Choose an isolation level that doesn’t block anyone

Extraction reads have to coexist with live writes. There are two common approaches, and only one of them is safe.

WITH (NOLOCK) is a false economy. It avoids blocking, but it reads data without any consistency guarantees. During a scan it can skip rows or read the same row twice when pages split under it. For a pipeline whose whole purpose is accuracy, that is the wrong trade.

Read committed snapshot isolation (RCSI) is the better choice. With RCSI enabled, readers see the last committed version of each row from SQL Server’s version store instead of waiting for locks. Readers don’t block writers, writers don’t block readers, and the results are consistent.

-- Requires a brief moment with no other active connections to the database
ALTER DATABASE YourDatabase SET READ_COMMITTED_SNAPSHOT ON;

RCSI adds some tempdb load because of the version store, so test it under a realistic workload first. Many OLTP systems already benefit from it for their own reasons. If changing the database default isn’t possible, snapshot isolation can be enabled and used only by the extraction session.

Summary checklist for large-scale extraction

AreaActionEffect
WatermarkUpper bound from MIN_ACTIVE_ROWVERSION()No rows lost to slow transactions
IndexingNon-clustered index on rowversionSeeks only the changed rows instead of scanning
ClusteringKeep the clustered index on a stable keyAvoids rows moving on every update
Chunking10k–50k rows per batch, committed with the watermarkShort transactions, small memory use, cheap restarts
IsolationRCSI or snapshot isolation, not NOLOCKConsistent reads without blocking production

Next steps

If extraction is the reason your warehouse is refreshed nightly rather than hourly, these changes are usually the quickest win available. YenDigital can profile your largest source tables and recommend an extraction design that fits your workload.

Talk to us about your pipeline

Share this article

Our Partners

Built with leading
AI technologies

We build and integrate AI solutions using technologies from OpenAI, Google Gemini, Anthropic, DeepSeek and Perplexity, selected to suit each client’s needs.

Enterprise-Ready AI Solutions Built for Scale & Security

Secure, scalable AI solutions designed to integrate with your enterprise systems and accelerate business outcomes.

Get a Quote