T-SQL : Finding Fill Rate of Table (2024)

Arulmouzhi

·

Follow

Published in

Analytics Vidhya

·

8 min read

·

Nov 28, 2020

--

The Fill Rate is one of the most important metric in many of the business related decisions. So Knowing Fill Rate is one of the essential tasks in Data Analysis and Decision making in business processes. In this article, we’re going to explain how to find the Fill Rate of a Table using T-SQL Queries.

T-SQL: Finding Fill Rate of Table (3)

Observing Fill Rate is the key factor for knowing and improving data quality that creates the fulfillment to both our management as well as our end users. Need to find Fill Rate of a Table, How to do it?

The Fill Rate is defined as the number of filled entries at each Column level that is divided by the total number of rows in a table. Depends on the total number of rows that is present in a table, we can figure out the Fill Rate easily.

The Schema changes like below things makes our Fill Rate approach little difficult than actual.

  • Table name changes
  • Column name changes
  • Data type changes
  • Removing Existing columns
  • Adding New Columns

Due to the above challenges, we cannot simply go for Static Solution to find Fill Rate of a table. Instead, we need something like Dynamic Approach to avoid our future re-works.

In the below sample, we are going to use one stored procedure named ‘Get_FillRate’ for demo. If any one have the same object name in database, please make sure to change the below stored procedure name.

--dropping temp table if exists

IF OBJECT_ID('TempDb..#TestEmp') IS NOT NULL

DROP TABLE #TestEmp;

CREATE TABLE #TestEmp

(

[TestEmp_Key] INT IDENTITY(1,1) NOT NULL,

[EmpName] VARCHAR(100) NOT NULL,

[Age] INT NULL,

[Address] VARCHAR(100) NULL,

[PhoneNo] VARCHAR(11) NULL,

[Inserted_dte] DATETIME NOT NULL,

[Updated_dte] DATETIME NULL,

CONSTRAINT [PK_TestEmp] PRIMARY KEY CLUSTERED

(

TestEmp_Key ASC

)

);

GO

INSERT INTO #TestEmp

(EmpName,Age,[Address],PhoneNo,Inserted_dte)

VALUES

('Arul',24,'xxxyyy','1234567890',GETDATE()),

('Gokul',22,'zzzyyy',NULL,GETDATE()),

('Krishna',24,'aaa','',GETDATE()),

('Adarsh',25,'bbb','1234567890',GETDATE()),

('Mani',21,'',NULL,GETDATE()),

('Alveena',20,'ddd',NULL,GETDATE()),

('Janani',30,'eee','',GETDATE()),

('Vino',26,NULL,'1234567890',GETDATE()),

('Madhi',25,'ggg',NULL,GETDATE()),

('Ronen',25,'ooo',NULL,GETDATE()),

('Visakh',25,'www',NULL,GETDATE()),

('Jayendran',NULL,NULL,NULL,GETDATE());

GO

SELECT [TestEmp_Key],[EmpName],[Age],[Address],[PhoneNo],[Inserted_dte],[Updated_dte] FROM #TestEmp;

GO

Temp Table — #TestEmp

T-SQL: Finding Fill Rate of Table (4)

Input Parameters

Both of the Input Parameters are mandatory.

1. @p_TableName — Data type used for this input Parameter is NVARCHAR(128) and Nullability is NOT NULL.

2. @p_Include_BlankAsNotFilled — Data type used for this input Parameter is BIT and Nullability is NOT NULL and either 0 or 1 needs to give. 0 is by Default and 0 means OFF. 1 is ON (when given as 1 — Blank entries will be considered As Not Filled Data).

Output Columns

There are Two output Columns. both of those are Non Nullable Output Columns.

1. [Column Name] — Data type used for this Output Column is sysname and Nullability is NOT NULL. All the Column Names for the user given Table Name would come as row values.

2. [Fill Rate (%)] — Data type used for this Output Column is DECIMAL(5,2) and Nullability is NOT NULL. Values from 0.00 to 100.00 would come in result with respective Column Names.

Info reg Stored Procedure

  • Created the store Procedure named — ‘Get_FillRate’.
  • To avoid the number of rows returned, set NOCOUNT as ON.
  • Try, Catch Blocks are added for error handling’s.
  • To read Uncommitted Modifications, set TRANSACTION ISOLATION LEVEL as READ UNCOMMITTED.
  • Parameter Sniffing Concept is also included.
  • Some handling’s done on the Table Name input parameters to support user typing table name formats like ‘.table_name’,’..table_name’,’…table_name’,’table_name’,’[table_name]’,’dbo.table_name’,’dbo.[table_name]’,’[dbo].[table_name]’ etc.,
  • Validation is included at the start, when user gives other than ‘table name’, stored procedure would throw ‘Table not exists in this Database’ as error message.
  • System table named SYS.OBJECTS and SYS.COLUMNS and System View named INFORMATION_SCHEMA.COLUMNS are used inside the stored procedure.
  • ORDINAL_POSITION from INFORMATION_SCHEMA.COLUMNS is used, to return the result set with the same column order that the table structure already has.
  • COLLATION_NAME from INFORMATION_SCHEMA.COLUMNS is used, to support conditions like blank is either need to consider or not, as not filled entries.
  • COLUMN_NAME from INFORMATION_SCHEMA.COLUMNS is used, to show the final result set with respective fill rates.
  • Dynamic Query is used, to support dynamic approach and this would avoid all the challenges that would come in static solutions like schema changes.
  • Both Method 1(Dynamic Query with WHILE LOOP) and Method 2(Dynamic Query with UNION ALL) produces same result sets and carries same functionality where some metrics like CPU time,Elapsed Time,Logical reads that are better in Method 2.

CREATE OR ALTER PROCEDURE [dbo].[Get_FillRate]

(

@p_TableName NVARCHAR(128),

@p_Include_BlankAsNotFilled BIT = 0 -- 0-OFF(Default); 1-ON(Blank As Not Filled Data)

)

AS

BEGIN

BEGIN TRY

SET NOCOUNT ON;

SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;

--Parameter Sniffing

DECLARE @TableName NVARCHAR(128),

@Include_BlankAsNotFilled BIT,

@ColumnName NVARCHAR(128),

@R_NO INT,

@DataType_Field BIT,

@i INT, --Iteration

@RESULT NVARCHAR(MAX);

SELECT @TableName = @p_TableName,

@Include_BlankAsNotFilled = @p_Include_BlankAsNotFilled,

@i = 1;

--To Support some of the table formats that user typing.

SELECT @TableName =REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(@TableName,'[',''),']',''),'dbo.',''),'...',''),'..',''),'.','');

--validation

IF NOT EXISTS(SELECT 1 FROM SYS.OBJECTS WHERE [TYPE]='U' AND [NAME]=@TableName )

BEGIN

SELECT Result = 1 , Reason ='Table not exists in this Database' ;

RETURN 1;

END;

--dropping temp table if exists - for debugging purpose

IF OBJECT_ID('TempDb..#Temp') IS NOT NULL

DROP TABLE #Temp;

IF OBJECT_ID('TempDb..#Columns') IS NOT NULL

DROP TABLE #Columns;

--temp table creations

CREATE TABLE #Temp

(

[R_NO] INT NOT NULL,

[ColumnName] NVARCHAR(128) NOT NULL,

[FillRate] DECIMAL(5,2) NOT NULL

PRIMARY KEY CLUSTERED (ColumnName)

);

CREATE TABLE #Columns

(

[R_NO] INT NOT NULL,

[Name] [sysname] NOT NULL,

[DataType_Field] BIT NOT NULL

PRIMARY KEY CLUSTERED ([Name])

);

INSERT INTO #Columns ([R_NO],[Name],[DataType_Field])

SELECT

COLUMN_ID,

[Name],

IIF(collation_name IS NULL,0,1)

FROM SYS.COLUMNS WHERE OBJECT_ID = OBJECT_ID(@TableName);

