Skip to content

Commit 684e82e

Browse files
authored
Merge pull request #221 from LowlyDBA/development
Development
2 parents 16bcfe9 + 84f8540 commit 684e82e

File tree

6 files changed

+440
-348
lines changed

6 files changed

+440
-348
lines changed

appveyor/sqlcover/Coverage.opencoverxml

Lines changed: 296 additions & 293 deletions
Large diffs are not rendered by default.

appveyor/sqlcover/[dbo].[sp_estindex]

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ CREATE PROCEDURE [dbo].[sp_estindex]
88
,@IsUnique BIT = 0
99
,@Filter NVARCHAR(2048) = ''
1010
,@FillFactor TINYINT = 100
11+
,@VarcharFillPercent TINYINT = 100
1112
,@Verbose BIT = 0
1213
-- Unit testing only
1314
,@SqlMajorVersion TINYINT = 0
@@ -21,7 +22,7 @@ sp_estindex - Estimate a new index's size and statistics.
2122

2223
Part of the DBA MultiTool http://dba-multitool.org
2324

24-
Version: 20210405
25+
Version: 20210908
2526

2627
MIT License
2728

@@ -93,6 +94,13 @@ BEGIN TRY
9394
THROW 51000, @Msg, 1;
9495
END;
9596

97+
/* Validate Varchar Fill Percent */
98+
IF (@VarcharFillPercent > 100 OR @VarcharFillPercent < 1)
99+
BEGIN;
100+
SET @Msg = 'Varchar fill percent must be between 1 and 100.';
101+
THROW 51000, @Msg, 1;
102+
END;
103+
96104
/* Validate Filter */
97105
IF (@Filter <> '' AND LEFT(@Filter, 5) <> 'WHERE')
98106
BEGIN;
@@ -282,6 +290,7 @@ BEGIN TRY
282290
,@NullCols INT = 0
283291
,@IndexNullBitmap BIGINT = 0
284292
,@VariableKeySize BIGINT = 0
293+
,@VariableKeyFillModifier DECIMAL(3,2) = (@VarcharFillPercent / 100)
285294
,@TotalFixedKeySize BIGINT = 0
286295
,@IndexRowSize BIGINT = 0
287296
,@IndexRowsPerPage BIGINT = 0
@@ -501,23 +510,26 @@ BEGIN TRY
501510
END;
502511

503512
-- Calculate variable length data size
504-
-- Assumes each col is 100% full
513+
-- Assumes each col is 100% full unless
514+
-- otherwise specified
505515
IF (@NumVariableKeyCols > 0)
506516
BEGIN;
507-
SET @VariableKeySize = 2 + (@NumVariableKeyCols * 2) + @MaxVarKeySize; --The bytes added to @MaxVarKeySize are for tracking each variable column.
517+
--The bytes added to @MaxVarKeySize are for tracking each variable column.
518+
SET @VariableKeySize = 2 + (@NumVariableKeyCols * 2) + (@MaxVarKeySize * @VariableKeyFillModifier);
508519
END;
509520

510521
-- Calculate index row size
511-
SET @IndexRowSize = @FixedKeySize + @VariableKeySize + @IndexNullBitmap + 1 + 6; -- + 1 (for row header overhead of an index row) + 6 (for the child page ID pointer)
512-
522+
-- + 1 (for row header overhead of an index row) + 6 (for the child page ID pointer)
523+
SET @IndexRowSize = @FixedKeySize + @VariableKeySize + @IndexNullBitmap + 1 + 6;
513524
IF (@Verbose = 1)
514525
BEGIN
515526
SET @Msg = CONCAT('IndexRowSize: ', @IndexRowSize);
516527
RAISERROR(@Msg, 10, 1) WITH NOWAIT;
517528
END;
518529

519-
--Calculate number of index rows / page
520-
SET @IndexRowsPerPage = FLOOR(@FreeBytesPerPage / (@IndexRowSize + 2)); -- + 2 for the row's entry in the page's slot array.
530+
-- Calculate number of index rows / page
531+
-- + 2 for the row's entry in the page's slot array.
532+
SET @IndexRowsPerPage = FLOOR(@FreeBytesPerPage / (@IndexRowSize + 2));
521533

