SQL Maintenance for SOLIDWORKS PDM Standard

Article by Bryce Hooper on Dec 22, 2020

The SQL backbone is a highly organized structure that allows SOLIDWORKS PDM to operate quickly and efficiently. Over time, this indexed structure can get itself out of order and reduce its efficiency. SQL Standard provides maintenance plans to help combat this. However, SQL Express does not have these automation or maintenance options. To perform this maintenance the rebuild and reorganizations must be done manually per table. With a little creative scripting, we can still automate this as well. 

GoEngineer Application Mentoring Session

Rebuilding and Reorganizing T-SQL 

For us to automate this we first need to generate some T-SQL. SOLIDWORKS gives us a leg up on this part by providing a procedure to check what the current fragmentation of all tables are and by design rebuilds or reorganizes them based on this fragmentation level. This isn’t necessarily ideal in terms of what we would need to do regularly. With some modifications we can split this script into two; one for rebuilds and another for reorganization.  

The first part of the script is to build a temporary table and we’ll borrow this from SOLIDWORKS Knowledgebase article S-074949. This bit of code will go in both our rebuild and reorganization scripts. Create a new query in SQL Management Studio and paste this code in. This code will build a temporary table that identifies the index fragmentation of the PDM tables. This will be used to determine which tables need which form of maintenance.   

SQL Maintenance for SOLIDWORKS PDM Standard
Rebuild and Reorganize T-SQL

To create our reorganizing script we’ll make an addition after the temporary table. It uses the table to reorganize any table with a fragmentation greater than 5% but less than 30%. Less than 5% is not necessary and greater than 30% requires a rebuild which the other script will cover. 

SOLIDWORKS PDM Standard SQL Script
SQL Script for SOLIDWORKS PDM Standard

In a second script for rebuilding, we’ll put the temporary table script and follow it up with a second edit of the script provided by SOLIDWORKS. This will take care of any table with fragmentation above 30%. 

SQL Script from SOLIDWORKS
SQL Script Fragmentation Run Rebuild

Save and close these when finished. We’ll rely on Task Scheduler and a batch script to execute them. 

Automating with Task Scheduler and a Batch Script 

The first step to automating is creating batch scripts to run the T-SQL through a command prompt. This is done with a simple command that we’ll use in the two batch scripts. Start by creating a text file and saving it as “Reorganize.bat” and then another as “Rebuild.bat”. The code will only vary between the two for which SQL script file we are pointing to. 

Automating Task Scheduler Batch Script

We then need to open the Windows Task Scheduler. We’re going to create two basic tasks to run on schedules. The schedule will be to run the Reorganize.bat weekly and the Rebuild.bat monthly. It is best to pick a day and time where there won’t be any activity in the vault. In the Task Scheduler go to Action > Create Basic Task. 

On the first page, we will give the task a name and hit next. The trigger for this task will be either weekly (for the reorganize) or monthly (for the rebuild). We’ll also need to specify which day of the week or month and a time. Pick a day and time where there should be no activity in the vault to avoid any issues. 

Create Basic Task Wizard Weekly
Create Basic Task Wizard Monthly

The action for the task will be to start a program. Click browse and select the appropriate batch script. After this, we can hit finish and repeat the process for the other script. The health of your PDM Standard database will be maintained automatically now. 

Learn more about SOLIDWORKS PDM 

SOLIDWORKS PDM Standard Automated SQL Backup

Moving the SOLIDWORKS PDM Vault Database Yourself? Start Here

SOLIDWORKS PDM Upgrading SQL 2014 to Service Pack 3

SOLIDWORKS PDM - How to Reset the SQL 'SA' Password

SOLIDWORKS PDM - Backup SQL 

 

About Bryce Hooper

Bryce has been using SOLIDWORKS since 2008 in multiple roles across multiple industries from industrial vacuum to RV frame design. He spent many of those years spent as a PDM administrator, implementing 3 PDM vaults along the way. He earned his CSWP in 2019, followed by his CSWE in 2013. Programming is a passion that has followed Bryce through his various positions, writing custom business applications to improve design processes. In his spare time Bryce is a gamer, a maker, and a brewer.

View all posts by Bryce Hooper