WHILE @i <= ( SELECT MAX(R_NO) FROM #Columns) --Checking of Iteration till total number of columns

BEGIN

SELECT @DataType_Field=DataType_Field,@ColumnName=[Name],@R_NO=[R_NO] FROM #Columns WHERE R_NO = @i;

SET @RESULT =

'INSERT INTO #Temp ([R_NO],[ColumnName], [FillRate]) ' +

'SELECT ' + QUOTENAME(@R_NO,CHAR(39)) + ',

''' + @ColumnName + ''',

CAST((100*(SUM(

CASE WHEN ' +

CASE

WHEN @Include_BlankAsNotFilled = 0

THEN '[' + @ColumnName + '] IS NOT NULL'

WHEN @DataType_Field = 0

THEN '[' + @ColumnName + '] IS NOT NULL'

ELSE 'ISNULL([' + @ColumnName + '],'''')<>'''' ' END +

' THEN 1 ELSE 0 END)*1.0 / COUNT(*)))

AS DECIMAL(5,2))

FROM ' + @TableName;

--PRINT(@RESULT); --for debug purpose

EXEC(@RESULT);

SET @i += 1; -- Incrementing Iteration Count

END;

--Final Result Set

SELECT

ColumnName AS [Column Name],

FillRate AS [Fill Rate (%)]

FROM #TEMP

ORDER BY [R_NO];

RETURN 0;

END TRY

BEGIN CATCH --error handling even it is fetching stored procedure

SELECT

ERROR_NUMBER() AS ErrorNumber

,ERROR_SEVERITY() AS ErrorSeverity

,ERROR_STATE() AS ErrorState

,ERROR_PROCEDURE() AS ErrorProcedure

,ERROR_LINE() AS ErrorLine

,ERROR_MESSAGE() AS ErrorMessage;

RETURN 1;

END CATCH;

END;

Execute this stored procedure — Method 1 by passing the table name like below

Execute like below if we need to consider NULL values alone as not filled

EXEC [Get_FillRate] @p_TableName='#TestEmp',@p_Include_BlankAsNotFilled=0;

Execute like below if we need to consider both NULL values and empty/blank values as not filled

EXEC [Get_FillRate] @p_TableName='#TestEmp',@p_Include_BlankAsNotFilled=1;

Method 1 -Output

T-SQL: Finding Fill Rate of Table (5)

CREATE OR ALTER PROCEDURE [dbo].[Get_FillRate]

(

@p_TableName NVARCHAR(128),

@p_Include_BlankAsNotFilled BIT = 0 -- 0-OFF(Default); 1-ON(Blank As Not Filled Data)

)

AS

BEGIN

BEGIN TRY

SET NOCOUNT ON;

SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;

--Parameter Sniffing

DECLARE @TableName NVARCHAR(128),

@Include_BlankAsNotFilled BIT,

@RESULT NVARCHAR(MAX);

SELECT @TableName = @p_TableName,

@Include_BlankAsNotFilled = @p_Include_BlankAsNotFilled,

@RESULT = '';

--To Support some of the table formats that user typing.

SELECT @TableName =REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(@TableName,'[',''),']',''),'dbo.',''),'...',''),'..',''),'.','');

--validation

IF NOT EXISTS(SELECT 1 FROM SYS.OBJECTS WHERE [TYPE]='U' AND [NAME]=@TableName )

BEGIN

SELECT Result = 1 , Reason ='Table not exists in this Database' ;

RETURN 1;

END;

--dropping temp table if exists - for debugging purpose

IF OBJECT_ID('TempDb..#Columns') IS NOT NULL

DROP TABLE #Columns;

--temp table creations

CREATE TABLE #Columns

(

[ORDINAL_POSITION] INT NOT NULL,

[COLUMN_NAME] [sysname] NOT NULL,

[DataType_Field] BIT NOT NULL,

[TABLE_NAME] [sysname] NOT NULL

PRIMARY KEY CLUSTERED ([ORDINAL_POSITION],[COLUMN_NAME])

);

INSERT INTO #Columns ([ORDINAL_POSITION],[COLUMN_NAME],[DataType_Field],[TABLE_NAME])

SELECT

[ORDINAL_POSITION],

[COLUMN_NAME],

CASE WHEN COLLATION_NAME IS NOT NULL THEN 1 ELSE 0 END,

[TABLE_NAME]

FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME =@tablename; --Using System_View

--Final Result Set

