{"id":585,"date":"2014-10-09T19:43:22","date_gmt":"2014-10-09T13:43:22","guid":{"rendered":"http:\/\/dotnetsql.info\/net-sql\/?p=585"},"modified":"2014-10-17T13:50:03","modified_gmt":"2014-10-17T07:50:03","slug":"data-sizing-or-sql-server-rowcount-without-doing-a-table-scan","status":"publish","type":"post","link":"http:\/\/dotnetsql.info\/net-sql\/index.php\/data-sizing-or-sql-server-rowcount-without-doing-a-table-scan\/","title":{"rendered":"DATA sizing or SQL Server Rowcount without doing a table scan"},"content":{"rendered":"<h3>\n\t<strong>Scenario Proposal<\/strong><br \/>\n<\/h3>\n<p>\n\tWhile working on different database projects I usually find it very necessary to perform a data sizing exercise on all tables focusing mostly on the number of rows in order to determine a growth factor of each table. This kind of information is useful in performing long term planning of growth and scalability of the data that we are managing.\n<\/p>\n<p>\n\tOut of an exercise like this one we can also determine future needs for data archiving.\n<\/p>\n<p>\n\t<!--more-->\n<\/p>\n<h3>\n\t<strong>Solution<\/strong><br \/>\n<\/h3>\n<p>\n\tThe most trivial way of achieving this task would be perform an &ldquo;<strong>Select Count(1) from&rdquo; <\/strong>each of the tables of our database but this could create a lot of overhead especially for large databases and large tables.\n<\/p>\n<p>\n\tThe optimal way to achieve this is by using the sp_spaceused system stored procedure.\n<\/p>\n<h3>\n\t<strong>Sample code below to outline the usage:<\/strong><br \/>\n<\/h3>\n<p>\n\t&#8212; variable declaration&nbsp; &#8212; www.dotnetsql.info\n<\/p>\n<p>\n\t&#8212; declare @n to store the total number of tables\n<\/p>\n<p>\n\tdeclare @n int\n<\/p>\n<p>\n\t&#8212; declare @i to use it as an index\n<\/p>\n<p>\n\tdeclare @i int\n<\/p>\n<p>\n\t&#8212; declare @tn to retrieve the tablename on the @i position\n<\/p>\n<p>\n\tdeclare @tn varchar(1000)\n<\/p>\n<p>\n\tset&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; @i = 1\n<\/p>\n<p>\n\t&#8212; table to store all the tablenames in scope\n<\/p>\n<p>\n\tdeclare @t table(\n<\/p>\n<p>\n\tid int identity(1,1),\n<\/p>\n<p>\n\tname varchar(1000)\n<\/p>\n<p>\n\t)\n<\/p>\n<p>\n\t&#8212; table to store the return results\n<\/p>\n<p>\n\tdeclare @tempt table\n<\/p>\n<p>\n\t(&nbsp;&nbsp;\n<\/p>\n<p>\n\ttableName varchar(100),\n<\/p>\n<p>\n\tnumberofRows varchar(100),\n<\/p>\n<p>\n\treservedSize varchar(50),\n<\/p>\n<p>\n\tdataSize varchar(50),\n<\/p>\n<p>\n\tindexSize varchar(50),\n<\/p>\n<p>\n\tunusedSize varchar(50)\n<\/p>\n<p>\n\t)\n<\/p>\n<p>\n\t&#8212; code&nbsp; &#8212;- www.dotnetsql.info\n<\/p>\n<p>\n\t&#8212; insert all the tables in the database in a temporary table\n<\/p>\n<p>\n\tinsert into @t\n<\/p>\n<p>\n\t&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (name)\n<\/p>\n<p>\n\tselect&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; s.name+&#39;.&#39;+t.name\n<\/p>\n<p>\n\tfrom&nbsp; sys.tables&nbsp; t\n<\/p>\n<p>\n\tjoin&nbsp; sys.schemas s&nbsp;&nbsp;&nbsp;&nbsp; on s.schema_id = t.schema_id\n<\/p>\n<p>\n\t&#8212; get the rowcount of the temporary table\n<\/p>\n<p>\n\tset @n = @@ROWCOUNT+1\n<\/p>\n<p>\n\t&#8212; loop through the table to retrieve table names\n<\/p>\n<p>\n\twhile (@i&lt;@n)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; begin\n<\/p>\n<p>\n\t&#8212; get the current table name\n<\/p>\n<p>\n\t&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; select @tn = name\n<\/p>\n<p>\n\t&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; from&nbsp; @t\n<\/p>\n<p>\n\t&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; where id = @i\n<\/p>\n<p>\n\t&#8212; insert the result of sp_spaceused into the results table\n<\/p>\n<p>\n\t&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; insert into @tempt\n<\/p>\n<p>\n\t&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; EXEC sp_spaceused @tn\n<\/p>\n<p>\n\t&#8212; increment the current row\n<\/p>\n<p>\n\t&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; set @i=@i+1\n<\/p>\n<p>\n\tend\n<\/p>\n<p>\n\t&#8212; return the rowcount and data usage\n<\/p>\n<p>\n\tselect *\n<\/p>\n<p>\n\tfrom @tempt\n<\/p>\n<p>\n\t<strong>Thanks for reading this article,<\/strong>\n<\/p>\n<p>\n\t<strong>Next steps :<\/strong>\n<\/p>\n<ol>\n<li>\n<p>\n\t\t\t<strong>Add this script to your database toolkit<\/strong>\n\t\t<\/p>\n<\/li>\n<li>\n<p>\n\t\t\t<strong>Share this with your colleagues because Sharing is Learning<\/strong>\n\t\t<\/p>\n<\/li>\n<li>\n<p>\n\t\t\t<strong>Comment below if you need any assistance<\/strong>\n\t\t<\/p>\n<\/li>\n<\/ol>\n<p style=\"text-align: right;\">\n\t<em><strong>Powered by <a href=\"http:\/\/www.codereview.co\">CodeReview &#8211; Let&#39;s make it Better!<\/a><\/strong><\/em><\/p>\n<div class=\"sharedaddy sd-sharing-enabled\"><div class=\"robots-nocontent sd-block sd-social sd-social-icon sd-sharing\"><h3 class=\"sd-title\">Share this:<\/h3><div class=\"sd-content\"><ul><li class=\"share-twitter\"><a rel=\"nofollow\" data-shared=\"sharing-twitter-585\" class=\"share-twitter sd-button share-icon no-text\" href=\"http:\/\/dotnetsql.info\/net-sql\/index.php\/data-sizing-or-sql-server-rowcount-without-doing-a-table-scan\/?share=twitter\" target=\"_blank\" title=\"Click to share on Twitter\"><span><\/span><span class=\"sharing-screen-reader-text\">Click to share on Twitter (Opens in new window)<\/span><\/a><\/li><li class=\"share-linkedin\"><a rel=\"nofollow\" data-shared=\"sharing-linkedin-585\" class=\"share-linkedin sd-button share-icon no-text\" href=\"http:\/\/dotnetsql.info\/net-sql\/index.php\/data-sizing-or-sql-server-rowcount-without-doing-a-table-scan\/?share=linkedin\" target=\"_blank\" title=\"Click to share on LinkedIn\"><span><\/span><span class=\"sharing-screen-reader-text\">Click to share on LinkedIn (Opens in new window)<\/span><\/a><\/li><li class=\"share-facebook\"><a rel=\"nofollow\" data-shared=\"sharing-facebook-585\" class=\"share-facebook sd-button share-icon no-text\" href=\"http:\/\/dotnetsql.info\/net-sql\/index.php\/data-sizing-or-sql-server-rowcount-without-doing-a-table-scan\/?share=facebook\" target=\"_blank\" title=\"Share on Facebook\"><span><\/span><span class=\"sharing-screen-reader-text\">Share on Facebook (Opens in new window)<\/span><\/a><\/li><li class=\"share-google-plus-1\"><a rel=\"nofollow\" data-shared=\"sharing-google-585\" class=\"share-google-plus-1 sd-button share-icon no-text\" href=\"http:\/\/dotnetsql.info\/net-sql\/index.php\/data-sizing-or-sql-server-rowcount-without-doing-a-table-scan\/?share=google-plus-1\" target=\"_blank\" title=\"Click to share on Google+\"><span><\/span><span class=\"sharing-screen-reader-text\">Click to share on Google+ (Opens in new window)<\/span><\/a><\/li><li class=\"share-pocket\"><a rel=\"nofollow\" data-shared=\"\" class=\"share-pocket sd-button share-icon no-text\" href=\"http:\/\/dotnetsql.info\/net-sql\/index.php\/data-sizing-or-sql-server-rowcount-without-doing-a-table-scan\/?share=pocket\" target=\"_blank\" title=\"Click to share on Pocket\"><span><\/span><span class=\"sharing-screen-reader-text\">Click to share on Pocket (Opens in new window)<\/span><\/a><\/li><li class=\"share-email\"><a rel=\"nofollow\" data-shared=\"\" class=\"share-email sd-button share-icon no-text\" href=\"http:\/\/dotnetsql.info\/net-sql\/index.php\/data-sizing-or-sql-server-rowcount-without-doing-a-table-scan\/?share=email\" target=\"_blank\" title=\"Click to email this to a friend\"><span><\/span><span class=\"sharing-screen-reader-text\">Click to email this to a friend (Opens in new window)<\/span><\/a><\/li><li class=\"share-end\"><\/li><\/ul><\/div><\/div><\/div><div class='sharedaddy sd-block sd-like jetpack-likes-widget-wrapper jetpack-likes-widget-unloaded' id='like-post-wrapper-76243027-585-69d82277e4d8f' data-src='\/\/widgets.wp.com\/likes\/#blog_id=76243027&amp;post_id=585&amp;origin=dotnetsql.info&amp;obj_id=76243027-585-69d82277e4d8f' data-name='like-post-frame-76243027-585-69d82277e4d8f'><h3 class='sd-title'>Like this:<\/h3><div class='likes-widget-placeholder post-likes-widget-placeholder' style='height:55px'><span class='button'><span>Like<\/span><\/span> <span class=\"loading\">Loading...<\/span><\/div><span class='sd-text-color'><\/span><a class='sd-link-color'><\/a><\/div>","protected":false},"excerpt":{"rendered":"<p>Scenario Proposal While working on different database projects I usually find it very necessary to perform a data sizing exercise on all tables focusing mostly on the number of rows in order to determine a growth factor of each table. This kind of information is useful in performing long term planning of growth and scalability &hellip; <a href=\"http:\/\/dotnetsql.info\/net-sql\/index.php\/data-sizing-or-sql-server-rowcount-without-doing-a-table-scan\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">DATA sizing or SQL Server Rowcount without doing a table scan<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n<div class=\"sharedaddy sd-sharing-enabled\"><div class=\"robots-nocontent sd-block sd-social sd-social-icon sd-sharing\"><h3 class=\"sd-title\">Share this:<\/h3><div class=\"sd-content\"><ul><li class=\"share-twitter\"><a rel=\"nofollow\" data-shared=\"sharing-twitter-585\" class=\"share-twitter sd-button share-icon no-text\" href=\"http:\/\/dotnetsql.info\/net-sql\/index.php\/data-sizing-or-sql-server-rowcount-without-doing-a-table-scan\/?share=twitter\" target=\"_blank\" title=\"Click to share on Twitter\"><span><\/span><span class=\"sharing-screen-reader-text\">Click to share on Twitter (Opens in new window)<\/span><\/a><\/li><li class=\"share-linkedin\"><a rel=\"nofollow\" data-shared=\"sharing-linkedin-585\" class=\"share-linkedin sd-button share-icon no-text\" href=\"http:\/\/dotnetsql.info\/net-sql\/index.php\/data-sizing-or-sql-server-rowcount-without-doing-a-table-scan\/?share=linkedin\" target=\"_blank\" title=\"Click to share on LinkedIn\"><span><\/span><span class=\"sharing-screen-reader-text\">Click to share on LinkedIn (Opens in new window)<\/span><\/a><\/li><li class=\"share-facebook\"><a rel=\"nofollow\" data-shared=\"sharing-facebook-585\" class=\"share-facebook sd-button share-icon no-text\" href=\"http:\/\/dotnetsql.info\/net-sql\/index.php\/data-sizing-or-sql-server-rowcount-without-doing-a-table-scan\/?share=facebook\" target=\"_blank\" title=\"Share on Facebook\"><span><\/span><span class=\"sharing-screen-reader-text\">Share on Facebook (Opens in new window)<\/span><\/a><\/li><li class=\"share-google-plus-1\"><a rel=\"nofollow\" data-shared=\"sharing-google-585\" class=\"share-google-plus-1 sd-button share-icon no-text\" href=\"http:\/\/dotnetsql.info\/net-sql\/index.php\/data-sizing-or-sql-server-rowcount-without-doing-a-table-scan\/?share=google-plus-1\" target=\"_blank\" title=\"Click to share on Google+\"><span><\/span><span class=\"sharing-screen-reader-text\">Click to share on Google+ (Opens in new window)<\/span><\/a><\/li><li class=\"share-pocket\"><a rel=\"nofollow\" data-shared=\"\" class=\"share-pocket sd-button share-icon no-text\" href=\"http:\/\/dotnetsql.info\/net-sql\/index.php\/data-sizing-or-sql-server-rowcount-without-doing-a-table-scan\/?share=pocket\" target=\"_blank\" title=\"Click to share on Pocket\"><span><\/span><span class=\"sharing-screen-reader-text\">Click to share on Pocket (Opens in new window)<\/span><\/a><\/li><li class=\"share-email\"><a rel=\"nofollow\" data-shared=\"\" class=\"share-email sd-button share-icon no-text\" href=\"http:\/\/dotnetsql.info\/net-sql\/index.php\/data-sizing-or-sql-server-rowcount-without-doing-a-table-scan\/?share=email\" target=\"_blank\" title=\"Click to email this to a friend\"><span><\/span><span class=\"sharing-screen-reader-text\">Click to email this to a friend (Opens in new window)<\/span><\/a><\/li><li class=\"share-end\"><\/li><\/ul><\/div><\/div><\/div><div class='sharedaddy sd-block sd-like jetpack-likes-widget-wrapper jetpack-likes-widget-unloaded' id='like-post-wrapper-76243027-585-69d82277e5277' data-src='\/\/widgets.wp.com\/likes\/#blog_id=76243027&amp;post_id=585&amp;origin=dotnetsql.info&amp;obj_id=76243027-585-69d82277e5277' data-name='like-post-frame-76243027-585-69d82277e5277'><h3 class='sd-title'>Like this:<\/h3><div class='likes-widget-placeholder post-likes-widget-placeholder' style='height:55px'><span class='button'><span>Like<\/span><\/span> <span class=\"loading\">Loading...<\/span><\/div><span class='sd-text-color'><\/span><a class='sd-link-color'><\/a><\/div>","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[81,5,6,54,82,25],"tags":[85,84,92,93,91],"aioseo_notices":[],"_links":{"self":[{"href":"http:\/\/dotnetsql.info\/net-sql\/index.php\/wp-json\/wp\/v2\/posts\/585"}],"collection":[{"href":"http:\/\/dotnetsql.info\/net-sql\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/dotnetsql.info\/net-sql\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/dotnetsql.info\/net-sql\/index.php\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"http:\/\/dotnetsql.info\/net-sql\/index.php\/wp-json\/wp\/v2\/comments?post=585"}],"version-history":[{"count":7,"href":"http:\/\/dotnetsql.info\/net-sql\/index.php\/wp-json\/wp\/v2\/posts\/585\/revisions"}],"predecessor-version":[{"id":593,"href":"http:\/\/dotnetsql.info\/net-sql\/index.php\/wp-json\/wp\/v2\/posts\/585\/revisions\/593"}],"wp:attachment":[{"href":"http:\/\/dotnetsql.info\/net-sql\/index.php\/wp-json\/wp\/v2\/media?parent=585"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/dotnetsql.info\/net-sql\/index.php\/wp-json\/wp\/v2\/categories?post=585"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/dotnetsql.info\/net-sql\/index.php\/wp-json\/wp\/v2\/tags?post=585"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}