-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathsp_Tablecount.sql
287 lines (251 loc) · 16.5 KB
/
sp_Tablecount.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
USE [master]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF OBJECT_ID('sp_Tablecount') IS NULL
BEGIN
EXEC('CREATE PROCEDURE sp_Tablecount AS');
END
GO
/*********************************************
Procedure Name: sp_Tablecount
Author: Adrian Buckman
Revision date: 20/07/2021
Version: 3
© www.sqlundercover.com
MIT License
------------
Copyright 2019 Sql Undercover
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*********************************************/
ALTER PROCEDURE [dbo].[sp_Tablecount] (
@Databasename NVARCHAR(128) = NULL,
@Schemaname NVARCHAR(128) = NULL,
@Tablename NVARCHAR(128) = NULL,
@Sortorder NVARCHAR(30) = NULL, --VALID OPTIONS 'Schema' 'Table' 'Rows' 'Delta' 'Size'
@Top INT = NULL,
@Interval TINYINT = NULL,
@Getsizes BIT = 0
)
AS
BEGIN
SET NOCOUNT ON;
DECLARE @Sql NVARCHAR(4000);
DECLARE @Delay VARCHAR(8);
IF OBJECT_ID('tempdb.dbo.#RowCounts') IS NOT NULL
DROP TABLE [#RowCounts]
CREATE TABLE [#RowCounts] (
Schemaname NVARCHAR(128),
Tablename NVARCHAR(128),
TotalRows BIGINT,
SizeMB MONEY,
StorageInfo XML,
IndexTypes VARCHAR(256)
);
--Show debug info:
PRINT 'Parameter values:'
PRINT '@Databasename: '+ISNULL(@Databasename,'NULL');
PRINT '@Schemaname: '+ISNULL(@Schemaname,'NULL');
PRINT '@Tablename: '+ISNULL(@Tablename,'NULL')
PRINT '@Sortorder: '+ISNULL(@Sortorder,'NULL');
PRINT '@Top: '+ISNULL(CAST(@Top AS VARCHAR(20)),'NULL');
PRINT '@Interval '+ISNULL(CAST(@Interval AS VARCHAR(3)),'NULL');
PRINT '@Getsizes '+ISNULL(CAST(@Getsizes AS CHAR(1)),'NULL');
IF @Databasename IS NULL
BEGIN
SET @Databasename = DB_NAME();
END
--Ensure database exists.
IF DB_ID(@Databasename) IS NULL
BEGIN
RAISERROR('Invalid databasename',11,0);
RETURN;
END
--Delta maximum is 60 seconds
IF (@Interval > 60)
BEGIN
SET @Interval = 60;
PRINT '@Interval was changed to the maximum value of 60 seconds';
END
--Set delay for WAITFOR
IF (@Interval IS NOT NULL AND @Interval > 0)
BEGIN
SET @Delay = CASE
WHEN @Interval = 60 THEN '00:01:00'
WHEN @Interval < 10 THEN '00:00:0'+CAST(@Interval AS VARCHAR(2))
ELSE '00:00:'+CAST(@Interval AS VARCHAR(2))
END;
END
--UPPER @Sortorder
IF @Sortorder IS NOT NULL
BEGIN
SET @Sortorder = UPPER(@Sortorder);
IF @Sortorder NOT IN ('SCHEMA','TABLE','ROWS','DELTA','SIZE')
BEGIN
RAISERROR('Valid options for @Sortorder are ''Schema'' ''Table'' ''Rows'' ''Delta'' ''Size''',11,0);
RETURN;
END
IF (@Sortorder = 'DELTA' AND (@Interval IS NULL OR @Interval = 0))
BEGIN
RAISERROR('@Sortorder = Delta is invalid with @Interval is null or zero',11,0);
RETURN;
END
IF (@Getsizes = 0 AND @Sortorder = 'SIZE')
BEGIN
PRINT '@Sortorder = ''Size'' is not compatible with @Getsizes = 0, using default sortorder';
END
END
SET @Sql = N'
SELECT'
+CASE
WHEN @Top IS NOT NULL THEN ' TOP ('+CAST(@Top AS VARCHAR(20))+')'
ELSE ''
END
+'
schemas.name AS Schemaname,
tables.name AS Tablename,
partitions.rows AS TotalRows,
'+
CASE
WHEN @Getsizes = 1 THEN 'ISNULL((SELECT SUM((CAST(total_pages AS MONEY)*8)/1024)
FROM ['+@Databasename+'].sys.allocation_units Allocunits
WHERE partitions.partition_id = Allocunits.container_id ),0.00) AS SizeMB,'
ELSE ''
END
+'CAST(Allocunits.PageInfo AS XML) AS StorageInfo,
ISNULL((SELECT type_desc + '': ''+CAST(COUNT(*) AS VARCHAR(6))+ '' ''
FROM ['+@Databasename+'].sys.indexes
WHERE object_id = tables.object_id AND indexes.type > 0
GROUP BY type_desc
ORDER BY type_desc
FOR XML PATH('''')),''HEAP'') AS IndexTypes
FROM ['+@Databasename+'].sys.tables
INNER JOIN ['+@Databasename+'].sys.schemas ON tables.schema_id = schemas.schema_id
INNER JOIN ['+@Databasename+'].sys.partitions ON tables.object_id = partitions.object_id
CROSS APPLY (SELECT type_desc
+ N'': Total pages: ''
+CAST(total_pages AS NVARCHAR(10))
+ '' ''
+CHAR(13)+CHAR(10)
+N'' Used pages: ''
+CAST(used_pages AS NVARCHAR(10))
+ '' ''
+CHAR(13)+CHAR(10)
+N'' Total Size: ''
+CAST((total_pages*8)/1024 AS NVARCHAR(10))
+N''MB''
+N'' ''
FROM ['+@Databasename+'].sys.allocation_units Allocunits
WHERE partitions.partition_id = Allocunits.container_id
ORDER BY type_desc ASC
FOR XML PATH('''')) Allocunits (PageInfo)
WHERE index_id IN (0,1)'
+
CASE
WHEN @Tablename IS NULL THEN ''
ELSE '
AND tables.name = @Tablename'
END
+
CASE
WHEN @Schemaname IS NULL THEN ''
ELSE '
AND schemas.name = @Schemaname'
END+'
ORDER BY '
+CASE
WHEN @Sortorder = 'SCHEMA' THEN 'schemas.name ASC,tables.name ASC;'
WHEN @Sortorder = 'TABLE' THEN 'tables.name ASC;'
WHEN @Sortorder = 'ROWS' THEN 'partitions.rows DESC'
WHEN @Getsizes = 1 AND @Sortorder = 'SIZE' THEN 'SizeMB DESC'
ELSE 'schemas.name ASC,tables.name ASC;'
END
PRINT '
Dynamic SQL:';
PRINT @Sql;
IF (@Interval IS NULL OR @Interval = 0)
BEGIN
EXEC sp_executesql @Sql,
N'@Tablename NVARCHAR(128), @Schemaname NVARCHAR(128)',
@Tablename = @Tablename, @Schemaname = @Schemaname;
END
ELSE
BEGIN
IF @Getsizes = 0
BEGIN
INSERT INTO #RowCounts (Schemaname,Tablename,TotalRows,StorageInfo,IndexTypes)
EXEC sp_executesql @Sql,
N'@Tablename NVARCHAR(128), @Schemaname NVARCHAR(128)',
@Tablename = @Tablename, @Schemaname = @Schemaname;
END
IF @Getsizes = 1
BEGIN
INSERT INTO #RowCounts (Schemaname,Tablename,TotalRows,SizeMB,StorageInfo,IndexTypes)
EXEC sp_executesql @Sql,
N'@Tablename NVARCHAR(128), @Schemaname NVARCHAR(128)',
@Tablename = @Tablename, @Schemaname = @Schemaname;
END
WAITFOR DELAY @Delay;
SET @Sql = N'
SELECT'
+CASE
WHEN @Top IS NOT NULL THEN ' TOP ('+CAST(@Top AS VARCHAR(20))+')'
ELSE ''
END
+'
schemas.name AS Schemaname,
tables.name AS Tablename,
#RowCounts.TotalRows AS TotalRows,
partitions.rows-#RowCounts.TotalRows AS TotalRows_Delta,
'+
CASE
WHEN @Getsizes = 1 THEN '#RowCounts.SizeMB,
ISNULL((SELECT SUM((CAST(total_pages AS MONEY)*8)/1024)
FROM ['+@Databasename+'].sys.allocation_units Allocunits
WHERE partitions.partition_id = Allocunits.container_id ),0.00)-#RowCounts.SizeMB AS SizeMB_Delta,'
ELSE ''
END
+'
#RowCounts.StorageInfo,
#RowCounts.IndexTypes
FROM ['+@Databasename+'].sys.tables
INNER JOIN ['+@Databasename+'].sys.schemas ON tables.schema_id = schemas.schema_id
INNER JOIN ['+@Databasename+'].sys.partitions ON tables.object_id = partitions.object_id
INNER JOIN #RowCounts ON tables.name = #RowCounts.Tablename COLLATE DATABASE_DEFAULT AND schemas.name = #RowCounts.Schemaname COLLATE DATABASE_DEFAULT
WHERE index_id IN (0,1)'
+
CASE
WHEN @Tablename IS NULL THEN ''
ELSE '
AND tables.name = @Tablename'
END
+
CASE
WHEN @Schemaname IS NULL THEN ''
ELSE '
AND schemas.name = @Schemaname'
END+'
ORDER BY '
+CASE
WHEN @Sortorder = 'SCHEMA' THEN 'schemas.name ASC,tables.name ASC;'
WHEN @Sortorder = 'TABLE' THEN 'tables.name ASC;'
WHEN @Sortorder = 'ROWS' THEN 'partitions.rows DESC'
WHEN @Sortorder ='DELTA' THEN 'ABS(partitions.rows-#RowCounts.TotalRows) DESC'
WHEN @Getsizes = 1 AND @Sortorder = 'SIZE' THEN 'SizeMB DESC'
ELSE 'schemas.name ASC,tables.name ASC;'
END
EXEC sp_executesql @Sql,
N'@Tablename NVARCHAR(128), @Schemaname NVARCHAR(128)',
@Tablename = @Tablename, @Schemaname = @Schemaname;
END
END