{"id":77,"date":"2012-09-16T10:31:00","date_gmt":"2012-09-16T15:31:00","guid":{"rendered":"https:\/\/www.chrystalsander.com\/blog\/?p=77"},"modified":"2023-07-26T12:38:06","modified_gmt":"2023-07-26T17:38:06","slug":"sql-server-get-rowcount-of-all-tables-without-the-overhead","status":"publish","type":"post","link":"https:\/\/www.chrystalsander.com\/blog\/?p=77","title":{"rendered":"SQL Server:  Get RowCount Of All Tables Without the Overhead"},"content":{"rendered":"<p>Avoid overhead with this completely awesome SQL Server query that returns the row-count of all tables without actually having to query each table. &nbsp;Instead, it just pulls from the statistics:<\/p>\n<p><\/p>\n<div style=\"background-color: white; font-family: arial, sans-serif; font-size: 13px;\">\n<span style=\"color: blue; font-family: 'Courier New'; font-size: 10pt;\">SELECT<\/span><span style=\"font-family: 'Courier New'; font-size: 10pt;\"><span style=\"color: #222222;\">&nbsp;<\/span><span style=\"color: red;\">&#8216;[&#8216;<\/span><span style=\"color: #222222;\">&nbsp;<\/span><span style=\"color: grey;\">+<\/span><span style=\"color: #222222;\">&nbsp;<\/span><span style=\"color: magenta;\">SCHEMA_NAME<\/span><span style=\"color: grey;\">(<\/span><span style=\"color: #222222;\">t<\/span><span style=\"color: grey;\">.<\/span><span style=\"color: magenta;\">schema_id<\/span><span style=\"color: grey;\">)<\/span><span style=\"color: grey;\">+<\/span><span style=\"color: #222222;\">&nbsp;<\/span><span style=\"color: red;\">&#8216;].[&#8216;<\/span><span style=\"color: #222222;\">&nbsp;<\/span><span style=\"color: grey;\">+<\/span><span style=\"color: #222222;\">&nbsp;t<\/span><span style=\"color: grey;\">.<\/span><span style=\"color: #222222;\">name&nbsp;<\/span><span style=\"color: grey;\">+<\/span><span style=\"color: #222222;\">&nbsp;<\/span><span style=\"color: red;\">&#8216;]&#8217;<\/span><span style=\"color: #222222;\">&nbsp;<\/span><span style=\"color: blue;\">AS <\/span>full_<span style=\"color: #222222;\">name<\/span><span style=\"color: grey;\">,<\/span><span style=\"color: #222222;\"><u><\/u><u><\/u><\/span><\/span><\/div>\n<div style=\"background-color: white; font-family: arial, sans-serif; font-size: 13px;\">\n<span style=\"color: magenta; font-family: 'Courier New'; font-size: 10pt;\">SCHEMA_NAME<\/span><span style=\"color: grey; font-family: 'Courier New'; font-size: 10pt;\">(<\/span><span style=\"font-family: 'Courier New'; font-size: 10pt;\"><span style=\"color: #222222;\">t<\/span><span style=\"color: grey;\">.<\/span><span style=\"color: magenta;\">schema_id<\/span><span style=\"color: grey;\">)<\/span><span style=\"color: #222222;\">&nbsp;<\/span><span style=\"color: blue;\">AS <\/span>schema_name<span style=\"color: grey;\">,<\/span><span style=\"color: #222222;\"><u><\/u><u><\/u><\/span><\/span><\/div>\n<div style=\"background-color: white; color: #222222; font-family: arial, sans-serif; font-size: 13px;\">\n<span style=\"font-family: 'Courier New'; font-size: 10pt;\">t<span style=\"color: grey;\">.<\/span>name&nbsp;<span style=\"color: blue;\">AS<\/span>&nbsp;table_name<span style=\"color: grey;\">,<\/span><u><\/u><u><\/u><\/span><\/div>\n<div style=\"background-color: white; color: #222222; font-family: arial, sans-serif; font-size: 13px;\">\n<span style=\"font-family: 'Courier New'; font-size: 10pt;\">i<span style=\"color: grey;\">.<\/span><span style=\"color: blue;\">rows<u><\/u><u><\/u><\/span><\/span><\/div>\n<div style=\"background-color: white; color: #222222; font-family: arial, sans-serif; font-size: 13px;\">\n<span style=\"color: blue; font-family: 'Courier New'; font-size: 10pt;\">FROM<\/span><span style=\"font-family: 'Courier New'; font-size: 10pt;\">&nbsp;<span style=\"color: green;\">sys<\/span><span style=\"color: grey;\">.<\/span><span style=\"color: green;\">tables<\/span>&nbsp;<span style=\"color: blue;\">AS<\/span>&nbsp;t&nbsp;<span style=\"color: grey;\">INNER<\/span>&nbsp;<span style=\"color: grey;\">JOIN<u><\/u><u><\/u><\/span><\/span><\/div>\n<div style=\"background-color: white; color: #222222; font-family: arial, sans-serif; font-size: 13px;\">\n<span style=\"color: green; font-family: 'Courier New'; font-size: 10pt;\">sys<\/span><span style=\"color: grey; font-family: 'Courier New'; font-size: 10pt;\">.<\/span><span style=\"color: green; font-family: 'Courier New'; font-size: 10pt;\">sysindexes<\/span><span style=\"font-family: 'Courier New'; font-size: 10pt;\">&nbsp;<span style=\"color: blue;\">AS<\/span>&nbsp;i&nbsp;<span style=\"color: blue;\">ON<\/span>&nbsp;t<span style=\"color: grey;\">.<\/span><span style=\"color: magenta;\">object_id<\/span>&nbsp;<span style=\"color: grey;\">=<\/span>i<span style=\"color: grey;\">.<\/span>id&nbsp;<span style=\"color: grey;\">AND<\/span>&nbsp;i<span style=\"color: grey;\">.<\/span>indid&nbsp;<span style=\"color: grey;\">&lt;<\/span>&nbsp;2<\/span><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Avoid overhead with this completely awesome SQL Server query that returns the row-count of all tables without actually having to query each table. &nbsp;Instead, it just pulls from the statistics: SELECT&nbsp;&#8216;[&#8216;&nbsp;+&nbsp;SCHEMA_NAME(t.schema_id)+&nbsp;&#8216;].[&#8216;&nbsp;+&nbsp;t.name&nbsp;+&nbsp;&#8216;]&#8217;&nbsp;AS full_name, SCHEMA_NAME(t.schema_id)&nbsp;AS schema_name, t.name&nbsp;AS&nbsp;table_name, i.rows FROM&nbsp;sys.tables&nbsp;AS&nbsp;t&nbsp;INNER&nbsp;JOIN sys.sysindexes&nbsp;AS&nbsp;i&nbsp;ON&nbsp;t.object_id&nbsp;=i.id&nbsp;AND&nbsp;i.indid&nbsp;&lt;&nbsp;2<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[50],"_links":{"self":[{"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/77"}],"collection":[{"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=77"}],"version-history":[{"count":1,"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/77\/revisions"}],"predecessor-version":[{"id":193,"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/77\/revisions\/193"}],"wp:attachment":[{"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=77"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=77"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.chrystalsander.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=77"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}