Sql-server – Removing physical fragmentation

fragmentationsql serverstorage

If I have inherited db that has grown to 400GB 1 MB chunk at at time. Is an offline defrag the only way to remove all the physical fragmentation. Another issue I have is that the data and log files are mixed and on the wrong RAID drives.

Can I kill 2 birds with 1 stone by backing up the db, delete it and restore it using WITH MOVE to put the files on the correct RAID drives. Will this eliminate the physical fragmentation?

Best Answer

No, backup / restore will keep all the fragmentation. Probably better to add a filegroup with files in the new location, and recreate all of your user tables on the new filegroup (by recreating the indexes with DROP_EXISTING, and as an online operation if possible). You won't be able to eliminate the original files entirely, but if you've moved all the user objects, you should be able to shrink the primary data file down to system objects only.

If you then want to move the primary MDF file and log file to the new RAID drives, you can do this with ALTER DATABASE - take the database offline, alter the individual file locations, physically move the files, then bring the database back online again.