Removing Filestream from your Maximizer Database
Filestream removal for Maximizer databases
Filestream is a storage method for binary objects in MS SQL databases. Rather than storing the binary object directly in the data file of the database, Filestream stores the file in the file structure of the server itself. There are pros and cons to the use of Filestream but with the withdrawal of support for SQL Express edition in Maximizer some versions ago, it is time to retire the use of Filestream.
Time to Completion Dependency
An important reminder is that removing FILESTREAM within the database is dependent upon:
- Total Size of your FILESTREAM data
- Total number of documents in your database
- Capabilities of your SQL Server (ie. processing power, memory, disk space)
- If you are removing FILESTREAM on a relatively new SQL Server, removing FILESTREAM will be relatively faster
- If your SQL Server is on a virtual machine, you could temporarily allocate more resources to it to accommodate the FILESTREAM removal process.
- The recommended available disk space is at least twice the size of your FILESTREAM.
Checking whether Filestream is in use.
- Open the SQL Management Studio and connect to the SQL server instance that is in use.
- Use the navigation to find the database that you are going to upgrade
- Right-click on the database and check the properties.
- From the properties dialog, choose the Filegroups option and look at the FILESTREAM section
- If there are files listed in the Filestream section, then you will need to remove Filestream from the database.
The process for each database is:
- Switch the database to Simple recovery mode so the log does not grow excessively
- Remove all full-text indexes.
- Turn off Filestream on
- Remove the Filestream file
- Remove the Filestream Filegroup
The process outline for each table is:
- Disable Audit logging trigger
- This will reduce overhead and minimize additions to the audit log tables
- Add a temporary
varbinary(max)column to the table
- Copy the contents of all documents into the temp column
- Remove the old data column
- Rename the temp column to the data column
- Drop 2 constraints on the
Uidcolumn and drop the
Uidcolumn that was used by Filestream
- Turn off Filestream at the table level
- Re-enable Audit logging trigger
Steps to Prepare the database.
- Ensure there is enough disk space (2 times the current database .mdf size free)
- Switch the database to Simple recovery mode (under Options in the database properties), if this is not already done.
- Remove the full-text indexes. For on-premise versions of Maximizer, the script is located in the following directory: \Maximizer\DbScripts\SqlServer\Drop_FTS_Indexes_script.sql
Steps for Filestream Removal
- Step through the Filestream Removal script below running just one command at a time in SQL Management studio. You will need to replace the constraint names in red with the actual constraints in the database. They can be found by expanding
AMGR_Documents_Tbl) and expanding Constraints to look for
AMGR_xxx_UId_xxx, or you can delete the constraint manually instead of running that script line.
--Run step by step in the SQL Management studio --ensure recovery mode is set to simple. Edit the database name specified (
ALTER DATABASE [
] SET RECOVERY SIMPLE;
--disable audit log trigger
Disable trigger AMGR_Letters_tbl_auditLog_trigger_all on dbo.amgr_letters_tbl
--Create a Temp Column to hold Filstream Data in SQL Table
ALTER Table AMGR_Letters_Tbl ADD tmp_TextCol varbinary(max) null
--Note that the next step is to copy data and may take a long time and use considerable disk space
UPDATE AMGR_Letters_Tbl SET tmp_TextCol = TextCol
ALTER TABLE AMGR_Letters_Tbl DROP COLUMN TextCol
sp_Rename 'AMGR_Letters_Tbl.tmp_TextCol', 'TextCol', 'COLUMN'
-- Remove constraints from the database. Constraints are uniquely named and you will need to update this command for each database
-- in the example below, navigate to the constraints of the table, and update the part in bold red with the uniquely named constraint
ALTER TABLE [dbo].[AMGR_Letters_Tbl]
DROP CONSTRAINT [
--Note: this next step is remove the UID column. If it fails, there may be some other object that refers to this. Common reasons for failure is that an index has been created for the letters table that includes UID. Explore the indexes of AMGR_Letters_Tbl to confirm. If there is an index that refers to UID, delete it.
DROP INDEX [AMGR_Letters_Tbl_UId_Unique] ON [dbo].[AMGR_Lettters_Tbl] WITH (ONLINE = OFF)
ALTER TABLE AMGR_Letters_Tbl DROP COLUMN [Uid]
--Finally, turn off Filestream for the Letters table
ALTER TABLE AMGR_Letters_Tbl set (Filestream_on = "NULL")
--Re enable audit log trigger
ENABLE trigger AMGR_Letters_tbl_auditLog_trigger_all on dbo.amgr_letters_tbl
-- Letters Table complete
-- Now start on the Documents Table -- Create a temp Column to hold FS data in AMGR_Documents_Tbl
ALTER TABLE AMGR_Documents_Tbl ADD tmp_Data varbinary(max) null
UPDATE AMGR_Documents_Tbl SET tmp_Data = Data
ALTER TABLE AMGR_Documents_Tbl DROP COLUMN Data
sp_Rename 'AMGR_Documents_Tbl.tmp_Data', 'Data', 'COLUMN'
-- This Constraint name will need to be updated for each Database.
ALTER TABLE AMGR_Documents_Tbl DROP CONSTRAINT [
DROP INDEX [AMGR_Documents_Tbl_UId_Unique] ON [dbo].[AMGR_Documents_Tbl] WITH (ONLINE = OFF)
ALTER TABLE AMGR_Documents_Tbl DROP COLUMN [Uid]
--Remove FILESTREAM assignment for AMGR_Documents_Tbl
ALTER TABLE AMGR_Documents_Tbl set (Filestream_on = "NULL")
- In SQL Management Studio, right-click on the database and open Properties. Under Files, locate the file of type “FILESTREAM Data” and remove it. Click OK to finish the file removal.
- In SQL Management Studio, right-click on the database and open Properties. Under Filegroups, remove all Filegroups in the FILESTREAM section. Click OK to finish the filegroup removal.
NB. You may find that it takes a few moments for the SQL management studio to update and allow the removal of the Filegroup
If all the steps are completed, you will be able to upgrade the Maximizer database in the normal way.