How can i reduce the size of a table with filestream

92 views Asked by At

I am working on a database function which activate filestream for a couple of tables.

It seems to work just fine. Filestream on SQL Server is enabled, the database has it enabled too.

I can now either create a new table with filestream columns, or I can add a column with filestream.

Both options seem to work.

Files are created in the FS directory.

The problem for me now is the table size.

  • When I create a new table and insert/copy data into it, the size of the table is (for example) under 1 MB

  • When I add a new column to the existing table and copy the data over, the table size is over 10 MB

Here is what I used to enable FS on the table and copy the data:


EXEC sys.sp_rename
    @objname = N'dbo.tImage.bImage', 
    @newname = bImage_OLD, 
    @objtype = 'COLUMN' 

ALTER TABLE dbo.tImage ADD bImage VARBINARY(MAX) FILESTREAM NULL

UPDATE dbo.tImage
SET bImage = bImage_OLD

ALTER TABLE dbo.tImage DROP COLUMN Image_OLD

How can I reduce the size of the table after I added the new column, or is this even possible without creating a new table?

I already tried to shrink the database file and the transaction log. I also performed a DBCC CHECKDB REPAIR_REBUILD.

DBCC SHRINKFILE(2)
DBCC SHRINKFILE(2, EMPTYFILE) 
DBCC CLEANTABLE(myDB, 'dbo.tImage')
DBCC DROPCLEANBUFFERS

The most documentation I found mentioned a new database and inserting new rows.

I also found a blog post that describes my preferred way to copy the data here.

sqlshack.com

UPDATE:

When I add a new picture to my changed table, the Size of the table does not increase. In this case it is a semi success for me, but I want the initial table size to be small as possible, and I know there is potential.

Using the following Code seem to fix my problem. I tried it before already, but with using the Schema and get some other problems. I have to investigate it more.

DBCC CLEANTABLE(database, 'tImage')
0

There are 0 answers