A script job for rebuild DB in AX 2012

-- Ensure a USE <databasename> statement has been executed first.
SET NOCOUNT ON;
DECLARE @objectid int;
DECLARE @indexid int;
DECLARE @partitioncount bigint;
DECLARE @schemaname nvarchar(130);
DECLARE @objectname nvarchar(130);
DECLARE @indexname nvarchar(130);
DECLARE @partitionnum bigint;
DECLARE @partitions bigint;
DECLARE @frag float;
DECLARE @command nvarchar(4000);
-- Conditionally select tables and indexes from the sys.dm_db_index_physical_stats function
-- and convert object and index IDs to names.
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 (DB_ID(), 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.
WHILE (1=1)
    BEGIN;
        FETCH NEXT
           FROM partitions
           INTO @objectid, @indexid, @partitionnum, @frag;
        IF @@FETCH_STATUS < 0 BREAK;
        SELECT @objectname = QUOTENAME(o.name), @schemaname = QUOTENAME(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 = QUOTENAME(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
            SET @command = N‘ALTER INDEX ‘ + @indexname + N‘ ON ‘ + @schemaname + N‘.‘ + @objectname + N‘ REORGANIZE‘;
        IF @frag >= 30.0
            SET @command = N‘ALTER INDEX ‘ + @indexname + N‘ ON ‘ + @schemaname + N‘.‘ + @objectname + N‘ REBUILD‘;
        IF @partitioncount > 1
            SET @command = @command + N‘ PARTITION=‘ + CAST(@partitionnum AS nvarchar(10));
        EXEC (@command);
        PRINT N‘Executed: ‘ + @command;
    END;

-- Close and deallocate the cursor.
CLOSE partitions;
DEALLOCATE partitions;

-- Drop the temporary table.
DROP TABLE #work_to_do;
GO

  

时间: 2024-10-16 07:12:26

A script job for rebuild DB in AX 2012的相关文章

Dynamics AX 2012 – Batch Jobs Not Executing

In Dynamics AX 2012, there are times when batch jobs appear to be stranded in a waiting status.  When this happens, the cause can typically be a batch server or batch group which is not set up properly.   However, if these are set up correctly and yo

How to: Debug X++ Code Running in .NET Business Connector [AX 2012]

This topic has not yet been rated - Rate this topic http://msdn.microsoft.com/EN-US/library/bb190066.aspx Updated: December 16, 2011 Applies To: Microsoft Dynamics AX 2012 R3, Microsoft Dynamics AX 2012 R2, Microsoft Dynamics AX 2012 Feature Pack, Mi

Using Controls in a Form Design [AX 2012]

Using Controls in a Form Design [AX 2012] This topic has not yet been rated - Rate this topic Updated: January 27, 2012 Applies To: Microsoft Dynamics AX 2012 R3, Microsoft Dynamics AX 2012 R2, Microsoft Dynamics AX 2012 Feature Pack, Microsoft Dynam

AX 2012 model应用

说句实话,AX2012 model真心不好用,当你开发好,把Model到入到客户环境时, 要防止有冲突,假如出现冲突的话,还必须去删除另外一个Model里面冲突的代码,真心麻烦. 下面给一个ax 导入的例子: 这个就是导入mode的例子,下面还有各种命令: 假如你想要上网搜索关于model命令时,可以直接百度或者谷歌搜索  ax uninstall model/ax  install model等等 AX 2012 model应用,码迷,mamicode.com

Failed to upgrade AX 2012 R3 Retail channel database from CU9 to CU11 if SQL Server version was lower than 2012

I tried to upgrade AX 2012 R3 Retail channel database from CU9 to CU11 for client. after generated new CreateDatabase.dll with upgraded Retail SDK, ran the Channel Configuration Utility, but got error. So I tried the sql queries from CreateDatabase p

Overview of Form Control Types [AX 2012]

Overview of Form Control Types [AX 2012] Other Versions 0 out of 1 rated this helpful - Rate this topic Updated: October 11, 2011 Applies To: Microsoft Dynamics AX 2012 R3, Microsoft Dynamics AX 2012 R2, Microsoft Dynamics AX 2012 Feature Pack, Micro

The new concept &#39;Model&#39; in AX 2012

There is a new concept 'Model' in AX 2012 rather than AX 2009. It can be considered as a container to store all the AOT elements. If you have some experience in .NET programing, you can regard it as 'Namespace'. Yes, it is only a virtual container or

Dynamics AX 2012 R3 Demo 安装与配置 - 导入测试数据 (Step 4)

    在前面三节中,Reinhard分别讲解了如何配置安装环境,安装数据库服务器,AOS和客户端,安装后的编译和配置.如果一直跟随Reinhard的脚步,到这里,已经拥有一个没有数据的系统.     本节,Reinhard将要讲解怎样导入微软提供的测试数据.     首先,将AOS服务停止运行.进入计算机管理,服务,选中AOS服务,右键点击停止.稍等片刻,AOS服务已经停止运行.     接着,Reinhard要将当前空数据库进行备份,避免因导入测试数据失败造成的损失.     进入数据库管理

Table Properties [AX 2012]

Table Properties [AX 2012] 1 out of 2 rated this helpful - Rate this topic Updated: July 20, 2012 Applies To: Microsoft Dynamics AX 2012 R2, Microsoft Dynamics AX 2012 Feature Pack, Microsoft Dynamics AX 2012 This topic describes the properties that