522534
IF (@Verbose = 1)
523535
BEGIN

docs/sp_estindex.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ how an index would look without having to actually create it!
3333
| @IsUnique | BIT | no | Whether or not the index is UNIQUE. Default is 0. |
3434
| @Filter | NVARCHAR(2048) | no | Optional filter for the index. |
3535
| @FillFactor | TINYINT | no | Optional fill factor for the index. Default is 100. |
36+
| @VarcharFillPercent | TINYINT | no | Optional estimated fill percent of data in variable length columns. Default is 100. |
3637
| @Verbose | BIT | no | Show intermediate variables used in size calculations. Default is 0. |
3738
| @SqlMajorVersion | TINYINT | no | For unit testing only. |
3839

install_dba-multitool.sql

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2292,6 +2292,12 @@ IF EXISTS (SELECT * FROM sys.fn_listextendedproperty(N'@FillFactor' , N'SCHEMA'
22922292
END;
22932293
GO
22942294

2295+
IF EXISTS (SELECT * FROM sys.fn_listextendedproperty(N'@VarcharFillPercent' , N'SCHEMA',N'dbo', N'PROCEDURE',N'sp_estindex', NULL,NULL))
2296+
BEGIN;
2297+
EXEC sys.sp_dropextendedproperty @name=N'@VarcharFillPercent' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'PROCEDURE',@level1name=N'sp_estindex';
2298+
END;
2299+
GO
2300+
22952301
IF EXISTS (SELECT * FROM sys.fn_listextendedproperty(N'@DatabaseName' , N'SCHEMA',N'dbo', N'PROCEDURE',N'sp_estindex', NULL,NULL))
22962302
BEGIN;
22972303
EXEC sys.sp_dropextendedproperty @name=N'@DatabaseName' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'PROCEDURE',@level1name=N'sp_estindex';
@@ -2322,6 +2328,7 @@ ALTER PROCEDURE [dbo].[sp_estindex]
23222328
,@IsUnique BIT = 0
23232329
,@Filter NVARCHAR(2048) = ''
23242330
,@FillFactor TINYINT = 100
2331+
,@VarcharFillPercent TINYINT = 100
23252332
,@Verbose BIT = 0
23262333
-- Unit testing only
23272334
,@SqlMajorVersion TINYINT = 0
@@ -2335,7 +2342,7 @@ sp_estindex - Estimate a new index's size and statistics.
23352342
23362343
Part of the DBA MultiTool http://dba-multitool.org
23372344
2338-
Version: 20210405
2345+
Version: 20210908
23392346
23402347
MIT License
23412348
@@ -2407,6 +2414,13 @@ BEGIN TRY
24072414
THROW 51000, @Msg, 1;
24082415
END;
24092416

2417+
/* Validate Varchar Fill Percent */
2418+
IF (@VarcharFillPercent > 100 OR @VarcharFillPercent < 1)
2419+
BEGIN;
2420+
SET @Msg = 'Varchar fill percent must be between 1 and 100.';
2421+
THROW 51000, @Msg, 1;
2422+
END;
2423+
24102424
/* Validate Filter */
24112425
IF (@Filter <> '' AND LEFT(@Filter, 5) <> 'WHERE')
24122426
BEGIN;
@@ -2596,6 +2610,7 @@ BEGIN TRY
25962610
,@NullCols INT = 0
25972611
,@IndexNullBitmap BIGINT = 0
25982612
,@VariableKeySize BIGINT = 0
2613+
,@VariableKeyFillModifier DECIMAL(3,2) = (@VarcharFillPercent / 100)
25992614
,@TotalFixedKeySize BIGINT = 0
26002615
,@IndexRowSize BIGINT = 0
26012616
,@IndexRowsPerPage BIGINT = 0
@@ -2815,23 +2830,26 @@ BEGIN TRY
28152830
END;
28162831

28172832
-- Calculate variable length data size
2818-
-- Assumes each col is 100% full
2833+
-- Assumes each col is 100% full unless
2834+
-- otherwise specified
28192835
IF (@NumVariableKeyCols > 0)
28202836
BEGIN;
2821-
SET @VariableKeySize = 2 + (@NumVariableKeyCols * 2) + @MaxVarKeySize; --The bytes added to @MaxVarKeySize are for tracking each variable column.
2837+
--The bytes added to @MaxVarKeySize are for tracking each variable column.
2838+
SET @VariableKeySize = 2 + (@NumVariableKeyCols * 2) + (@MaxVarKeySize * @VariableKeyFillModifier);
28222839
END;
28232840

28242841
-- Calculate index row size
2825-
SET @IndexRowSize = @FixedKeySize + @VariableKeySize + @IndexNullBitmap + 1 + 6; -- + 1 (for row header overhead of an index row) + 6 (for the child page ID pointer)
2826-
2842+
-- + 1 (for row header overhead of an index row) + 6 (for the child page ID pointer)
2843+
SET @IndexRowSize = @FixedKeySize + @VariableKeySize + @IndexNullBitmap + 1 + 6;
28272844
IF (@Verbose = 1)
28282845
BEGIN
28292846
SET @Msg = CONCAT('IndexRowSize: ', @IndexRowSize);
28302847
RAISERROR(@Msg, 10, 1) WITH NOWAIT;
28312848
END;
28322849

2833-
--Calculate number of index rows / page
2834-
SET @IndexRowsPerPage = FLOOR(@FreeBytesPerPage / (@IndexRowSize + 2)); -- + 2 for the row's entry in the page's slot array.
2850+
-- Calculate number of index rows / page
2851+
-- + 2 for the row's entry in the page's slot array.
2852+
SET @IndexRowsPerPage = FLOOR(@FreeBytesPerPage / (@IndexRowSize + 2));
28352853

28362854
IF (@Verbose = 1)
28372855
BEGIN
@@ -3114,6 +3132,9 @@ GO
31143132
EXEC sys.sp_addextendedproperty @name=N'Description', @value=N'Estimate a new index''s size and statistics.' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'PROCEDURE',@level1name=N'sp_estindex';
31153133
GO
31163134

3135+
EXEC sys.sp_addextendedproperty @name=N'@VarcharFillPercent', @value=N'Optional estimated fill percent of data in variable length columns. Default is 100.' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'PROCEDURE',@level1name=N'sp_estindex';
3136+
GO
3137+
31173138
EXEC sys.sp_addextendedproperty @name=N'@Verbose', @value=N'Show intermediate variables used in size calculations. Default is 0.' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'PROCEDURE',@level1name=N'sp_estindex';
31183139
GO
31193140
SET ANSI_NULLS ON;

sp_estindex.sql

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,12 @@ IF EXISTS (SELECT * FROM sys.fn_listextendedproperty(N'@FillFactor' , N'SCHEMA'
5858
END;
5959
GO
6060

61+
IF EXISTS (SELECT * FROM sys.fn_listextendedproperty(N'@VarcharFillPercent' , N'SCHEMA',N'dbo', N'PROCEDURE',N'sp_estindex', NULL,NULL))
62+
BEGIN;
63+
EXEC sys.sp_dropextendedproperty @name=N'@VarcharFillPercent' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'PROCEDURE',@level1name=N'sp_estindex';
64+
END;
65+
GO
66+
6167
IF EXISTS (SELECT * FROM sys.fn_listextendedproperty(N'@DatabaseName' , N'SCHEMA',N'dbo', N'PROCEDURE',N'sp_estindex', NULL,NULL))
6268
BEGIN;
6369
EXEC sys.sp_dropextendedproperty @name=N'@DatabaseName' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'PROCEDURE',@level1name=N'sp_estindex';
@@ -88,6 +94,7 @@ ALTER PROCEDURE [dbo].[sp_estindex]
8894
,@IsUnique BIT = 0
8995
,@Filter NVARCHAR(2048) = ''
9096
,@FillFactor TINYINT = 100
97+
,@VarcharFillPercent TINYINT = 100
9198
,@Verbose BIT = 0
9299
-- Unit testing only
93100
,@SqlMajorVersion TINYINT = 0
@@ -101,7 +108,7 @@ sp_estindex - Estimate a new index's size and statistics.
101108
102109
Part of the DBA MultiTool http://dba-multitool.org
103110
104-
Version: 20210405
111+
Version: 20210908
105112
106113
MIT License
107114
@@ -173,6 +180,13 @@ BEGIN TRY
173180
THROW 51000, @Msg, 1;
174181
END;
175182

183+
/* Validate Varchar Fill Percent */
184+
IF (@VarcharFillPercent > 100 OR @VarcharFillPercent < 1)
185+
BEGIN;
186+
SET @Msg = 'Varchar fill percent must be between 1 and 100.';
187+
THROW 51000, @Msg, 1;
188+
END;
189+
176190
/* Validate Filter */
177191
IF (@Filter <> '' AND LEFT(@Filter, 5) <> 'WHERE')
178192
BEGIN;
@@ -362,6 +376,7 @@ BEGIN TRY
362376
,@NullCols INT = 0
363377
,@IndexNullBitmap BIGINT = 0
364378
,@VariableKeySize BIGINT = 0
379+
,@VariableKeyFillModifier DECIMAL(3,2) = (@VarcharFillPercent / 100)
365380
,@TotalFixedKeySize BIGINT = 0
366381
,@IndexRowSize BIGINT = 0
367382
,@IndexRowsPerPage BIGINT = 0
@@ -581,23 +596,26 @@ BEGIN TRY
581596
END;
582597

583598
-- Calculate variable length data size
584-
-- Assumes each col is 100% full
599+
-- Assumes each col is 100% full unless
600+
-- otherwise specified
585601
IF (@NumVariableKeyCols > 0)
586602
BEGIN;
587-
SET @VariableKeySize = 2 + (@NumVariableKeyCols * 2) + @MaxVarKeySize; --The bytes added to @MaxVarKeySize are for tracking each variable column.
603+
--The bytes added to @MaxVarKeySize are for tracking each variable column.
604+
SET @VariableKeySize = 2 + (@NumVariableKeyCols * 2) + (@MaxVarKeySize * @VariableKeyFillModifier);
588605
END;
589606

590607
-- Calculate index row size
591-
SET @IndexRowSize = @FixedKeySize + @VariableKeySize + @IndexNullBitmap + 1 + 6; -- + 1 (for row header overhead of an index row) + 6 (for the child page ID pointer)
592-
608+
-- + 1 (for row header overhead of an index row) + 6 (for the child page ID pointer)
609+
SET @IndexRowSize = @FixedKeySize + @VariableKeySize + @IndexNullBitmap + 1 + 6;
593610
IF (@Verbose = 1)
594611
BEGIN
595612
SET @Msg = CONCAT('IndexRowSize: ', @IndexRowSize);
596613
RAISERROR(@Msg, 10, 1) WITH NOWAIT;
597614
END;
598615

599-
--Calculate number of index rows / page
600-
SET @IndexRowsPerPage = FLOOR(@FreeBytesPerPage / (@IndexRowSize + 2)); -- + 2 for the row's entry in the page's slot array.
616+
-- Calculate number of index rows / page
617+
-- + 2 for the row's entry in the page's slot array.
618+
SET @IndexRowsPerPage = FLOOR(@FreeBytesPerPage / (@IndexRowSize + 2));
601619

602620
IF (@Verbose = 1)
603621
BEGIN
@@ -880,5 +898,8 @@ GO
880898
EXEC sys.sp_addextendedproperty @name=N'Description', @value=N'Estimate a new index''s size and statistics.' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'PROCEDURE',@level1name=N'sp_estindex';
881899
GO
882900

901+
EXEC sys.sp_addextendedproperty @name=N'@VarcharFillPercent', @value=N'Optional estimated fill percent of data in variable length columns. Default is 100.' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'PROCEDURE',@level1name=N'sp_estindex';
902+
GO
903+
883904
EXEC sys.sp_addextendedproperty @name=N'@Verbose', @value=N'Show intermediate variables used in size calculations. Default is 0.' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'PROCEDURE',@level1name=N'sp_estindex';
884905
GO

0 commit comments

Comments
 (0)