when were index statistics last updated? - sql-server

Is there a quick and easy way to list when every index in the database last had their statistics updated? The preferred answer would be a query. Also, is it possible to determine the "quality" of the statistics: FULLSCAN, SAMPLE n, etc.
EDIT
This worked for what I needed, a slight mod to #OrbMan great answer...
SELECT
STATS_DATE(i.object_id, i.index_id) AS LastStatisticsDate
,o.Name AS TableName
,i.name AS IndexName
FROM sys.objects o
INNER JOIN sys.indexes i ON o.object_id = i.object_id
WHERE o.is_ms_shipped=0
ORDER BY 1 DESC

You can do: STATS_DATE ( table_id , index_id )
So:
USE AdventureWorks;
GO
SELECT 'Index Name' = i.name, 'Statistics Date' = STATS_DATE(i.object_id, i.index_id)
FROM sys.objects o
JOIN sys.indexes i ON o.name = 'Address' AND o.object_id = i.object_id;
GO
where Address is the name of the table whose indexes you would like to examine.

Related

How to determine when SQL Server index statistics were last updated

I want to check which index stats that hasn't been updated on tables that has page count of 500 mb and more.
I have this script:
SELECT OBJECT_NAME(object_id) AS [ObjectName]
,[name] AS [StatisticName]
,STATS_DATE([object_id], [stats_id]) AS [StatisticUpdateDate]
FROM sys.stats
order by StatisticUpdateDate desc
Its not giving me all the info I want. Can someone be able to modify my script or show me how to get where I want to be?
Thanks in advance.
Here is a solution for 500 pages even if I think 500Mb is more reasonable finter:
with cte as
(
select p.object_id,
object_name(p.object_id) as obj,
sum(total_pages) as tot_pages
from sys.partitions p join sys.allocation_units au
on au.container_id = p.hobt_id
group by p.object_id
)
SELECT c.* ,
OBJECT_NAME(st.object_id) AS [ObjectName]
,[name] AS [StatisticName]
,STATS_DATE(st.[object_id],
[stats_id]) AS [StatisticUpdateDate]
FROM sys.stats st join cte c
on st.object_id = c.object_id
where tot_pages >= 500;

What are the tables whose names start with `TT_`?

Using this SQL statement to find in my database all tables the have columns whose names contain ItemId:
SELECT o.name,
c.name
FROM sys.columns c
INNER JOIN sys.objects o ON c.object_id = o.object_id
WHERE c.name LIKE '%ItemId%'
ORDER BY o.name, c.column_id
I received a result containing two tables:
ResourceData
TT_Data_117F9D94
I know about the ResourceData table and I can find it in the list of my tables.
But I have not been able to find in my database any table name TT_Data_117F9D94.
Any idea where/how did this table show up in the result of my query?
First of all
You should have used
SELECT *
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME LIKE '%itemid%';
Probably TT_Data_117F9D94 is a data table in your database.
Please check the database design of that table. Or simply do a select query from that table.
Check about table types -
CREATE TYPE dbo.TT_Type AS TABLE (a INT)
SELECT o.name, c.name
FROM sys.columns c
JOIN sys.objects o ON c.[object_id] = o.[object_id]
WHERE c.name = 'a'
Correct query -
SELECT SCHEMA_NAME(o.[schema_id]), o.name, c.name
FROM sys.columns c
JOIN sys.objects o ON c.[object_id] = o.[object_id]
WHERE o.[type] = 'U' -- only user tables

Get record count of each table?

I have 700 tables, and I want to know the record count of each table using dbvisualizer.
How can do simply and effective? Help me.
Try this:
SELECT SUM(TABLE_ROWS) FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '{database}';
SELECT distinct t.name, p.rows
FROM sys.tables AS t
INNER JOIN sys.partitions AS p
ON t.object_id = p.object_id
ORDER BY t.name

How can i get a list of Indexes where a particular index column appears first?

I've noticed that we have a lot of indexes that begin with a certain column and that column has low cardinality (i.e. Company and 99% of records belong to the 'live' company).
Therefore these indexes are next to useless (from what I've read) as they are not providing a means of segregating the data in the table to be found quickly.
So, I want to run a script that will find me all indexes in the database, where that indexes first column, is a column called 'ROW_COMPANY' for example.
I've tried various samples and stuff but I'm not getting the correct results (i.e. too many rows, rows that include indexes that do not start with 'ROW_COMPANY').
You Help Appreciated !!
Try this out:
SELECT
o.name TableName
, c.name ColumnName
, i.name IndexName
FROM
sys.index_columns ic
INNER JOIN sys.indexes i ON ic.object_id = i.object_id
AND ic.index_id = i.index_id
INNER JOIN sys.columns c ON ic.object_id = c.object_id
AND ic.column_id = c.column_id
INNER JOIN sys.objects o ON ic.object_id = o.object_id
WHERE
o.is_ms_shipped = 0
AND c.name = 'ROW_COMPANY'
AND ic.index_column_id = 1

Is there a clean T-SQL query I can use to verify an index has the right columns?

I am writing a DB upgrade script that will check to see if an index has the right two columns defined. If it doesn't, or if it only has one of them, then I will DROP it (is there a way to ALTER an index?) and then recreate it with both.
I don't have a database immediately on-hand to test this, but you should be able to see if a column exists in an index by using the following IF EXISTS statement.
I'm not sure whether you can alter an index on the fly.
IF EXISTS
(
SELECT MyIndex.Name AS IndexName,
Columns.name AS ColumnName
FROM sys.indexes MyIndex
INNER JOIN sys.index_columns IndexColumns
ON MyIndex.index_id = IndexColumns.index_id
AND MyIndex.object_id = IndexColumns.object_id
INNER JOIN sys.columns Columns
ON Columns.column_id = IndexColumns.column_id
AND IndexColumns.object_id = Columns.object_id
WHERE Columns.name = 'ColumnName'
AND MyIndex.Name='IX_MyIndexName'
)
Thanks for your help, Ed. Here is the solution I wrote using yours as a start. It has been verified. Basically it has all of the correct joins.
IF EXISTS
(
SELECT i.Name AS IndexName, c.Name AS ColumnName
FROM sys.indexes i
JOIN sys.index_columns ic
ON i.object_id = ic.object_id AND i.index_id = ic.index_id
JOIN sys.columns c
ON ic.object_id = c.object_id AND c.column_id = ic.column_id
WHERE c.Name = 'MyColumnName' AND i.Name='MyIndexName'
)

Resources