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
Related
I've created a table in my database by mistake which has zero rows, and I want to know a systemic way to find out that table. I know we can use sp_spaceused to get the number of rows but how can I do it for all the tables.
Paste the output of the below query in a new SQL Editor and do a visual scan of the output
SELECT *
FROM (
SELECT 1000 * ROW_NUMBER () OVER (ORDER BY name) rk,
' sp_spaceused ' + name spused
FROM sys.Tables
UNION ALL
SELECT 1 + 1000 * ROW_NUMBER () OVER (ORDER BY name), ' go'
FROM sys.Tables
) a
ORDER BY a.rk
Below you can find the query with additional details
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
SELECT
CASE WHEN FG.Name IS NULL THEN DS.Name ELSE FG.Name END AS 'FileGroupName'
,DB_NAME() AS 'DatabaseName'
,OBJECT_SCHEMA_NAME(P.OBJECT_ID) AS 'Schema'
,OBJECT_NAME(P.OBJECT_ID) AS 'TableName'
,CASE WHEN P.index_id = 0 THEN 'HEAP' ELSE I.name END AS 'IndexName'
,CASE WHEN P.index_id IN (0,1) THEN 'data' ELSE 'index' END AS 'Type'
,P.partition_number AS 'PartitionNumber'
,CAST(P.reserved_page_count * 0.008 AS NUMERIC(18,2)) AS 'ReservedSize_MB'
,CAST(P.used_page_count * 0.008 AS NUMERIC(18,2)) AS 'UsedSize_MB'
,P.row_count AS 'RowCount'
FROM
sys.dm_db_partition_stats P
INNER JOIN sys.indexes I ON I.OBJECT_ID = P.OBJECT_ID AND I.index_id = P.index_id
INNER JOIN sys.data_spaces DS ON DS.data_space_id = I.data_space_id
LEFT OUTER JOIN sys.filegroups FG ON FG.data_space_id = DS.data_space_id
WHERE
OBJECTPROPERTY(p.OBJECT_ID, 'ISMSSHipped') = 0
ORDER BY ReservedSize_MB DESC
Below you can find the query for details of partitioned tables
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED
SELECT
DB_NAME() AS 'DatabaseName'
,OBJECT_NAME(p.OBJECT_ID) AS 'TableName'
,p.index_id AS 'IndexId'
,CASE
WHEN p.index_id = 0 THEN 'heap'
ELSE i.[name]
END AS 'IndexName'
,sp.data_compression_desc
,p.partition_number AS 'PartitionNumber'
,prv_left.[value] AS 'LowerBoundary'
,prv_right.[value] AS 'UpperBoundary'
,CASE
WHEN fg.[name] IS NULL THEN ds.[name]
ELSE fg.[name]
END AS 'FileGroupName'
,CAST(p.used_page_count * 0.0078125 AS numeric(18,2)) AS 'UsedPages_MB'
,CAST(p.in_row_data_page_count * 0.0078125 AS numeric(18,2)) AS 'DataPages_MB'
,CAST(p.reserved_page_count * 0.0078125 AS numeric(18,2)) AS 'ReservedPages_MB'
,p.row_count AS 'RowCount'
,CASE
WHEN p.index_id IN (0, 1) THEN 'data'
ELSE 'index'
END 'Type'
FROM
sys.dm_db_partition_stats p
INNER JOIN sys.indexes i ON i.OBJECT_ID = p.OBJECT_ID
AND i.index_id = p.index_id
INNER JOIN sys.partitions sp ON p.[partition_id] = sp.[partition_id]
INNER JOIN sys.data_spaces ds ON ds.data_space_id = i.data_space_id
LEFT OUTER JOIN sys.partition_schemes ps ON ps.data_space_id = i.data_space_id
LEFT OUTER JOIN sys.destination_data_spaces dds ON dds.partition_scheme_id = ps.data_space_id
AND dds.destination_id = p.partition_number
LEFT OUTER JOIN sys.filegroups fg ON fg.data_space_id = dds.data_space_id
LEFT OUTER JOIN sys.partition_range_values prv_right ON prv_right.function_id = ps.function_id
AND prv_right.boundary_id = p.partition_number
LEFT OUTER JOIN sys.partition_range_values prv_left ON prv_left.function_id = ps.function_id
AND prv_left.boundary_id = p.partition_number - 1
WHERE
OBJECTPROPERTY(p.OBJECT_ID, 'ISMSSHipped') = 0
ORDER BY 2,3,5
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;
I found this script that give you unused index however it runs only per databases how make it run through all the databases on server
SELECT o.name ,
indexname = i.name ,
i.index_id ,
reads = user_seeks + user_scans + user_lookups ,
writes = user_updates ,
rows = ( SELECT SUM(p.rows)
FROM sys.partitions p
WHERE p.index_id = s.index_id
AND s.object_id = p.object_id
) ,
CASE WHEN s.user_updates < 1 THEN 100
ELSE 1.00 * ( s.user_seeks + s.user_scans + s.user_lookups )
/ s.user_updates
END AS reads_per_write ,
'DROP INDEX ' + QUOTENAME(i.name) + ' ON ' + QUOTENAME(c.name) + '.'
+ QUOTENAME(OBJECT_NAME(s.object_id)) AS 'drop statement'
FROM sys.dm_db_index_usage_stats s
INNER JOIN sys.indexes i ON i.index_id = s.index_id
AND s.object_id = i.object_id
INNER JOIN sys.objects o ON s.object_id = o.object_id
INNER JOIN sys.schemas c ON o.schema_id = c.schema_id
WHERE OBJECTPROPERTY(s.object_id, 'IsUserTable') = 1
AND s.database_id = DB_ID()
AND i.type_desc = 'nonclustered'
AND i.is_primary_key = 0
AND i.is_unique_constraint = 0
AND ( SELECT SUM(p.rows)
FROM sys.partitions p
WHERE p.index_id = s.index_id
AND s.object_id = p.object_id
) > 10000
ORDER BY reads_per_write ASC;
There's a line in your code that is restricting the records to the current database:
AND s.database_id = DB_ID()
in your where clause. if you remove that, it will give you stats on all of the databases and objects you have access to.
you may want to also include the database name if you remove that line:
SELECT
db_name(database_id) DATABASE_NAME,
...
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.
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 );