sql one specific table size and stats - sql

i have this fine working query for determining
size,rowcount,ununsed,total mb of db..
it is mssql
thing is is there a way to modify this so it does query only one specific table in DB ? thanks alot. also any hints on how can i query more statistics ? thanks
USE [mydbname]
GO
SELECT
s.Name AS SchemaName,
t.Name AS TableName,
p.rows AS RowCounts,
CAST(ROUND((SUM(a.used_pages) / 128.00), 2) AS NUMERIC(36, 2)) AS Used_MB,
CAST(ROUND((SUM(a.total_pages) - SUM(a.used_pages)) / 128.00, 2) AS NUMERIC(36, 2)) AS Unused_MB,
CAST(ROUND((SUM(a.total_pages) / 128.00), 2) AS NUMERIC(36, 2)) AS Total_MB
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
INNER JOIN sys.schemas s ON t.schema_id = s.schema_id
GROUP BY t.Name, s.Name, p.Rows
ORDER BY s.Name, t.Name
GO

You should just add the where condition:
WHERE t.Name = 'TABLE_NAME_GOES_HERE'
And if you have same tables in different schemas
WHERE t.Name = 'TABLE_NAME_GOES_HERE' and s.Name = 'SCHEMA_NAME_GOES_HERE'

okay finally a solution.
USE db_name
GO
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 = 'table_name'
AND t.is_ms_shipped = 0
AND i.object_id > 255
GROUP BY
t.name, s.name, p.rows
ORDER BY
t.name;
GO

Related

What is MS SQL table's sys.allocation_units alternative table in Oracle? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 1 year ago.
Improve this question
I have a query in MS SQL which I need to convert in Oracle for obtaining the same results as in MS SQL.
But I am unable to find the corresponding system tables in Oracle Database which would give me the required columns/results.
Please help me to build the equivalent oracle for the following MS SQL query.
The MS SQL query is:-
SELECT
TOP 10 B.TableName AS TableName,
A.used_mb AS used_mb,
A.allocated_mb AS allocated_mb,
REPLACE(A.Ratio_used_mb, '%', '') AS Ratio_used_mb,
REPLACE(A.Ratio_allocated_mb, '%', '') AS Ratio_allocated_mb,
B.RowCounts AS RowCounts,
B.TotalPages AS TotalPages,
B.UsedPages AS UsedPages,
B.DataPages AS DataPages,
DATEPART(WEEK, GETUTCDATE()) AS Weekvalue
FROM
(
SELECT
tab.name AS TableName,
CAST(SUM(spc.used_pages * 8) / 1024.00 AS NUMERIC(36, 2)) AS used_mb,
CAST(SUM(spc.total_pages * 8) / 1024.00 AS NUMERIC(36, 2)) AS allocated_mb,
FORMAT((CAST(SUM(spc.used_pages * 8) / 1024.00 AS NUMERIC(36, 2))) / (
SELECT
CAST(SUM(spc.used_pages * 8) / 1024.00 AS NUMERIC(36, 2)) AS used_mb
FROM
sys.tables tab
JOIN
sys.indexes ind
ON tab.object_id = ind.object_id
JOIN
sys.partitions part
ON ind.object_id = part.object_id
AND ind.index_id = part.index_id
JOIN
sys.allocation_units spc
ON part.partition_id = spc.container_id), 'P') AS Ratio_used_mb,
FORMAT((CAST(SUM(spc.total_pages * 8) / 1024.00 AS NUMERIC(36, 2))) / (
SELECT
CAST(SUM(spc.total_pages * 8) / 1024.00 AS NUMERIC(36, 2)) AS allocated_mb
FROM
sys.tables tab
JOIN
sys.indexes ind
ON tab.object_id = ind.object_id
JOIN
sys.partitions part
ON ind.object_id = part.object_id
AND ind.index_id = part.index_id
JOIN
sys.allocation_units spc
ON part.partition_id = spc.container_id), 'P') AS Ratio_allocated_mb
FROM
sys.tables tab
JOIN
sys.indexes ind
ON tab.object_id = ind.object_id
JOIN
sys.partitions part
ON ind.object_id = part.object_id
AND ind.index_id = part.index_id
JOIN
sys.allocation_units spc
ON part.partition_id = spc.container_id
GROUP BY
tab.name
)
A
FULL JOIN
(
SELECT
t.name AS TableName,
SUM(p.ROWS) AS RowCounts,
SUM(a.total_pages) AS TotalPages,
SUM(a.used_pages) AS UsedPages,
SUM(a.data_pages) AS DataPages
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
AND i.index_id <= 1
GROUP BY
t.name,
i.object_id,
i.index_id,
i.name
)
B
ON B.TableName = A.TableName
ORDER BY
B.UsedPages DESC
There probably isn't a one-for-one same table. The architecture of the two products is fundamentally different, as is the method of space (storage) allocation. Take a look at DBA_TABLES, DBA_SEGMENTS, DBA_EXTENTS. They are all documented in the Database Reference. That manual for Oracle 18 is located here.

SQL: count and show all rows and columns of all tables under a scheme

