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;
Related
Can anyone help me combining these 2 queries to have a single output with 3 columns: TableName, last_user_update and NumberOfRows? If you could add TotalSpaceMB as well, even better.
SELECT object_name(Object_id) as TableName,
last_user_update
FROM sys.dm_db_index_usage_stats
WHERE database_id = DB_ID( 'databasename')
and
SELECT T.name TableName,i.Rows NumberOfRows
FROM sys.tables T
JOIN sys.sysindexes I ON T.OBJECT_ID = I.ID
WHERE indid IN (0,1)
Thank you.
SELECT
T.name TableName,
i.Rows NumberOfRows,
last_user_update,
(SUM(a.data_pages) * 8) / 1024 AS DataSpaceMB
FROM sys.tables T
JOIN sys.sysindexes I ON T.OBJECT_ID = I.ID
JOIN sys.dm_db_index_usage_stats us ON us.object_id = T.object_id
INNER JOIN
sys.indexes IND ON t.OBJECT_ID = IND.object_id
INNER JOIN
sys.partitions p ON IND.object_id = p.OBJECT_ID AND IND.index_id = p.index_id
INNER JOIN
sys.allocation_units a ON p.partition_id = a.container_id
WHERE indid IN (0,1)
AND database_id = DB_ID( 'databasename')
GROUP BY T.name, I.rows, us.last_user_update
I think this works well.
Test this and take about the result.
SELECT T.name TableName,i.Rows NumberOfRows, st.last_user_update
FROM sys.tables T
JOIN sys.sysindexes I ON T.OBJECT_ID = I.ID
JOIN sys.dm_db_index_usage_stats st on st.object_id = T.object_id
WHERE I.indid IN (0,1)
and st.database_id = DB_ID(DB_NAME())
Thank you.
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
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
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.
I need to list/display all the clustered and non clustered indexes contained by a table.
How can I do that using SQL Server 2008 R2?
How about this:
SELECT
TableName = t.Name,
i.*
FROM
sys.indexes i
INNER JOIN
sys.tables t ON t.object_id = i.object_id
WHERE
T.Name = 'YourTableName'
If you need more information (like columns contained in the index, their datatype etc.) - you can expand your query to something like this:
SELECT
TableName = t.Name,
IndexName = i.Name,
IndexType = i.type_desc,
ColumnOrdinal = Ic.key_ordinal,
ColumnName = c.name,
ColumnType = ty.name
FROM
sys.indexes i
INNER JOIN
sys.tables t ON t.object_id = i.object_id
INNER JOIN
sys.index_columns ic ON ic.object_id = i.object_id AND ic.index_id = i.index_id
INNER JOIN
sys.columns c ON c.object_id = ic.object_id AND c.column_id = ic.column_id
INNER JOIN
sys.types ty ON c.system_type_id = ty.system_type_id
WHERE
t.name = 'YourTableName'
ORDER BY
t.Name, i.name, ic.key_ordinal
These system catalog views contain a wealth of information about your system....