Efficient way to alter 100GB table

We have a number of databases which store 10s to 100s of gigabytes of data in one of the tables. It contains image data. The problem is that a lot of these databases were created improperly. Basically the primary key isn't actually a primary key. The were created with a unique index on a nullable column. And some of them have an int as a primary key instead of a bigint.

So we've been slowly going through and fixing these databases. They run on SQL Server 2000 through SQL Server 2008, although most of the ones with primary key issues are on SQL Server 2000. The problem is, we don't want to lock down the database for a whole day while it converts the table. We've gone through several strategies:

  1. Tell SQL Server to directly change the column type. This locks the table until it's complete, and after leaving it overnight in many cases, it still wasn't finished.

  2. Insert all the images into a new table in one go. This was more easily interrupted, but the whole table basically gets written to the log file in the process.

  3. Insert 100 rows at a time where the rows don't exist in the target table. The upside is that they can continue using the database while this is going on (with a big performance hit) and that it can be stopped and restarted arbitrarily at any point, and it prevents the 100GB+ log files. This is what we're currently doing, but finding the top 100 rows that don't exist gets really really slow as the target table gets bigger and bigger. UPDATE STATISTICS and DBCC INDEXDEFRAG help considerably, but in the most recent attempt, we got to the point even 100 images at a time was sitting there not responding.

    INSERT INTO %s 
     ВЫБЕРИТЕ ТОП 100 источников. * 
     ИЗ% s КАК Источник С (НЕТ) 
     LEFT OUTER JOIN% s AS Target WITH (NOLOCK) ON Source.DocumentID = Target.DocumentID 
     ГДЕ Target.DocumentID ЕСТЬ NULL 
     ЗАКАЗАТЬ ПО Source.DocumentID 
    

Итак, вопрос в том, есть ли опция, которая может эффективно копировать массивы данных и возобновлять их действие? Он не обязательно должен быть на 100% точным, мы всегда можем вернуться и исправить любые неточности в конце, если он выполняет 99% всей работы.

7
задан Joey 13 September 2010 в 17:11
поделиться