Skip to content

Instantly share code, notes, and snippets.

@swasheck
Created September 23, 2015 04:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save swasheck/063063b37266f5eb5ee9 to your computer and use it in GitHub Desktop.
Save swasheck/063063b37266f5eb5ee9 to your computer and use it in GitHub Desktop.
SQL Server Duplicate Statistics Finder
with x as (
select
stats_generation_number = row_number() over (partition by base.object_id, base.stat_cols, base.filter_definition order by base.is_index desc),
*
from (
select
o.object_id,
s.stats_id,
schema_name = sh.name,
object_name = o.name,
stat_name = s.name,
is_index = case when i.name is null then 0 else 1 end,
s.has_filter,
s.filter_definition,
stat_cols = stuff(
(
select ',' + index_col(db_name(db_id()) + '.' + quotename(sh.name) + '.' + quotename(o.name),sc.stats_id,sc.stats_column_id)
from sys.stats_columns sc
where s.object_id = sc.object_id
and s.stats_id = sc.stats_id
for xml path('')
),1,1,'' --eliminates NULL column names which (in my environment) are reducible to partition key injection
)
from sys.stats s
join sys.objects o
on s.object_id = o.object_id
join sys.schemas sh
on o.schema_id = sh.schema_id
left join sys.indexes i
on s.object_id = i.object_id
and s.name = i.name
where sh.name != 'sys'
) base
)
select *
from x
where x.stats_generation_number > 1
and x.is_index = 0
order by object_id
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment