Can I find which indexes are current / in use - sql

I have the following:
SELECT name AS index_name,
STATS_DATE(OBJECT_ID, index_id) AS StatsUpdated
FROM sys.indexes
Is it possible to add some information so I have an idea if each index is current and in use?
Also I'd like to return in the SELECT clause which table the index is on?

I am using the following query to find if an index is is use - if seeks,scans and look-ups are zero then it's not used.
SELECT TOP 25
o.name AS ObjectName
, i.name AS IndexName
, i.index_id AS IndexID
, dm_ius.user_seeks AS UserSeek
, dm_ius.user_scans AS UserScans
, dm_ius.user_lookups AS UserLookups
, dm_ius.user_updates AS UserUpdates
, p.TableRows
, is_disabled
,STATS_DATE(i.[OBJECT_ID], i.index_id) AS StatsUpdated
FROM sys.dm_db_index_usage_stats dm_ius
INNER JOIN sys.indexes i ON i.index_id = dm_ius.index_id AND dm_ius.object_id = i.object_id
INNER JOIN sys.objects o on dm_ius.object_id = o.object_id
INNER JOIN sys.schemas s on o.schema_id = s.schema_id
INNER JOIN (SELECT SUM(p.rows) TableRows, p.index_id, p.object_id
FROM sys.partitions p GROUP BY p.index_id, p.object_id) p
ON p.index_id = dm_ius.index_id AND dm_ius.object_id = p.object_id
WHERE OBJECTPROPERTY(dm_ius.object_id,'IsUserTable') = 1
AND dm_ius.database_id = DB_ID()
AND i.type_desc = 'nonclustered'
AND i.is_primary_key = 0
AND i.is_unique_constraint = 0
ORDER BY (dm_ius.user_seeks + dm_ius.user_scans + dm_ius.user_lookups) ASC
GO

Related

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

Records not shown while selecting from table in mssql

I'm Getting total tables in whole database and its row-count from following query:
SELECT SCHEMA_NAME(A.schema_id) + '.' +
A.Name, SUM(B.rows) AS 'RowCount'
FROM sys.objects A
INNER JOIN sys.partitions B ON A.object_id = B.object_id
WHERE A.type = 'U'
GROUP BY A.schema_id, A.Name
Order By 'RowCount' desc
After that i'm getting below result:
And then finally when i'm trying to fetch records from one of this table
select * from dbo.[xxx_$Retail ICT Header]
It gives 0 rows as output......Any clue?
To get the Row count in each table try below query and after that cross check by running select query.
SELECT s.name+'.'+o.name,
ddps.row_count
FROM sys.indexes AS i
INNER JOIN sys.objects AS o ON i.OBJECT_ID = o.OBJECT_ID
INNER JOIN sys.schemas s on s.schema_id= o.schema_id
INNER JOIN sys.dm_db_partition_stats AS ddps ON i.OBJECT_ID = ddps.OBJECT_ID
AND i.index_id = ddps.index_id
WHERE i.index_id < 2 AND o.is_ms_shipped = 0 ORDER BY o.NAME

Individual index size for SQL Server

I can use sp_spaceused tablename to get the total index size for that table.
But is there a way to get all indexes of that table's size individually?
table-and-index-size-in-sql-server
Following script is copied from the above answer from Rob Garisson
SELECT
i.name AS IndexName,
s.used_page_count * 8 AS IndexSizeKB
FROM sys.dm_db_partition_stats AS s
JOIN sys.indexes AS i
ON s.[object_id] = i.[object_id] AND s.index_id = i.index_id
WHERE s.[object_id] = object_id('dbo.TableName')
ORDER BY i.name
SELECT
i.name AS IndexName,
SUM(page_count * 8) AS IndexSizeKB
FROM sys.dm_db_index_physical_stats(
db_id(), object_id('dbo.TableName'), NULL, NULL, 'DETAILED') AS s
JOIN sys.indexes AS i
ON s.[object_id] = i.[object_id] AND s.index_id = i.index_id
GROUP BY i.name
ORDER BY i.name