SELECT @RESULT = @RESULT+ N'SELECT '''+C.COLUMN_NAME+''' AS [Column Name],

CAST((100*(SUM(

CASE WHEN ' +

CASE

WHEN @include_blankasnotfilled = 0

THEN '[' + C.COLUMN_NAME + '] IS NOT NULL'

WHEN C.[DataType_Field]=0

THEN '[' + C.COLUMN_NAME + '] IS NOT NULL'

ELSE 'ISNULL([' + C.COLUMN_NAME + '],'''')<>'''' ' END +

' THEN 1 ELSE 0 END)*1.0 / COUNT(*)))

AS DECIMAL(5,2)) AS [Fill Rate (%)]

FROM '+C.TABLE_NAME+' UNION ALL '

FROM #Columns C;

SET @RESULT=LEFT(@RESULT,LEN(@RESULT)-10); --To Omit 'Last UNION ALL '.

--PRINT(@RESULT); --for debug purpose

EXEC(@RESULT);

RETURN 0;

END TRY

BEGIN CATCH --error handling even it is fetching stored procedure

SELECT

ERROR_NUMBER() AS ErrorNumber

,ERROR_SEVERITY() AS ErrorSeverity

,ERROR_STATE() AS ErrorState

,ERROR_PROCEDURE() AS ErrorProcedure

,ERROR_LINE() AS ErrorLine

,ERROR_MESSAGE() AS ErrorMessage;

RETURN 1;

END CATCH;

END;

Execute this stored procedure — Method 2 by passing the table name like below

Execute like below if we need to consider NULL values alone as not filled

EXEC [Get_FillRate] @p_TableName='#TestEmp',@p_Include_BlankAsNotFilled=0;

Execute like below if we need to consider both NULL values and empty/blank values as not filled

EXEC [Get_FillRate] @p_TableName='#TestEmp',@p_Include_BlankAsNotFilled=1;

Method 2 -Output

T-SQL: Finding Fill Rate of Table (6)

The below four metrics taken for consideration for knowing the difference between Method 1 Vs Method 2

  • No. of Query Sets in Exec Query Plan
  • Total CPU Time (in ms)
  • Total Elapsed Time (in ms)
  • Total Logical Reads
T-SQL: Finding Fill Rate of Table (7)

In conclusion, we have seen how to find the Fill Rate of a Table Using T-SQL Queries that is applicable to run in both AZURE and On-Premises SQL Databases. Thus, it would helps us to take business decisions effectively as well as immediately.

As a future scope, We can do some more enhancements (some of the ideas mentioned below) with this and can do many more things like supporting Fill Rates for

  • Required column list of a table in a database (using exclude/include option and column_list as input parameter)
  • Required column list in a database (by introducing separate input parameter for column name and supporting null in table name)
  • Required list of tables in a database (by changing the data length in input parameter)
  • All tables in a database (by removing the table name filter)
T-SQL : Finding Fill Rate of Table (2024)

FAQs

T-SQL : Finding Fill Rate of Table? ›

The Fill Rate is defined as the number of filled entries at each Column level that is divided by the total number of rows in a table. Depends on the total number of rows that is present in a table, we can figure out the Fill Rate easily.

How to find fill rate in SQL? ›

The sum of the filled entries at each column level divided by the total number of rows in the table. Depends on the total number of rows of a table,we can figure out the Fill Rate easily.

How to check fill factor in SQL Server? ›

Using SQL Server Management Studio
  1. In Object Explorer, right-click a server and select Properties.
  2. Click the Database Settings node.
  3. In the Default index fill factor box, type or select the index fill factor that you want.
Nov 18, 2022

How do I find out how much space a SQL table is using? ›

SQL Server provides the system-stored procedure sp_spaceused that shows the reserved, used, and unused space of a specified table. exec sp_MSForEachTable 'exec sp_spaceused [?]

How do you get the amount of entries in a table in SQL? ›

In SQL, you can make a database query and use the COUNT function to get the number of rows for a particular group in the table. Here is the basic syntax: SELECT COUNT(column_name) FROM table_name; COUNT(column_name) will not include NULL values as part of the count.

How do you calculate the fill rate? ›

Using the fill rate formula is easy. First, take the number of orders completely fulfilled and divide it by the total number of orders received. Second, multiply that number by 100. The resulting number is your fill rate percentage.

How do I find the fill factor of a table in SQL Server? ›

For example, if we specify 'FILLFACTOR = 90', then 90% of page space will be filled with data and 10% of page space will be left unfilled. “FILLFACTOR = 0 or FILLFACTOR = 100” can be used interchangeably to tell SQL Server to fill the page completely.

What is the default fill factor in TSQL? ›

The fill-factor value is a percentage from 1 to 100, and the server-wide default is 0 which means that the leaf-level pages are filled to capacity. Fill-factor values 0 and 100 are the same in all respects.

What is the fill factor in SQL table? ›

Fill factor determines how much space within each page is initially reserved for future updates. Postgres uses a default fill factor of 100 for tables. This means that by default, Postgres will try to pack each page as full as possible when initially storing data, without leaving any space for future updates.

What is the best fill factor in SQL Server? ›

Regular updated Tables – Set Fill Factor between 70 and 90.

If you notice the recurring issue again with page split, it gives room for fragmentation, so I recommend setting the fill factor to 70 with an interval of 5 at a time. Fill factor has to keep the balance between reads/writes.

How to check table space in SQL? ›

To do this, follow the simple steps below!
  1. Connect to Oracle. Use SQL*Plus or any other tool that connects to your Oracle database.
  2. Identify tablespace. Use this query to view available tablespaces: SELECT tablespace_name FROM dba_tablespaces;
  3. Check status. ...
  4. Verify size. ...
  5. Monitor usage. ...
  6. Check temp tablespace.

How to check if data has space in SQL? ›

Whitespaces are detected in SQL by using a TRIM function.

How to find table growth rate in SQL Server? ›

Connect to a SQL instance and right-click on a database for which we want to get details of Auto Growth and Shrink Events. It opens the disk usage report of the specified database. In this disk usage report, we get the details of the data file and log file space usage.

How many entries are in a SQL table? ›

SQL Server does not limit the number of rows in a table (different from columns), nor does it have a specific maximum number of rows.

How do you get the amount of records in SQL? ›

SQL Server COUNT() Function

The COUNT() function returns the number of records returned by a select query. Note: NULL values are not counted.

How do you count entries in a table? ›

What to Know
  1. Calculate number of records in a table: Type SELECT COUNT(*) [Enter] FROM table name;
  2. Identify number of unique values in a column: Type SELECT COUNT(DISTINCT column name) [Enter] FROM table name;
Mar 5, 2021

How to calculate win rate in SQL? ›

How to Calculate Your Win Rate
  1. SQLs to Opps won. Win rate % = (Total amount of Opps won / SQL) * 100. ...
  2. Opps won to opps closed. Win rate % = Opps won / (Opps won + Opps lost) * 100. ...
  3. Closed $ / $ in pipeline. Win rate % = (Total amount of $ closed / Total amount of $ in pipeline) * 100.
Sep 23, 2019

How do you find the average rate in SQL? ›

The syntax for using the AVG function in SQL is: SELECT AVG(column_name) FROM table_name; For example, suppose you have a table named bookshop with a price column and you need to answer what is the average price of the books.

How to calculate MRR in SQL? ›

To calculate your MRR, you simply take your total users and multiply that number by your ARPU. For example, if you have 500 active users and your ARPU is $20, you're MRR would be $10,000. If you base your business model on contracts, you can divide your ARR by 12 to calculate your MRR.

Top Articles
Homemade Ginger Syrup & Ginger co*cktail Recipes
25 Vintage Casserole Recipes from the '50s That We Still Love Today
Television Archive News Search Service
Tabc On The Fly Final Exam Answers
Frank Lloyd Wright, born 150 years ago, still fascinates
Wisconsin Women's Volleyball Team Leaked Pictures
1movierulzhd.fun Reviews | scam, legit or safe check | Scamadviser
Naturalization Ceremonies Can I Pick Up Citizenship Certificate Before Ceremony
Directions To Lubbock
Wnem Radar
Craigslist Pets Sac
Hijab Hookup Trendy
Craftology East Peoria Il
Find Such That The Following Matrix Is Singular.
Inside the life of 17-year-old Charli D'Amelio, the most popular TikTok star in the world who now has her own TV show and clothing line
Fraction Button On Ti-84 Plus Ce
G Switch Unblocked Tyrone
Account Suspended
Amazing deals for Abercrombie & Fitch Co. on Goodshop!
Encore Atlanta Cheer Competition
Optum Urgent Care - Nutley Photos
Happy Homebodies Breakup
Www.craigslist.com Austin Tx
Craigs List Jonesboro Ar
Craigslist Rentals Coquille Oregon
Cor Triatriatum: Background, Pathophysiology, Epidemiology
How To Improve Your Pilates C-Curve
Mobile crane from the Netherlands, used mobile crane for sale from the Netherlands
Craigslist Middletown Ohio
Grove City Craigslist Pets
Bursar.okstate.edu
Manuel Pihakis Obituary
Japanese Pokémon Cards vs English Pokémon Cards
Netherforged Lavaproof Boots
Directions To 401 East Chestnut Street Louisville Kentucky
Best Restaurants In Blacksburg
Duff Tuff
World History Kazwire
Improving curriculum alignment and achieving learning goals by making the curriculum visible | Semantic Scholar
White County
Ohio Road Construction Map
Mother Cabrini, the First American Saint of the Catholic Church
Value Village Silver Spring Photos
Unblocked Games 6X Snow Rider
Sapphire Pine Grove
Race Deepwoken
Haunted Mansion Showtimes Near Millstone 14
Shannon Sharpe Pointing Gif
Craigslist Psl
Pulpo Yonke Houston Tx
Land of Samurai: One Piece’s Wano Kuni Arc Explained
7 National Titles Forum
Latest Posts
Article information

Author: Lidia Grady

Last Updated:

Views: 5776

Rating: 4.4 / 5 (45 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Lidia Grady

Birthday: 1992-01-22

Address: Suite 493 356 Dale Fall, New Wanda, RI 52485

Phone: +29914464387516

Job: Customer Engineer

Hobby: Cryptography, Writing, Dowsing, Stand-up comedy, Calligraphy, Web surfing, Ghost hunting

Introduction: My name is Lidia Grady, I am a thankful, fine, glamorous, lucky, lively, pleasant, shiny person who loves writing and wants to share my knowledge and understanding with you.