Removing Database Mail History
Issue
The SQL Server MSDB database can grow very large due to extensive use of Database Mail and/or sending large attachments via Database Mail.
Product
Struxureware Power Monitoring
Power Monitoring Expert
ION Enterprise
ION EEM
Environment
SQL Server 2005, SQL Server 2008 R2, SQL Server 2012, SQL Server 2014
Cause
SQL Server stores all mails and attachments in the MSDB database. To avoid unnecessary growth of the MSDB database you should remove
the mail history unless it is required for auditing or other purposes. If frequently sending large attachments using database mail this can cause the database MSDB to grow rapidly.
Resolution
*Warning: Irreparable database damage can occur. This procedure should only be performed by users familiar with SQL Server Management Studio. Databases should be backed up prior to performing this procedure.*
To display all mails processed by Database Mail, the sysmail_allitems view can be queried by executing one of the following statements.
There are 3 additional views available (sysmail_faileditems, sysmail_unsentitems, and sysmail_sentitems) which contain the mails of different status respectively.
Also, all attachments are stored within the MSDB database. These are contained in the sysmail_attachment table.
To delete mail items, use the system stored procedure sysmail_delete_mailitems_sp.
The syntax is as follows:
Mail can be deleted using either of the parameters. The @sent_before parameter specifies a date which all mail sent prior to the
specified date will be deleted. The @sent_status specifies the status of the mails which will be deleted.
For example, to delete all mails which are sent and are older than current month we can use:
After executing the above stored procedure to delete the unrequired mail, querying the sysmail_event_log view verifies the deletions that have been initiated.
The SQL Server MSDB database can grow very large due to extensive use of Database Mail and/or sending large attachments via Database Mail.
Product
Struxureware Power Monitoring
Power Monitoring Expert
ION Enterprise
ION EEM
Environment
SQL Server 2005, SQL Server 2008 R2, SQL Server 2012, SQL Server 2014
Cause
SQL Server stores all mails and attachments in the MSDB database. To avoid unnecessary growth of the MSDB database you should remove
the mail history unless it is required for auditing or other purposes. If frequently sending large attachments using database mail this can cause the database MSDB to grow rapidly.
Resolution
*Warning: Irreparable database damage can occur. This procedure should only be performed by users familiar with SQL Server Management Studio. Databases should be backed up prior to performing this procedure.*
To display all mails processed by Database Mail, the sysmail_allitems view can be queried by executing one of the following statements.
SELECT COUNT(*)
FROM msdb.dbo.sysmail_allitemsor
SELECT *
FROM msdb.dbo.sysmail_allitemsThere are 3 additional views available (sysmail_faileditems, sysmail_unsentitems, and sysmail_sentitems) which contain the mails of different status respectively.
Also, all attachments are stored within the MSDB database. These are contained in the sysmail_attachment table.
To delete mail items, use the system stored procedure sysmail_delete_mailitems_sp.
The syntax is as follows:
sysmail_delete_mailitems_sp [@sent_before] [@sent_status]Mail can be deleted using either of the parameters. The @sent_before parameter specifies a date which all mail sent prior to the
specified date will be deleted. The @sent_status specifies the status of the mails which will be deleted.
For example, to delete all mails which are sent and are older than current month we can use:
EXEC msdb.dbo.sysmail_delete_mailitems_sp
@sent_before = '2012-05-10 00:00:00',
@sent_status = 'sent'After executing the above stored procedure to delete the unrequired mail, querying the sysmail_event_log view verifies the deletions that have been initiated.
SELECT description
FROM sysmail_event_log
ORDER BY log_date required** see attachment containing the SQL script text
發佈於: 施耐德電機Taiwan


需要協助?
產品選型工具
快速輕鬆地為您的應用找到合適的產品和附件。
取得報價
立即線上提交您的銷售需求,專業團隊將主動聯繫您。
購買地點
輕鬆在您所在地區找到最近的施耐德電機經銷商。
支援中心
在同一位置找到滿足您所有需求的支援資源。