Tech Note 975 - Defragmenting MES Databases
Tech Note 975 - Defragmenting MES Databases
All Tech Notes, Tech Alerts and KBCD documents and software are provided "as is" without warranty of any kind. See the Terms of Use for more information.
Topic#: 002800
Created: August 2013
Introduction
MES (SQLServer) databases need to be regularly defragmented. Indexes including the primary key will need to be defragmented on a
regular basis due to inserts and updates on the table.
Indexes may need to be rebuilt or reorganized depending on the level of fragmentation. Typically we want to reorganize the index if its
fragmentation is over 10% and less than 30%. Any indexes with over 30% fragmentation will require to be Rebuilt. If tables have less
than 1000 rows, the table is too small to be concerned with index fragmentation.
Wonderware generally recommends running the defragmentation on a weekly basis, during periods of low demand. If a Primary Key
index needs to be rebuilt, then it could cause a table lock and a production transaction could fail. If you are a 24x7 plant then you should
find time once a quarter where you can run the procedure. If you never defrag, your system performance will become progressively
slower, potentially causing locks that will cause transactions to fail. For this reason, we recommend you schedule a job to run and deal
with the potential failed transactions.
The standard Microsoft SQLServer maintenance plans have an Index Rebuild and Index Reorganize job. The problem with these jobs
is that they do not check the fragmentation level to determine if the jobs are needed or not.
This Tech Note provides detailed guidelines and options for defragmenting and maintaining your production databases.
Note: This operation is highly recommended for any Microsoft SQL Database used by Wonderware Products.
Application Versions
Factelligence 3.5
MES 4.x
Microsoft SQLServer 2005, 2008 and 2012 (including while running with compatibility mode 80)
ww_spDefrag
Temp_Frag_log
Temp_Frag_Data
2. Run the ww_spDefrag.sql script. This script creates the ww_spDefrag stored procedure that will do the defragmentation.
Option A
To schedule it to run whenever the job runs, use the following T-SQL:
Option B
To schedule the specific time to run, for example every Sunday at 2AM:
This could be useful if you have data transfer job the runs regularly and you don’t want the defrag to interfere with it. In this case, you
would add a step to the data transfer job to do the defrag. The stored procedure would run every period (say 10 minutes) but only
defrag when schedule (Sunday at 2AM).
Option C
To update more than just the current db (this examples does the MESDB and the ArchiveDB) at the default time Sunday at 2AM:
Parameter Details
The following table contains details on the parameters for the Stored Procedure.
ww_spDefrag Parameters
@p_Schedule Weekly Daily, Weekly, Monthly, Quarterly or Null to defrag when run.
@p_DayNo 7 CSV of day weekday:1=Sun,2=Mon,3=Tue,4=Wed,5=Thur,6=Fri,7=Sat). This value is the SQLServer weekday value.
@p_db_names Null Null/empty local db, Supports CSV for dbname like MESDB,ArchiveDB.
@p_min_row_Count 1000 Tables with less then 1000 rows will not be updated (default is 1000).
@p_FillFactor Null Index property, Null=not used, 1 to 100 valid, default Null.
@p_Sort_in_TempDB 1 Index property, 0=Off, 1=On, when sorting in tempdb more space is required to rebuild.
@p_OnLine 0 Index property, 0=Off, 1=On, only supported for Enterprise edition.
@p_Reorg_Page_Lock Fix Fix or Skip. If Fix, for reorg index will be altered to allow page locks (else it fails).
Note: You can change the name of the Database according to your application and version.
Name ArchiveBackupDefrag
Name: Archive
T-SQL: EXEC
FactelligenceProd.dbo.sp_Archive_Wo
Type: Full
• FactillegenceArchive
Databases:
Step 2 Backup Database Task • FactelligenceProd
• FactelligenceArchive
Step 3 Check Database Integrity Task Databases:
• FactelligenceProd
Extensions: bak
Make sure the connection between tasks is Completion so that even if it fails the next task is done (Figure 2 below).
D. McKenna
Tech Notes are published occasionally by Wonderware Technical Support. Publisher: Invensys Systems, Inc., 26561 Rancho Parkway South, Lake Forest, CA 92630. There is also
technical information on our software products at Wonderware Technical Support.
Back to top
©2013 Invensys Systems, Inc. All rights reserved. No part of the material protected by this copyright may be reproduced or utilized in any form or by any means, electronic or
mechanical, including photocopying, recording, broadcasting, or by anyinformation storage and retrieval system, without permission in writing from Invensys Systems, Inc.
Terms of Use.