<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Optimizing the database indexes on SQL Server in Nuxeo Forum</title>
    <link>https://connect.hyland.com/t5/nuxeo-forum/optimizing-the-database-indexes-on-sql-server/m-p/314317#M1318</link>
    <description>&lt;P&gt;This is not specific to Nuxeo but a customer had good results with the following:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;USE [NUXEO]
GO
/****** Object:  StoredProcedure [dbo].[refreshIndexes] ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROC [dbo].[refreshIndexes](@db_name varchar(500))
AS

declare @tmptable TABLE(TableName varchar(500),IndexName varchar(500),AvgPageFragmentation dec(10,2),allowPageLocks bit)
declare @table_name varchar(500)
declare @index_name varchar(500)
declare @frag float
declare @allowPageLocks bit
declare @req nvarchar(max)

set @req = '
 SELECT 
  OBJECT_NAME(DPS.OBJECT_ID,DB_ID(''' + @db_name + ''')) AS TableName
  ,SI.NAME AS IndexName
  ,DPS.AVG_FRAGMENTATION_IN_PERCENT AS AvgPageFragmentation,
  allow_page_locks
 FROM sys.dm_db_index_physical_stats (DB_ID(''' + @db_name + '''), NULL, NULL , NULL, NULL) DPS
 INNER JOIN [' + @db_name + '].sys.indexes SI 
  ON DPS.OBJECT_ID = SI.OBJECT_ID 
  AND DPS.INDEX_ID = SI.INDEX_ID
  AND SI.NAME IS NOT NULL
 ORDER BY DPS.avg_fragmentation_in_percent DESC
 '
insert into @tmptable
exec(@req)
--print @req

DECLARE bigindex cursor LOCAL FAST_FORWARD
FOR
 select *from @tmptable where IndexName is not null

open bigindex
FETCH NEXT from bigindex into @table_name,@index_name,@frag,@allowPageLocks
WHILE (@@FETCH_STATUS=0)
BEGIN
 set @req = ''
-- REORGANIZE FAILED = Indexes for which ALLOW_PAGE_LOCKS is OFF
 if (@frag &amp;gt;=40 or (@allowPageLocks=0 and @frag &amp;gt;=10)) 
  set @req = 'ALTER INDEX [' + @index_name + '] ON [' + @db_name + '].dbo.[' + @table_name + '] REBUILD WITH(FILLFACTOR=80,STATISTICS_NORECOMPUTE=OFF,ONLINE=OFF)' 
 else if (@frag &amp;gt;=10 )
  set @req = 'ALTER INDEX [' + @index_name + '] ON [' + @db_name + '].dbo.[' + @table_name + '] REORGANIZE WITH ( LOB_COMPACTION = ON )'
 if (@req!='')
  exec(@req)
print @req

FETCH NEXT from bigindex into @table_name,@index_name,@frag,@allowPageLocks
END

CLOSE bigindex
DEALLOCATE bigindex
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The following links may also be of interest:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;A href="http://www.brentozar.com/archive/2013/09/index-maintenance-sql-server-rebuild-reorganize/" target="test_blank"&gt;http://www.brentozar.com/archive/2013/09/index-maintenance-sql-server-rebuild-reorganize/&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://sqlmag.com/blog/what-best-value-fill-factor-index-fill-factor-and-performance-part-2" target="test_blank"&gt;http://sqlmag.com/blog/what-best-value-fill-factor-index-fill-factor-and-performance-part-2&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://social.msdn.microsoft.com/Forums/sqlserver/en-US/c3e3ba9d-d4df-4ff4-89d0-dbbaec4af5bb/index-name-null?forum=sqldatabaseengine" target="test_blank"&gt;http://social.msdn.microsoft.com/Forums/sqlserver/en-US/c3e3ba9d-d4df-4ff4-89d0-dbbaec4af5bb/index-name-null?forum=sqldatabaseengine&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;ALTER INDEX: &lt;A href="http://technet.microsoft.com/fr-fr/library/ms188388.aspx" target="test_blank"&gt;http://technet.microsoft.com/fr-fr/library/ms188388.aspx&lt;/A&gt;&lt;/LI&gt;
&lt;/UL&gt;</description>
    <pubDate>Fri, 21 Mar 2014 12:09:12 GMT</pubDate>
    <dc:creator>Florent_Guillau</dc:creator>
    <dc:date>2014-03-21T12:09:12Z</dc:date>
    <item>
      <title>Optimizing the database indexes on SQL Server</title>
      <link>https://connect.hyland.com/t5/nuxeo-forum/optimizing-the-database-indexes-on-sql-server/m-p/314316#M1317</link>
      <description>&lt;P&gt;What maintenance tasks should I use on SQL Server to optimize the indexes (rebuild/reorganize) in order for the database performance to be constant?&lt;/P&gt;</description>
      <pubDate>Fri, 21 Mar 2014 11:59:15 GMT</pubDate>
      <guid>https://connect.hyland.com/t5/nuxeo-forum/optimizing-the-database-indexes-on-sql-server/m-p/314316#M1317</guid>
      <dc:creator>Florent_Guillau</dc:creator>
      <dc:date>2014-03-21T11:59:15Z</dc:date>
    </item>
    <item>
      <title>Re: Optimizing the database indexes on SQL Server</title>
      <link>https://connect.hyland.com/t5/nuxeo-forum/optimizing-the-database-indexes-on-sql-server/m-p/314317#M1318</link>
      <description>&lt;P&gt;This is not specific to Nuxeo but a customer had good results with the following:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE&gt;USE [NUXEO]
GO
/****** Object:  StoredProcedure [dbo].[refreshIndexes] ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROC [dbo].[refreshIndexes](@db_name varchar(500))
AS

declare @tmptable TABLE(TableName varchar(500),IndexName varchar(500),AvgPageFragmentation dec(10,2),allowPageLocks bit)
declare @table_name varchar(500)
declare @index_name varchar(500)
declare @frag float
declare @allowPageLocks bit
declare @req nvarchar(max)

set @req = '
 SELECT 
  OBJECT_NAME(DPS.OBJECT_ID,DB_ID(''' + @db_name + ''')) AS TableName
  ,SI.NAME AS IndexName
  ,DPS.AVG_FRAGMENTATION_IN_PERCENT AS AvgPageFragmentation,
  allow_page_locks
 FROM sys.dm_db_index_physical_stats (DB_ID(''' + @db_name + '''), NULL, NULL , NULL, NULL) DPS
 INNER JOIN [' + @db_name + '].sys.indexes SI 
  ON DPS.OBJECT_ID = SI.OBJECT_ID 
  AND DPS.INDEX_ID = SI.INDEX_ID
  AND SI.NAME IS NOT NULL
 ORDER BY DPS.avg_fragmentation_in_percent DESC
 '
insert into @tmptable
exec(@req)
--print @req

DECLARE bigindex cursor LOCAL FAST_FORWARD
FOR
 select *from @tmptable where IndexName is not null

open bigindex
FETCH NEXT from bigindex into @table_name,@index_name,@frag,@allowPageLocks
WHILE (@@FETCH_STATUS=0)
BEGIN
 set @req = ''
-- REORGANIZE FAILED = Indexes for which ALLOW_PAGE_LOCKS is OFF
 if (@frag &amp;gt;=40 or (@allowPageLocks=0 and @frag &amp;gt;=10)) 
  set @req = 'ALTER INDEX [' + @index_name + '] ON [' + @db_name + '].dbo.[' + @table_name + '] REBUILD WITH(FILLFACTOR=80,STATISTICS_NORECOMPUTE=OFF,ONLINE=OFF)' 
 else if (@frag &amp;gt;=10 )
  set @req = 'ALTER INDEX [' + @index_name + '] ON [' + @db_name + '].dbo.[' + @table_name + '] REORGANIZE WITH ( LOB_COMPACTION = ON )'
 if (@req!='')
  exec(@req)
print @req

FETCH NEXT from bigindex into @table_name,@index_name,@frag,@allowPageLocks
END

CLOSE bigindex
DEALLOCATE bigindex
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The following links may also be of interest:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;A href="http://www.brentozar.com/archive/2013/09/index-maintenance-sql-server-rebuild-reorganize/" target="test_blank"&gt;http://www.brentozar.com/archive/2013/09/index-maintenance-sql-server-rebuild-reorganize/&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://sqlmag.com/blog/what-best-value-fill-factor-index-fill-factor-and-performance-part-2" target="test_blank"&gt;http://sqlmag.com/blog/what-best-value-fill-factor-index-fill-factor-and-performance-part-2&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://social.msdn.microsoft.com/Forums/sqlserver/en-US/c3e3ba9d-d4df-4ff4-89d0-dbbaec4af5bb/index-name-null?forum=sqldatabaseengine" target="test_blank"&gt;http://social.msdn.microsoft.com/Forums/sqlserver/en-US/c3e3ba9d-d4df-4ff4-89d0-dbbaec4af5bb/index-name-null?forum=sqldatabaseengine&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;ALTER INDEX: &lt;A href="http://technet.microsoft.com/fr-fr/library/ms188388.aspx" target="test_blank"&gt;http://technet.microsoft.com/fr-fr/library/ms188388.aspx&lt;/A&gt;&lt;/LI&gt;
&lt;/UL&gt;</description>
      <pubDate>Fri, 21 Mar 2014 12:09:12 GMT</pubDate>
      <guid>https://connect.hyland.com/t5/nuxeo-forum/optimizing-the-database-indexes-on-sql-server/m-p/314317#M1318</guid>
      <dc:creator>Florent_Guillau</dc:creator>
      <dc:date>2014-03-21T12:09:12Z</dc:date>
    </item>
  </channel>
</rss>

