The IN_DB_UTIL_SP_CLEANUP_AUDIT_DATA stored procedure can be used to cleanup audit data that is stored in the database. This procedure will delete data from the IN_AUDIT_DETAIL, and IN_AUDIT_OBJ, and IN_AUDIT table based on the value of the KEEP_DAYS parameter or the BEGIN_DATETIME and END_DATETIME parameters.
WARNING: The execution of this procedure to cleanup audit data could result in lock escalation and create contention on the audit tables that could impact the creation of new audit data and result in delays and errors during normal operations. It is advised to only execute this procedure after hours or on weekends while monitoring for database contention to ensure minimal impact to the Perceptive Content application. If necessary, utilize smaller batch sizes with the BATCH_SIZE parameter to help keep transaction sizes smaller and reduce the scope and duration of database locks.
Parameter | Description | |
---|---|---|
MODE | Type: | INPUT |
Datatype: | TINYINT | |
Default Value: | 0 | |
Description | Specifies whether to count or delete all qualifying rows. | |
Options |
0 = Count Rows 1 = Delete Rows |
|
KEEP_DAYS | Type: | INPUT |
Datatype: | INTEGER | |
Default Value: | NULL | |
Description | Specifies the number of days of audit data that you want to keep. Uses DATEADD against GETUTCDATE(). | |
Options |
0 = Delete All Rows Any number of days greater than 0 |
|
BEGIN_DATETIME | Type: | INPUT |
Datatype: | NVARCHAR(40) | |
Default Value: | NULL | |
Description |
Specifies the start date[time] when deleting audit data based on a date range. Expected format is YEAR [MONTH] [DAY] [TIME] |
|
END_DATETIME | Type: | INPUT |
Datatype: | NVARCHAR(40) | |
Default Value: | NULL | |
Description | Specifies the end date[time] when deleting audit data based on a date
range. Expected format is YEAR [MONTH] [DAY] [TIME] |
|
CONVERT_TO_UTC | Type: | INPUT |
Datatype: | TINYINT | |
Default Value: | 1 | |
Description | Specifies whether to convert the entered BEGIN_DATETIME and END_DATETIME parameters to UTC. | |
Options |
0 = Do not convert to UTC 1 = Convert to UTC |
|
BATCH_SIZE | Type: | INPUT |
Datatype: | INTEGER | |
Default Value: | 100 | |
Description | Specifies the batch size if breaking up deletion into smaller transactions. Batch sizes greater than 1,000 could result in lock escalation and acquiring table locks on the audit tables which would block any inserts into the tables. | |
Options |
0 = Single transaction Any number greater than 50 |
|
USE_TIME | Type: | INPUT |
Datatype: | TINYINT | |
Default Value: | 0 | |
Description | Specifies whether to include the time parts for the date ranges. | |
Options |
0 = 12AM 1 = Current UTC time for KEEP_DAYS Time provided with the BEGIN_DATETIME and END_DATETIME parameters |
|
MAX_MINUTES | Type: | INPUT |
Datatype: | INTEGER | |
Default Value: | 0 | |
Description |
Specifies a time limit to determine whether to start deleting the next batch of rows. This only applies if using BATCH_SIZE greater than 0. Batches will run through completion but a new batch will not start if time has expired. |
|
Options |
0 = No Time Limit Any number of minutes greater than 0 |
|
DISPLAY_BATCHES | Type: | INPUT |
Datatype: | NVARCHAR(3) | |
Default Value: | YES | |
Description | Specifies whether to display the batch results and batch summary after each batch is completed. | |
Options | YES = Display the batch results after each batch. NO = Do not display the batch results after each batch. |
|
INCLUDE_SUMMARY | Type: | INPUT |
Datatype: | NVARCHAR(3) | |
Default Value: | YES | |
Description | Specifies whether to fetch and display the row counts of all qualifying rows for each table. Fetching summary data during execution could result in a much longer execution time as all qualifying rows are counted before any rows are deleted. | |
Options |
NO = Do not fetch and display the number of qualifying rows. YES = Fetch and display the number of qualifying rows for each table. |
|
DEBUG | Type: | INPUT |
Datatype: | NVARCHAR(3) | |
Default Value: | NO | |
Description |
Specifies whether to display the SQL being executed during the script. Will also display background operations including session setup and state changes and call stack details. |
|
Options |
NO = Do not display additional details to the Message tab. YES = Display additional details to the Messages tab. |
|
HELP | Type: | INPUT |
Datatype: | NVARCHAR(3) | |
Default Value: | NO | |
Description | Specifies whether to display the procedure usage examples. | |
Options |
NO = Do not display examples. YES = Only display examples. |
|
RETURN_VAL | Type: | RETURN |
Datatype: | INTEGER | |
Default Value: | 0 | |
Description |
Specifies whether the execution of the procedure was successful or not. 0 = Success 1 = Error |
Recommended index:
The following index is recommended to facilitate efficient searches using the CREATION_TIME column.
CREATE NONCLUSTERED INDEX AUDIT_IDX_01ON inuser.IN_AUDIT ( CREATION_TIME ASC, AUDIT_ID ASC) WITH (DATA_COMPRESSION = PAGE) ON [SECONDARY];
Examples
Display Usage Information Only
EXEC [IN_CLEANUP_AUDIT_DATA] @HELP = 'YES'
KEEP_DAYS Example:
The following will delete all audit data that is older than 365 days using batch sizes of 1,000 rows
Note that from a transaction standpoint the number of rows is based on qualifying rows in the IN_AUDIT table but the transaction also includes the deletion of rows from the IN_AUDIT_OBJ and IN_AUDIT_DETAIL tables which could have more or less than 1,000 each that correspond to the rows in the IN_AUDIT table. That means that each transaction will affect roughly three times the number of rows as specified in the BATCH_SIZE parameter. A BATCH_SIZE of 1,000 could result in a transaction size of roughly 3,000 or more rows. Beware of lock escalation that could result in table locks when using a batch size larger than 1,000 rows.
Note that in the examples below the Results tab will only contain the results of the final select statement displaying the RETURN_VAL of the procedure (0 = Success and 1 = Error). The output of the procedure, including number of rows counted or deleted, is displayed in the Messages tab.
DECLARE @return_value integer EXEC @return_value = IN_CLEANUP_AUDIT_DATA @MODE = 1 ,@KEEP_DAYS = 365 ,@BEGIN_DATETIME = NULL ,@END_DATETIME = NULL ,@CONVERT_TO_UTC = 1 ,@BATCH_SIZE = 1000 ,@USE_TIME = 0 ,@MAX_MINUTES = 0 ,@DISPLAY_BATCHES = 'YES' ,@INCLUDE_SUMMARY = 'NO' ,@DEBUG = 'NO' SELECT 'Return Value' = @return_value GO
DATE RANGE Example:
The following example will delete all audit data that was created on January 1, 2018 between 8am and 5pm using a batch size 1,000 rows.
Note that to preserve the time part of the timestamps you must set USE_TIME = 1
Note that CONVERT_TO_UTC = 1 (default) which means the provided timestamps will be converted to the UTC timestamp so that it aligns with the timestamps in the database.
DECLARE @return_value integer EXEC @return_value = IN_CLEANUP_AUDIT_DATA @MODE = 1 ,@KEEP_DAYS = NULL ,@BEGIN_DATETIME = 'January 01 2018 8:00 AM' ,@END_DATETIME = 'January 01 2018 5:00 PM' ,@CONVERT_TO_UTC = 1 ,@BATCH_SIZE = 1000 ,@USE_TIME = 1 ,@MAX_MINUTES = 0 ,@DISPLAY_BATCHES = 'YES' ,@INCLUDE_SUMMARY = 'YES' ,@DEBUG = 'NO' SELECT 'Return Value' = @return_value GO