Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'm going to guess that you are closing in on the 2.1billion limit of an INT datatype on an artificial key for a column. Yes, that's a pain. Much easier to fix before the fact than after you've actually hit that limit and production is shut down while you are trying to fix it :)</p> <p>Anyway, several of the ideas here will work. Let's talk about speed, efficiency, indexes, and log size, though.</p> <h2>Log Growth</h2> <p>The log blew up originally because it was trying to commit all 2b rows at once. The suggestions in other posts for "chunking it up" will work, <em>but</em> that may not totally resolve the log issue.</p> <p>If the database is in SIMPLE mode, you'll be fine (the log will re-use itself after each batch). If the database is in FULL or BULK_LOGGED recovery mode, you'll have to run log backups frequently during the running of your operation so that SQL can re-use the log space. This might mean increasing the frequency of the backups during this time, or just monitoring the log usage while running.</p> <h2>Indexes and Speed</h2> <p>ALL of the <code>where bigid is null</code> answers will slow down as the table is populated, because there is (presumably) no index on the new BIGID field. You could, (of course) just add an index on BIGID, but I'm not convinced that is the right answer.</p> <p>The key (pun intended) is my assumption that the original ID field is probably the primary key, or the clustered index, or both. In that case, lets take advantage of that fact, and do a variation of Jess' idea:</p> <pre><code>set @counter = 1 while @counter &lt; 2000000000 --or whatever begin update test_table set bigid = id where id between @counter and (@counter + 499999) --BETWEEN is inclusive set @counter = @counter + 500000 end </code></pre> <p>This should be extremely fast, because of the existing indexes on ID. </p> <p>The ISNULL check really wasn't necessary anyway, neither is my (-1) on the interval. If we duplicate some rows between calls, that's not a big deal.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload