{"id":291,"date":"2012-05-10T13:48:30","date_gmt":"2012-05-10T07:48:30","guid":{"rendered":"http:\/\/dotnetsql.info\/net-sql\/?p=291"},"modified":"2014-10-20T19:44:11","modified_gmt":"2014-10-20T13:44:11","slug":"how-to-perfom-fast-record-count-in-sql-server-t-sql","status":"publish","type":"post","link":"http:\/\/dotnetsql.info\/net-sql\/index.php\/how-to-perfom-fast-record-count-in-sql-server-t-sql\/","title":{"rendered":"How to perfom fast record count in Sql Server T-Sql"},"content":{"rendered":"<p>\n\tScenario :&nbsp; You have a table that has a lot of rows and you want to know the total number of rows.<!--more-->\n<\/p>\n<p>\n\tSolution 1 :&nbsp; The classic solution is <span style=\"color: rgb(51, 102, 255);\">SELECT COUNT(*) FROM TableName<\/span> but this can take a while.\n<\/p>\n<p>\n\tSolution 2 : The faster way to get the record count would be one of the choises below :\n<\/p>\n<ol>\n<li>\n\t\t<span style=\"color: rgb(51, 102, 255);\">SELECT SUM(1) FROM TableName<\/span>\n\t<\/li>\n<li>\n\t\t<span style=\"color: rgb(51, 102, 255);\">SELECT COUNT(1) FROM TableName<\/span>\n\t<\/li>\n<li>\n\t\t<span style=\"color: rgb(51, 102, 255);\">SELECT ROWS FROM SYSINDEXES WHERE OBJECT_NAME(ID)&nbsp; = &#39;TableName&#39; and indid&lt;2<\/span>\n\t<\/li>\n<li>\n\t\t<span style=\"color: rgb(51, 102, 255);\">EXEC SP_SPACEUSED &#39;TableName&#39;<\/span>\n\t<\/li>\n<li>\n\t\t<span style=\"color: rgb(51, 102, 255);\">DBCC&nbsp; CHECKTABLE(&#39;&#39;TableName)<\/span>\n\t<\/li>\n<\/ol>\n<p>\n\t<span style=\"color: rgb(136, 136, 136);\">Lucian Leonte<\/span>\n<\/p>\n<p>\n\t<span style=\"color: rgb(136, 136, 136);\">MCT, MCITP, MCTS<\/span>\n<\/p>\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-291\" class=\"share-twitter sd-button share-icon no-text\" href=\"http:\/\/dotnetsql.info\/net-sql\/index.php\/how-to-perfom-fast-record-count-in-sql-server-t-sql\/?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-291\" class=\"share-linkedin sd-button share-icon no-text\" href=\"http:\/\/dotnetsql.info\/net-sql\/index.php\/how-to-perfom-fast-record-count-in-sql-server-t-sql\/?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-291\" class=\"share-facebook sd-button share-icon no-text\" href=\"http:\/\/dotnetsql.info\/net-sql\/index.php\/how-to-perfom-fast-record-count-in-sql-server-t-sql\/?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-291\" class=\"share-google-plus-1 sd-button share-icon no-text\" href=\"http:\/\/dotnetsql.info\/net-sql\/index.php\/how-to-perfom-fast-record-count-in-sql-server-t-sql\/?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\/how-to-perfom-fast-record-count-in-sql-server-t-sql\/?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\/how-to-perfom-fast-record-count-in-sql-server-t-sql\/?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-291-69e041eb8760a' data-src='\/\/widgets.wp.com\/likes\/#blog_id=76243027&amp;post_id=291&amp;origin=dotnetsql.info&amp;obj_id=76243027-291-69e041eb8760a' data-name='like-post-frame-76243027-291-69e041eb8760a'><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 :&nbsp; You have a table that has a lot of rows and you want to know the total number of rows.<\/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-291\" class=\"share-twitter sd-button share-icon no-text\" href=\"http:\/\/dotnetsql.info\/net-sql\/index.php\/how-to-perfom-fast-record-count-in-sql-server-t-sql\/?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-291\" class=\"share-linkedin sd-button share-icon no-text\" href=\"http:\/\/dotnetsql.info\/net-sql\/index.php\/how-to-perfom-fast-record-count-in-sql-server-t-sql\/?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-291\" class=\"share-facebook sd-button share-icon no-text\" href=\"http:\/\/dotnetsql.info\/net-sql\/index.php\/how-to-perfom-fast-record-count-in-sql-server-t-sql\/?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-291\" class=\"share-google-plus-1 sd-button share-icon no-text\" href=\"http:\/\/dotnetsql.info\/net-sql\/index.php\/how-to-perfom-fast-record-count-in-sql-server-t-sql\/?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\/how-to-perfom-fast-record-count-in-sql-server-t-sql\/?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\/how-to-perfom-fast-record-count-in-sql-server-t-sql\/?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-291-69e041eb87a7d' data-src='\/\/widgets.wp.com\/likes\/#blog_id=76243027&amp;post_id=291&amp;origin=dotnetsql.info&amp;obj_id=76243027-291-69e041eb87a7d' data-name='like-post-frame-76243027-291-69e041eb87a7d'><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":[5,6,25],"tags":[41,42,40,309],"aioseo_notices":[],"_links":{"self":[{"href":"http:\/\/dotnetsql.info\/net-sql\/index.php\/wp-json\/wp\/v2\/posts\/291"}],"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=291"}],"version-history":[{"count":9,"href":"http:\/\/dotnetsql.info\/net-sql\/index.php\/wp-json\/wp\/v2\/posts\/291\/revisions"}],"predecessor-version":[{"id":622,"href":"http:\/\/dotnetsql.info\/net-sql\/index.php\/wp-json\/wp\/v2\/posts\/291\/revisions\/622"}],"wp:attachment":[{"href":"http:\/\/dotnetsql.info\/net-sql\/index.php\/wp-json\/wp\/v2\/media?parent=291"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/dotnetsql.info\/net-sql\/index.php\/wp-json\/wp\/v2\/categories?post=291"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/dotnetsql.info\/net-sql\/index.php\/wp-json\/wp\/v2\/tags?post=291"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}