SQL Server 索引运维

Posted 哦呵呵G

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQL Server 索引运维相关的知识,希望对你有一定的参考价值。

sql server检测库里所有表的索引碎片

 

SELECT schema_name(T.schema_id) AS Schema_Name,T.Name AS Table_Name,I.name AS Index_Name,
I.type AS Index_Type,D.avg_fragmentation_in_percent AS avg_fragmentation_in_percent 
FROM sys.dm_db_index_physical_stats(DB_id(DBNAME),null, null, null, null) AS D
INNER JOIN sys.indexes AS I WITH(NOLOCK) ON D.index_id=I.index_id AND D.object_id=I.object_id
INNER JOIN sys.tables AS T WITH(NOLOCK) ON T.object_id=D.object_id
WHERE I.type>0 AND T.is_ms_shipped=0 AND D.avg_fragmentation_in_percent>=30
order by D.avg_fragmentation_in_percent desc

 

SQL Server 重建所有表索引方法

USE My_Database;
DECLARE @name varchar(100)

DECLARE authors_cursor CURSOR FOR  
Select [name]   from sysobjects where xtype=u order by id

OPEN authors_cursor

FETCH NEXT FROM authors_cursor  INTO @name

WHILE @@FETCH_STATUS = 0 
BEGIN    

   DBCC DBREINDEX (@name, ‘‘, 90)

   FETCH NEXT FROM authors_cursor  
   INTO @name 
END

deallocate authors_cursor

 

以上是关于SQL Server 索引运维的主要内容,如果未能解决你的问题,请参考以下文章

Microsoft SQL Server 代码片段收集

SQL Server中的全文搜索

缺少 SQL SERVER 2014 代码片段

使用实体框架迁移时 SQL Server 连接抛出异常 - 添加代码片段

sql server 2008r2 向带有索引的表里大批量插入数据

一图胜千言 -- SQL Server 日常运维