sql server table size check - sql-server

how to check size of tables of user defined database

Try this, sp_spaceused 'Your_tableName'

If you are using SQL Server Management Studio then you can check it by opening Object Explorer Details (press F7). Then click on Tables folder and find your table from list of tables. Once you click on your table you will find Data Space Used and Index Space Used in kilobytes.
If you want proper explanation about checking size you can refer
https://dba.stackexchange.com/questions/81245/how-to-determine-the-size-of-my-tables-in-the-sql-server-database/81253

Try this
SELECT
t.NAME AS TableName,
s.Name AS SchemaName,
p.rows AS RowCounts,
SUM(a.total_pages) * 8 AS TotalSpaceKB,
SUM(a.used_pages) * 8 AS UsedSpaceKB,
(SUM(a.total_pages) - SUM(a.used_pages)) * 8 AS UnusedSpaceKB
FROM
sys.tables t
INNER JOIN
sys.indexes i ON t.OBJECT_ID = i.object_id
INNER JOIN
sys.partitions p ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id
INNER JOIN
sys.allocation_units a ON p.partition_id = a.container_id
LEFT OUTER JOIN
sys.schemas s ON t.schema_id = s.schema_id
WHERE
t.NAME NOT LIKE 'dt%'
AND t.is_ms_shipped = 0
AND i.OBJECT_ID > 255
GROUP BY
t.Name, s.Name, p.Rows
ORDER BY
t.Name

Related

Table is seen twice in sys.tables, once with index and once without

I have a database in Azure SQL Server, and I run the following query to track my DELETE statement.
SELECT
t.NAME AS TableName,
i.name as indexName,
p.[Rows],
sum(a.total_pages) as TotalPages,
sum(a.used_pages) as UsedPages,
sum(a.data_pages) as DataPages,
(sum(a.total_pages) * 8) / 1024 as TotalSpaceMB,
(sum(a.used_pages) * 8) / 1024 as UsedSpaceMB,
(sum(a.data_pages) * 8) / 1024 as DataSpaceMB
FROM
sys.tables t
INNER JOIN
sys.indexes i ON t.OBJECT_ID = i.object_id
INNER JOIN
sys.partitions p ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id
INNER JOIN
sys.allocation_units a ON p.partition_id = a.container_id
WHERE
t.NAME NOT LIKE 'dt%' AND
i.OBJECT_ID > 255
GROUP BY
t.NAME, i.object_id, i.index_id, i.name, p.[Rows]
ORDER BY
object_name(i.object_id) ;
The results shows the table Events twice, but once with index and once without.
# TableName indexName Rows
1 Events [NULL] 1,878
2 Events IX_timestamp 1,886
Why I run the following delete statement, it deletes from both tables:
DELETE from [dbo].[Events]
where timestamp > parse('2021-11-29 08:59:00.1130000' as datetime)
and timestamp < cast('2021-11-30' as date)
Can someone please assist in understanding why this happens? I only want the table with the index to exist.

SQL Server - Grouping all partition table size

I'm using sp_msforeachDB to identify the size occupied at table level. In case of a partition table, is it possible to group all the partitions of a particular table and give a consolidated size occupied.
For instance, Table A has 12 partitions. sp_msforeachDB provides 12 rows of Table A with the size, rowcount etc. Is there any way to consolidate all the 12 rows in one single row
SELECT CAST(MONTH(#Date) AS VARCHAR,
CAST(YEAR(#Date) AS VARCHAR,
#DBName as db,
t.Name as TableName,
s.Name as SchemaName,
p.Rows as RowCount,
SUM(a.total_pages) * 8 as TotalSpaceKB
FROM sys.tables t
INNER JOIN sys.indexes i ON t.Object_ID = i.Object_ID
INNER JOIN sys.partitions p ON i.Object_ID = p.Object_ID and i.Index_ID = p.Index_ID
INNER JOIN sys.allocation_units a ON p.partition_ID = a.container_ID
LEFT OUTER JOIN sys.schemas s ON t.schema_ID = s.schema_ID
WHERE p.Rows > 0 and t.is_ms_shipped = 0 and i.Object_ID > 255
GROUP BY t.Name, s.Name, p.Rows
Take the SUM of p.Rows instead of grouping by it.
SELECT CAST(MONTH(GetDate()) AS VARCHAR),
CAST(YEAR(GetDate()) AS VARCHAR),
DB_NAME() as db,
t.Name as TableName,
s.Name as SchemaName,
Sum(p.Rows) as [RowCount],
SUM(a.total_pages) * 8 as TotalSpaceKB
FROM sys.tables t
INNER JOIN sys.indexes i ON t.Object_ID = i.Object_ID
INNER JOIN sys.partitions p ON i.Object_ID = p.Object_ID and i.Index_ID = p.Index_ID
INNER JOIN sys.allocation_units a ON p.partition_ID = a.container_ID
LEFT OUTER JOIN sys.schemas s ON t.schema_ID = s.schema_ID
WHERE p.Rows > 0 and t.is_ms_shipped = 0 and i.Object_ID > 255
GROUP BY t.Name, s.Name;

Differences in Table size

I have to check big tables on our SQL Server and shrink these tables if possible. See select below.
One table is about 18 GB. This big table contains a column of type Image which stores binary data.
SELECT
s.Name AS SchemaName,
t.NAME AS TableName,
p.rows AS Zeilenanzahl,
SUM(a.total_pages) * 8 AS [Total Storage],
SUM(a.used_pages) * 8 AS [Used Storage],
(SUM(a.total_pages) - SUM(a.used_pages)) * 8 AS [Free Storage]
FROM
sys.tables t
INNER JOIN
sys.schemas s ON s.schema_id = t.schema_id
INNER JOIN
sys.indexes i ON t.OBJECT_ID = i.object_id
INNER JOIN
sys.partitions p ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id
INNER JOIN
sys.allocation_units a ON p.partition_id = a.container_id
WHERE
t.is_ms_shipped = 0
AND i.OBJECT_ID > 255
GROUP BY
t.Name, s.Name, p.Rows
ORDER BY
[Total Storage] desc, s.Name, t.Name
If I check the image field I get about 700 MB.
select sum( datalength(Property))
from dbo.TIF_UserDWProperty
I have checked the actual Size with this:
select * into NewTable
from dbo.TIF_UserDWProperty
The newly created table has the correct size of about 700 MB.
Where does this difference came from?
How can I shrink this table?
I have tried DBCC SHRINKDATABASE with no success.
Thank in advance.

How can I check the size of MS PDW table?

The way of checking the table space size in MS SQLServer I've known is
sp_spaceused [tablename]
OR
SELECT
t.NAME AS TableName,
p.rows AS RowCounts,
SUM(a.total_pages) * 8 AS TotalSpaceKB,
SUM(a.used_pages) * 8 AS UsedSpaceKB,
(SUM(a.total_pages) - SUM(a.used_pages)) * 8 AS UnusedSpaceKB
FROM
[sys].[tables] t
INNER JOIN
[sys].[indexes] i ON t.OBJECT_ID = i.object_id
INNER JOIN
[sys].[partitions] p ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id
INNER JOIN
[sys].[allocation_units] a ON p.partition_id = a.container_id
WHERE
t.NAME NOT LIKE 'dt%'
AND t.is_ms_shipped = 0
AND i.OBJECT_ID > 255
GROUP BY
t.Name, p.Rows
ORDER BY
t.Name
But I can't find the [sys].[allocation_units] table in MS PDW (SQL Server 2012 Parallel Data Warehouse).
So it is hard to check the table size.
How can I know the table space size ??
USE AdventureWorksPDW2012;
DBCC PDW_SHOWSPACEUSED ( "AdventureWorksPDW2012.dbo.FactInternetSales" );
DBCC PDW_SHOWSPACEUSED ( "AdventureWorksPDW2012..FactInternetSales" );
DBCC PDW_SHOWSPACEUSED ( "dbo.FactInternetSales" );
DBCC PDW_SHOWSPACEUSED ( FactInternetSales );

How do I check the size of an indexed view in SQL Server?

Its easy to check storage sizes for Tables and Indexes, you can right-click the table object on SSMS explorer and voila, the details appear in a nice popup.
But since Indexed Views are displayed the same as Normal Views, there is no storage information avaiable in SSMS to show me the current size taken up on disk.
Is there an alterate way to calculate the size (say via a system SP or similar method)?
Thanks.
EXEC sys.sp_spaceused #objname = N'dbo.YourView'
You can use this query here to find your data for any given indexed view:
SELECT
v.NAME AS ViewName,
i.name AS IndexName,
p.rows AS RowCounts,
SUM(a.total_pages) * 8 AS TotalSpaceKB,
SUM(a.used_pages) * 8 AS UsedSpaceKB,
SUM(a.data_pages) * 8 AS DataSpaceKB
FROM
sys.views v
INNER JOIN
sys.indexes i ON v.OBJECT_ID = i.object_id
INNER JOIN
sys.partitions p ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id
INNER JOIN
sys.allocation_units a ON p.partition_id = a.container_id
WHERE
v.Name = 'YourViewNameHere' --View name only, not 'schema.viewname'
AND
i.index_id = 1 -- clustered index, remove this to see all indexes
GROUP BY
v.NAME, i.object_id, i.index_id, i.name, p.Rows
Gives an output something like
ViewName IndexName RowCounts TotalSpaceKB UsedSpaceKB DataSpaceKB
YourViewName IX_YourView 1771 592 552 536

Resources