somewhere on the net (source missing, may be here) I got this SQL-statement:
SELECT t.NAME AS TableName, SUM(a.total_pages) * 8 AS TotalSpaceKB, p.Rows 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.NAME LIKE '%MY_TABLES%' AND s.Name LIKE 'MY_ACHEMA'
AND t.is_ms_shipped = 0
AND i.OBJECT_ID > 255
GROUP BY t.Name, p.Rows
ORDER BY p.Rows DESC
which gives me a beautiful view with 3 columns. something like this:
TableName TotalSpaceKB Rows
TABLE 3231656 76000
TABLE 2305632 29136
TABLE 2213128 14160
TABLE 1954200 3020
etc...
now I'd like to expand it to inclide the number of columns of each table. How can this be done?
I removed the grouping and used subqueries:
SELECT
t.NAME AS TableName,
(SELECT SUM(a.total_pages) * 8
FROM sys.indexes i
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.OBJECT_ID = i.object_id AND i.OBJECT_ID > 255
) AS TotalSpaceKB,
(SELECT SUM(p.Rows)
FROM sys.indexes i
INNER JOIN sys.partitions p ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id
WHERE t.OBJECT_ID = i.object_id AND i.OBJECT_ID > 255
) AS Rows,
(SELECT COUNT(*) FROM sys.columns c WHERE t.OBJECT_ID = c.object_id) AS Columns
FROM sys.tables t
LEFT OUTER JOIN sys.schemas s ON t.schema_id = s.schema_id
WHERE t.NAME NOT LIKE 'dt%' AND t.NAME LIKE '%MY_TABLES%' AND s.Name LIKE 'MY_ACHEMA'
AND t.is_ms_shipped = 0
ORDER BY Rows DESC
This should work for you by adding in the INFORMATION_SCHEMA.COLUMNS.
SELECT t.NAME AS TableName, SUM(a.total_pages) * 8 AS TotalSpaceKB, p.Rows, COUNT(COLUMN_NAME) AS ColumnCount
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 JOIN sys.schemas s ON t.schema_id = s.schema_id
LEFT JOIN INFORMATION_SCHEMA.COLUMNS ISC ON t.NAME = ISC.TABLE_NAME
WHERE t.NAME NOT LIKE 'dt%' AND t.NAME LIKE '%MY_TABLES%' AND s.Name LIKE 'MY_ACHEMA'
AND t.is_ms_shipped = 0
AND i.OBJECT_ID > 255
GROUP BY t.Name, p.Rows
ORDER BY p.Rows DESC

Metadata of Getting number of rows in Views

How come is it not possible to return number of rows in a View using sys.views in the below query while using sys.table you can
SELECT t.NAME AS table_name,
s.name AS owner,
p.[Rows] as NUM_ROWS,
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.schemas s on s.schema_id = t.schema_id
WHERE t.NAME NOT LIKE 'dt%' AND i.OBJECT_ID > 255 AND
i.index_id <= 1 /* AND
s.name in ('Schemaname1','Schemaname2') */
GROUP BY t.NAME, i.object_id, i.index_id, i.name,s.name, p.[Rows]
ORDER BY object_name(i.object_id) ;
Its a syntax issue you got, an extra comma in the query after NUM_ROWS ?
SELECT t.NAME AS table_name,
s.name AS owner,
p.[Rows] as NUM_ROWS /*, remove this comma*/
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.schemas s on s.schema_id = t.schema_id
WHERE t.NAME NOT LIKE 'dt%' AND i.OBJECT_ID > 255 AND
i.index_id <= 1 /* AND
s.name in ('Schemaname1','Schemaname2') */
GROUP BY t.NAME, i.object_id, i.index_id, i.name,s.name, p.[Rows]
ORDER BY object_name(i.object_id) ;

SQL multiple tables reach

I have this chunk of code here, thing is it query selected "TestLog" and shows:
name,
schema,
rowcount,
totalspace,
usedspace,....
Thing is I need this information to be queried for more than one table, lets say a 5 specific ones... can anyone help out?
USE myDB
GO
SELECT
t.name AS TableName,
s.name AS SchemaName,
p.rows AS RowCounts,
SUM(a.total_pages) * 8 / 1024 AS TotalSpaceMB,
SUM(a.used_pages) * 8 / 1024 AS UsedSpaceMB,
(SUM(a.total_pages) - SUM(a.used_pages)) * 8 / 1024 AS UnusedSpaceMB
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 = ('TestLog')
AND t.is_ms_shipped = 0
AND i.object_id > 255
GROUP BY
t.name, s.name, p.rows
ORDER BY
t.name;
GO
It works as below:
-- specific tables
WHERE
t.name in ('Test1' , 'Test2' , 'Test3')
AND t.is_ms_shipped = 0
AND i.object_id > 255
-- one specific table
WHERE
t.name = 'TestLog'
AND t.is_ms_shipped = 0
AND i.object_id > 255
-- specific tables with test in front
WHERE
t.name like 'Test%'
AND t.is_ms_shipped = 0
AND i.object_id > 255

Multiple tables with the same name in SQL Server

when running the query below, I get a duplicate table returned, even though only one of it exists. Why could that be?
SELECT
s.Name AS SchemaName,
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.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.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
TotalSpaceKB DESC
** UPDATE
The database name was displayed with a (In Recovery) tag, I just ran a script to check and it said that it is 11% completed. Could this also have caused this problem?
As Sean pointed out, you have more than one index on the duplicated tables. To check the index name(s) you can run the slightly modified query:
SELECT
t.NAME AS TableName,
s.Name AS SchemaName,
p.rows AS RowCounts,
i.name,
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, i.name
ORDER BY
t.name