Skip to content

Commit 2ea7bde

Browse files
committed
Add check constraint info
Fixes #1846
1 parent 37b2636 commit 2ea7bde

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

sp_BlitzIndex.sql

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,9 @@ IF OBJECT_ID('tempdb..#TraceStatus') IS NOT NULL
193193

194194
IF OBJECT_ID('tempdb..#TemporalTables') IS NOT NULL
195195
DROP TABLE #TemporalTables;
196+
197+
IF OBJECT_ID('tempdb..#CheckConstraints') IS NOT NULL
198+
DROP TABLE #CheckConstraints;
196199

197200
RAISERROR (N'Create temp tables.',0,1) WITH NOWAIT;
198201
CREATE TABLE #BlitzIndexResults
@@ -648,6 +651,23 @@ IF OBJECT_ID('tempdb..#TemporalTables') IS NOT NULL
648651
period_name NVARCHAR(128) NOT NULL
649652
);
650653

654+
CREATE TABLE #CheckConstraints
655+
(
656+
index_sanity_id INT IDENTITY(1, 1) NOT NULL,
657+
database_name NVARCHAR(128) NULL,
658+
database_id INT NOT NULL,
659+
table_name NVARCHAR(128) NOT NULL,
660+
schema_name NVARCHAR(128) NOT NULL,
661+
constraint_name NVARCHAR(128) NULL,
662+
is_disabled BIT NULL,
663+
definition NVARCHAR(MAX) NULL,
664+
uses_database_collation BIT NOT NULL,
665+
is_not_trusted BIT NOT NULL,
666+
is_function INT NOT NULL,
667+
column_definition NVARCHAR(MAX) NULL
668+
);
669+
670+
651671
/* Sanitize our inputs */
652672
SELECT
653673
@OutputServerName = QUOTENAME(@OutputServerName),
@@ -1547,6 +1567,30 @@ BEGIN TRY
15471567

15481568
EXEC sp_executesql @dsql;
15491569

1570+
SET @dsql=N'SELECT DB_ID(@i_DatabaseName) AS [database_id],
1571+
@i_DatabaseName AS database_name,
1572+
t.name AS table_name,
1573+
s.name AS schema_name,
1574+
cc.name AS constraint_name,
1575+
cc.is_disabled,
1576+
cc.definition,
1577+
cc.uses_database_collation,
1578+
cc.is_not_trusted,
1579+
CASE WHEN cc.definition LIKE ''%|].|[%'' ESCAPE ''|'' THEN 1 ELSE 0 END AS is_function,
1580+
''ALTER TABLE '' + QUOTENAME(s.name) + ''.'' + QUOTENAME(t.name) +
1581+
'' ADD CONSTRAINT '' + QUOTENAME(cc.name) + '' CHECK '' + cc.definition + '';'' COLLATE DATABASE_DEFAULT AS [column_definition]
1582+
FROM ' + QUOTENAME(@DatabaseName) + N'.sys.check_constraints AS cc
1583+
JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.tables AS t
1584+
ON t.object_id = cc.parent_object_id
1585+
JOIN ' + QUOTENAME(@DatabaseName) + N'.sys.schemas AS s
1586+
ON s.schema_id = t.schema_id
1587+
OPTION (RECOMPILE);';
1588+
1589+
INSERT #CheckConstraints
1590+
( database_id, [database_name], table_name, schema_name, constraint_name, is_disabled, definition,
1591+
uses_database_collation, is_not_trusted, is_function, column_definition )
1592+
EXEC sp_executesql @dsql, @params = N'@i_DatabaseName NVARCHAR(128)', @i_DatabaseName = @DatabaseName;
1593+
15501594
END;
15511595

15521596
END;
@@ -3954,7 +3998,28 @@ BEGIN;
39543998
WHERE NOT (@GetAllDatabases = 1 OR @Mode = 0)
39553999
OPTION ( RECOMPILE );
39564000

4001+
----------------------------------------
4002+
--Check Constraint Info: Check_id 120-129
4003+
----------------------------------------
39574004

4005+
RAISERROR(N'check_id 120: Check Constraints That Reference Functions', 0,1) WITH NOWAIT;
4006+
INSERT #BlitzIndexResults ( check_id, Priority, findings_group, finding, [database_name], URL, details, index_definition,
4007+
secret_columns, index_usage_summary, index_size_summary )
4008+
SELECT 99 AS check_id,
4009+
50 AS Priority,
4010+
'Obsessive Constraintive' AS findings_group,
4011+
'Serial Forcer' AS finding,
4012+
cc.database_name,
4013+
'' AS URL,
4014+
'The check constraint ' + QUOTENAME(cc.constraint_name) + ' on ' + QUOTENAME(cc.schema_name) + '.' + QUOTENAME(cc.table_name) + ' is based on ' + cc.definition
4015+
+ '. That indicates it may reference a scalar function, or a CLR function with data access, which can cause all queries and maintenance to run serially.' AS details,
4016+
cc.column_definition,
4017+
'N/A' AS secret_columns,
4018+
'N/A' AS index_usage_summary,
4019+
'N/A' AS index_size_summary
4020+
FROM #CheckConstraints AS cc
4021+
WHERE cc.is_function = 1
4022+
OPTION ( RECOMPILE );
39584023

39594024
END;
39604025

0 commit comments

Comments
 (0)