Sql Server 数据库索引整理语句,自动整理数据库索引

时间:2024-01-14 02:37:25 

在一个大型数据库中,数据的更改是非常频繁的。
而建立在这些数据上的索引也是需要经常去维护的。
否则这这些数据索引就起不到起应起的作用。甚至会成为数据库本身的负担。
我们就要定期的对数据库的索引进行维护 我在MSDN上发现了这个脚本不过其中有些小问题我已经修正 大家可以使用这个脚本对数据库的索引进行日常维护


SET NOCOUNT ON;
DECLARE @objectid int;
DECLARE @indexid int;
DECLARE @partitioncount bigint;
DECLARE @schemaname sysname;
DECLARE @objectname sysname;
DECLARE @indexname sysname;
DECLARE @partitionnum bigint;
DECLARE @partitions bigint;
DECLARE @frag float;
DECLARE @command varchar(8000);
DECLARE @dbId int;
-- ensure the temporary table does not exist
IF EXISTS (SELECT name FROM sys.objects WHERE name = 'work_to_do')
DROP TABLE work_to_do;
-- conditionally select from the function, converting object and index IDs to names.
set @dbId=DB_ID();
SELECT
object_id AS objectid,
index_id AS indexid,
partition_number AS partitionnum,
avg_fragmentation_in_percent AS frag
INTO work_to_do FROM sys.dm_db_index_physical_stats (@dbId, NULL, NULL , NULL, 'LIMITED')
WHERE avg_fragmentation_in_percent > 10.0 AND index_id > 0;
-- Declare the cursor for the list of partitions to be processed.
DECLARE partitions CURSOR FOR SELECT * FROM work_to_do;

-- Open the cursor.
OPEN partitions;

-- Loop through the partitions.
FETCH NEXT
FROM partitions
INTO @objectid, @indexid, @partitionnum, @frag;

WHILE @@FETCH_STATUS = 0
BEGIN;
SELECT @objectname = o.name, @schemaname = s.name
FROM sys.objects AS o
JOIN sys.schemas as s ON s.schema_id = o.schema_id
WHERE o.object_id = @objectid;

SELECT @indexname = name
FROM sys.indexes
WHERE object_id = @objectid AND index_id = @indexid;

SELECT @partitioncount = count (*)
FROM sys.partitions
WHERE object_id = @objectid AND index_id = @indexid;

-- 30 is an arbitrary decision point at which to switch between reorganizing and rebuilding
IF @frag < 30.0
BEGIN;
SELECT @command = 'ALTER INDEX [' + @indexname + '] ON ' + @schemaname + '.[' + @objectname + '] REORGANIZE';
IF @partitioncount > 1
SELECT @command = @command + ' PARTITION=' + CONVERT (CHAR, @partitionnum);

EXEC (@command);
END;

IF @frag >= 30.0
BEGIN;
SELECT @command = 'ALTER INDEX [' + @indexname +'] ON ' + @schemaname + '.[' + @objectname + '] REBUILD';
IF @partitioncount > 1
SELECT @command = @command + ' PARTITION=' + CONVERT (CHAR, @partitionnum);

EXEC (@command);
END;
PRINT 'Executed ' + @command;

FETCH NEXT FROM partitions INTO @objectid, @indexid, @partitionnum, @frag;
END;
-- Close and deallocate the cursor.
CLOSE partitions;
DEALLOCATE partitions;

-- drop the temporary table
IF EXISTS (SELECT name FROM sys.objects WHERE name = 'work_to_do')
DROP TABLE work_to_do;
GO


这个脚本在运行时 会建立一个表 work_to_do 整理完毕后会自动删除这个表。如果大家不喜欢这样的话也可以用 一个 临时表解决 .

标签:Sql,Server,数据库索引
0
投稿

猜你喜欢

  • Pycharm导入anaconda环境的教程图解

    2022-12-15 04:26:40
  • MySql 8.0.11安装配置教程

    2024-01-18 23:41:46
  • vue如何通过params和query传值(刷新不丢失)

    2024-05-09 15:17:23
  • asp对象之:基于adodb.stream的文件操作类

    2008-06-07 08:38:00
  • python numpy元素的区间查找方法

    2021-12-22 17:10:31
  • 《色彩解答》系列之二 色彩比例

    2008-02-17 14:38:00
  • Deepin20安装开发环境的超详细教程

    2023-12-16 20:05:41
  • MySQL错误中文参照列表

    2010-09-30 14:41:00
  • 基于JS设计12306登录页面

    2024-04-29 13:45:12
  • JavaScript获取时区实现过程解析

    2023-08-22 20:58:34
  • 如何正确处理ajax 302跳转问题回博客首页

    2009-02-28 14:01:00
  • JavaScript变量声明var,let.const及区别浅析

    2024-05-09 15:05:37
  • SQL Server误区30日谈 第2天 DBCC CHECKDB会导致阻塞

    2024-01-14 10:09:11
  • Python 基于Selenium实现动态网页信息的爬取

    2023-10-31 12:00:22
  • 关于MySQL与Golan分布式事务经典的七种解决方案

    2024-01-15 00:49:33
  • Windows下anaconda安装第三方包的方法小结(tensorflow、gensim为例)

    2021-09-14 00:22:11
  • 在CentOS上MySQL数据库服务器配置方法

    2024-01-19 07:01:21
  • 使用ERWin进行基于MySQL数据库的物理设计

    2009-01-04 12:54:00
  • 关于换行和回车的图文小结

    2023-07-17 14:41:37
  • Tensorflow使用tfrecord输入数据格式

    2022-06-18 22:55:40
  • asp之家 网络编程 m.aspxhome.com