finding large tables in sql server Archives - Anuj Varma, Hands-On Technology Architect, Clean Air Activist https://www.anujvarma.com/tag/finding-large-tables-in-sql-server/ Production Grade Technical Solutions | Data Encryption and Public Cloud Expert Thu, 21 Nov 2013 16:57:08 +0000 en-US hourly 1 https://wordpress.org/?v=6.9.4 https://www.anujvarma.com/wp-content/uploads/anujtech.png finding large tables in sql server Archives - Anuj Varma, Hands-On Technology Architect, Clean Air Activist https://www.anujvarma.com/tag/finding-large-tables-in-sql-server/ 32 32 Find Largest Tables in your SQL Server database https://www.anujvarma.com/find-largest-tables-in-your-sql-server-database/ https://www.anujvarma.com/find-largest-tables-in-your-sql-server-database/#comments Thu, 21 Nov 2013 16:57:08 +0000 http://www.anujvarma.com/?p=1840 Introduction This script uses a temp table to store the results of the query. It sorts the results in descending order – showing the largest tables on top. CREATE TABLE […]

The post Find Largest Tables in your SQL Server database appeared first on Anuj Varma, Hands-On Technology Architect, Clean Air Activist.

]]>
Introduction

This script uses a temp table to store the results of the query. It sorts the results in descending order – showing the largest tables on top.

CREATE TABLE #FindLargeTables 

(

  table_name    sysname ,

  row_count     INT,

  reserved_size VARCHAR(50),

  data_size     VARCHAR(50),

  index_size    VARCHAR(50),

  unused_size   VARCHAR(50)

)

 

SET NOCOUNT ON

 

INSERT #FindLargeTables

 

EXEC sp_msforeachtable 'sp_spaceused ''?'''

 

SELECT 

  a.table_name,

  a.row_count,

  COUNT(*) AS col_count,

  a.data_size

  FROM #FindLargeTables a

     INNER JOIN information_schema.columns b

     ON a.table_name collate database_default

     = b.table_name collate database_default

       GROUP BY a.table_name, a.row_count, a.data_size

       ORDER BY CAST(REPLACE(a.data_size, ' KB', '') AS integer) DESC

 

DROP TABLE #FindLargeTables

The post Find Largest Tables in your SQL Server database appeared first on Anuj Varma, Hands-On Technology Architect, Clean Air Activist.

]]>
https://www.anujvarma.com/find-largest-tables-in-your-sql-server-database/feed/ 1