How can I check the size of MS PDW table? - sql-server

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 );

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;

sql server table size check

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

SQL - Get databases size

I have implemented Blob storage for sharepoint. The code below works and I want to make sure it's counting the filestream on the drive for the blobs as well.
SELECT [Database Name] = DB_NAME(database_id),
[Type] = CASE WHEN Type_Desc = 'ROWS' THEN 'Data File(s)'
WHEN Type_Desc = 'LOG' THEN 'Log File(s)'
ELSE Type_Desc END,
[Size in MB] = CAST( ((SUM(Size)* 8) / 1024.0) AS DECIMAL(18,2) )
FROM sys.master_files
-- Uncomment if you need to query for a particular database
-- WHERE database_id = DB_ID(‘Database Name’)
GROUP BY GROUPING SETS
(
(DB_NAME(database_id), Type_Desc),
(DB_NAME(database_id))
)
ORDER BY DB_NAME(database_id), Type_Desc DESC
GO
Please try one of these SQL scripts.
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
--- SQL2005
select o.name
, reservedpages = sum(a.total_pages)
, usedpages = sum(a.used_pages)
, pages = sum(case when a.type <> 1 then a.used_pages
when p.index_id < 2 then a.data_pages else 0 end)
, SUM(a.used_pages)*8096 AS 'Size(B)'
, rows = sum(case when (p.index_id < 2) and (a.type = 1) then p.rows else 0 end)
from sys.objects o
join sys.partitions p on p.object_id = o.object_id
join sys.allocation_units a on p.partition_id = a.container_id
where o.type = 'U'
group by o.name
order by 3 desc --biggest tables first
In addition, here are a couple links that will give you some more ideas.
http://ask.sqlservercentral.com/questions/88859/sql-server-2008-r2-table-sizes.html
http://www.sqlmatters.com/Articles/Listing%20all%20tables%20in%20a%20database%20and%20their%20row%20counts%20and%20sizes.aspx

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.

Resources