一键生成统计信息语句
2012-05-18 13:49
176 查看
if exists (select * from tempdb..sysobjects where name ='##T')
drop table ##T
create table ##T (name varchar(128),keys varchar(1000),tab varchar(100))
exec sp_MSforeachtable '
insert ##T(name,keys)
exec sp_helpstats ''?''
update ##T set tab=''?''
where tab is null
'
select
'
begin try
if object_id('''+tab+''') is not null and
exists(select 1 from sys.stats
where name ='''+name+''' and object_id = object_id('''+tab+'''))
drop STATISTICS '+tab+'.'+quotename(name)+'
end try
begin catch
print ERROR_MESSAGE()
end catch
' del,
'
if object_id('''+tab+''') is not null and
not exists(select 1 from sys.stats
where name ='''+name+''' and object_id = object_id('''+tab+'''))
create STATISTICS '+quotename(name)+' on '+tab+'('+keys+')
' ad
from ##T a
--WHERE exists(select * from [master].[dbo].[UnionALL]
--where quotename(sch)+'.'+quotename(tbl)=a.tab and a.keys like '%'+col+'%')
--select * from ##T
drop table ##T
drop table ##T
create table ##T (name varchar(128),keys varchar(1000),tab varchar(100))
exec sp_MSforeachtable '
insert ##T(name,keys)
exec sp_helpstats ''?''
update ##T set tab=''?''
where tab is null
'
select
'
begin try
if object_id('''+tab+''') is not null and
exists(select 1 from sys.stats
where name ='''+name+''' and object_id = object_id('''+tab+'''))
drop STATISTICS '+tab+'.'+quotename(name)+'
end try
begin catch
print ERROR_MESSAGE()
end catch
' del,
'
if object_id('''+tab+''') is not null and
not exists(select 1 from sys.stats
where name ='''+name+''' and object_id = object_id('''+tab+'''))
create STATISTICS '+quotename(name)+' on '+tab+'('+keys+')
' ad
from ##T a
--WHERE exists(select * from [master].[dbo].[UnionALL]
--where quotename(sch)+'.'+quotename(tbl)=a.tab and a.keys like '%'+col+'%')
--select * from ##T
drop table ##T
相关文章推荐
- 统计文件信息及生成SQL语句(VBS)实例
- python脚本统计局域网服务器和pc机的系统信息,并生成excel表格
- 用sql语句统计生成(统计项目,值)对
- 使用索引统计信息(Index Statistics)优化查询语句,提高查询效率
- 一键生成索引新语句
- SQL语句调优 - 统计信息的含义与作用及维护计算
- SQL Server读懂语句运行的统计信息 SET STATISTICS TIME IO PROFILE ON
- 当前session生成redo,undo大小,统计信息,以及表的大小
- PLSQL_统计信息系列08_统计信息生成和还原
- sqlserver 中统计信息语句
- 查看当前正在运行的SQL语句的统计信息
- shell脚本生成统计信息
- SQLSERVER收集语句运行的统计信息并进行分析
- 使用索引统计信息(Index Statistics)优化查询语句,提高查询效率
- Mybatis分页-利用Mybatis Generator插件生成基于数据库方言的分页语句,统计记录总数
- Mybatis分页-利用Mybatis Generator插件生成基于数据库方言的分页语句,统计记录总数
- 转自【柳永法】SQL统计:从北京到天津的与从天津到北京的汇总到一起 常用sql语句 SQL Server 生成版权信息及测试环境信息
- 一个 Sql语句优化的问题- STATISTICS 统计信息
- 统计信息中常见的语句