views:

390

answers:

1

Hi all,

I have some databases that have four files each; one for PRIMARY, IDX, IMAGE, LOG their initial sizes were set to 200MB each but they are consuming far less space, about 100MB total.

1) is it possible to resize the database files while they are in use down to more reasonable sizes, say "current consumption" + 10MB?

2) is it possible to merge these files into just a single .mdf and .ldf?

My preferred answer to this would be some T-SQL, but I will accept links to MSDN (or other sites) with a reference for what T-SQL to look for.

+3  A: 

1) DBCC SHRINKFILE or SHRINKDATABASE. There is also a context click in the SQL Management Studio for all tasks.

If they are set to auto grow, then they will do just that, and undo your shrinking. Shrink ops are best for when there is a large data removal that has taken place in the db, or when the server is going to crash from low disk space.

2) Yes, but its ridiculously difficult. You will have drop all tables and create them on the target filegroup and then repopulate them. There may be a way to do this using the RESTORE options, but IMHO you are better off leaving them if there is less than 10 files.

StingyJack
Regarding #2, it's easy to move indexes using CREATE INDEX .. ON filegroup WITH DROP_EXISTING. You can easily write a query to script this. Tables with no clustered indexes (heaps) should have an clustered index added, then moved, then remove the clustered index.
Jim McLeod
in a reasonable database with 300 tables spread across 10 files in 5 filegroups, how long would it take you to do A) make the script and B) how much downtime is involved while this is happening?
StingyJack