{"id":65,"date":"2013-06-14T12:01:29","date_gmt":"2013-06-14T17:01:29","guid":{"rendered":"https:\/\/www.chrystalsander.com\/blog\/?p=65"},"modified":"2023-07-27T12:02:33","modified_gmt":"2023-07-27T17:02:33","slug":"sql-set-up-email-alert-when-partition-size-reaches-threshold","status":"publish","type":"post","link":"https:\/\/www.chrystalsander.com\/blog\/?p=65","title":{"rendered":"SQL: Set Up Email Alert When Partition Size Reaches Threshold"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>-- =============================================\n-- Query to determine if a partition's size may\n-- be reaching a critical maximum\n-- Note: Currently uses SP_SEND_DBMAIL instead of XP_SENDMAIL\n-- =============================================\n\nDECLARE @DbName VARCHAR(25)\nDECLARE @sql VARCHAR(100)\nDECLARE @ThresholdCountMsg INT\nDECLARE @Subject VARCHAR(100)\n\nSET @DbName = DB_NAME(); --get name of current database to use in email alert\n\nSET @Subject = 'Partitioning Threshold Alert On ' + @DbName + '!'\n\nSET @ThresholdCountMsg = 14000000;\u00a0 -- set partition row count\n\nIF OBJECT_ID('tempdb..##yourPartitionSizeCheck') IS NOT NULL\nBEGIN\nDROP TABLE ##yourPartitionSizeCheck\nEND;\n\u00a0\n\n--use global temp table for email alert\n\nCREATE TABLE ##yourPartitionSizeCheck\n(\ntable_name varchar(20),\npartition_number int,\nrow_count int\n);\n\n\u00a0\n\nINSERT INTO ##yourPartitionSizeCheck\n\nSELECT b.name AS 'table_name',\npartition_number,\na.&#91;rows] AS 'row_count'\nFROM sys.partitions AS a INNER JOIN\nsys.objects AS b ON a.object_id = b.object_id\nINNER JOIN sys.schemas AS c On c.schema_id = b.schema_id\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\nWHERE LOWER(b.name) = 'yourPartitionedTableName' \u00a0\u00a0\u00a0\u00a0\nand a.index_id &lt; 2\nand a.&#91;rows] > @ThresholdCountMsg;\u00a0\n\nIF @@ROWCOUNT > 0\n\nEXEC MSDB.DBO.SP_SEND_DBMAIL\n@profile_name = 'YourSqlmail',\n@recipients = '&#121;&#x6f;&#117;&#x72;&#46;&#x65;&#109;&#x61;&#105;&#x6c;&#64;&#x79;&#111;&#x75;&#114;&#x63;&#111;&#x6d;&#112;&#x61;&#110;&#x79;&#46;&#x63;&#111;&#x6d;',\n@subject = @Subject,\n@body = 'The following partitions may be approaching performance degradation: ',\u00a0\u00a0\u00a0\n\n@execute_query_database = @DbName,\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\n\n@query = 'SELECT table_name, partition_number, row_count FROM ##yourPartitionSizeCheck';<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[45,27],"tags":[49],"_links":{"self":[{"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/65"}],"collection":[{"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=65"}],"version-history":[{"count":1,"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/65\/revisions"}],"predecessor-version":[{"id":266,"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/65\/revisions\/266"}],"wp:attachment":[{"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=65"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=65"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=65"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}