nathanharcup
MIS
Hi All,
I hope someone can help me with this. I have a strange issue in that if i re-build the index the fragmentation does not change (internal fragmentation)
I have both unique clustered indexes and non unique non clustered indexes on each table.
Runing;
ALTER INDEX ALL ON 'tablename' REBUILD'
does re-build the non clustered indexes but seems to ignor the clustered ones.
running;
CREATE UNIQUE CLUSTERED INDEX [indexname] ON [dbo].[tablename] ([columns] ASC)
WITH (FILLFACTOR = 1,DROP_EXISTING = ON)
affects the clustered indexes (not a great deal) but then really fragments the non clustered indexes.
I am trying to build a statment that will for the clustered indexes use the create statment and for the non clustered indexes use the alter statment.
Having problems (i think with sub cursors) but a a novice really
This is what I have so far, any help would be very much appreciated;
USE DB
DECLARE @db_id SMALLINT;
DECLARE @object_id INT;
declare @indexname varchar(2000);
declare @type varchar(2000);
declare @UNIQUE int;
declare @UNIQUEVALUE varchar(2000);
declare @OBJECTNAME varchar(2000);
DECLARE @SQL VARCHAR (2000);
DECLARE @object_id1 int;
DECLARE @INDEXID INT;
DECLARE @INDEXIDSYSINDEXES INT;
DECLARE @COLID INT;
SET @db_id = DB_ID(N'DB');
SET @object_id = OBJECT_ID(N'');
declare curCursor cursor for
select [name], [type_desc],[is_unique],[object_id], [INDEX_ID]
from sys.indexes
where object_id in (select id from sysobjects where xtype = 'u')
open curCursor
fetch next from curCursor into @indexname, @type, @UNIQUE, @object_id1,@INDEXID
while (@@FETCH_STATUS = 0)
begin
set @OBJECTNAME = (select [name] from sysobjects where xtype = 'u' and id = @object_id1)
declare curCursor1 cursor for
SELECT [COLID] FROM sysindexkeys WHERE [INDID]=@INDEXID AND ID = @object_id1
open curCursor1
fetch next from curCursor1 into @COLID
while (@@FETCH_STATUS = 0)
begin
IF @db_id IS NULL
BEGIN;
PRINT N'Invalid database';
END;
IF @UNIQUE =1 SET @UNIQUEVALUE = 'UNIQUE'
IF @UNIQUE =0 SET @UNIQUEVALUE = ''
BEGIN;
if @UNIQUEVALUE = 'Unique' SELECT @SQL = 'CREATE '+ @UNIQUEVALUE + ' INDEX ' + @TYPE+ ' INDEX ' + ' ' + '['+@INDEXNAME +']'+ ' ON '+@OBJECTNAME+' WITH (FILLFACTOR = 1,DROP_EXISTING = ON)'
if @UNIQUEVALUE = '' SELECT @SQL = 'ALTER INDEX '+ @INDEXNAME + ' ON '+@OBJECTNAME+' REBUILD'
PRINT @SQL
END;
fetch next from curCursor1 into @COLID
fetch next from curCursor into @indexname,@type, @UNIQUE,@object_id1,@INDEXID
end
close curCursor
deallocate curCursor
close curCursor1
deallocate curCursor1
END;
I hope someone can help me with this. I have a strange issue in that if i re-build the index the fragmentation does not change (internal fragmentation)
I have both unique clustered indexes and non unique non clustered indexes on each table.
Runing;
ALTER INDEX ALL ON 'tablename' REBUILD'
does re-build the non clustered indexes but seems to ignor the clustered ones.
running;
CREATE UNIQUE CLUSTERED INDEX [indexname] ON [dbo].[tablename] ([columns] ASC)
WITH (FILLFACTOR = 1,DROP_EXISTING = ON)
affects the clustered indexes (not a great deal) but then really fragments the non clustered indexes.
I am trying to build a statment that will for the clustered indexes use the create statment and for the non clustered indexes use the alter statment.
Having problems (i think with sub cursors) but a a novice really
This is what I have so far, any help would be very much appreciated;
USE DB
DECLARE @db_id SMALLINT;
DECLARE @object_id INT;
declare @indexname varchar(2000);
declare @type varchar(2000);
declare @UNIQUE int;
declare @UNIQUEVALUE varchar(2000);
declare @OBJECTNAME varchar(2000);
DECLARE @SQL VARCHAR (2000);
DECLARE @object_id1 int;
DECLARE @INDEXID INT;
DECLARE @INDEXIDSYSINDEXES INT;
DECLARE @COLID INT;
SET @db_id = DB_ID(N'DB');
SET @object_id = OBJECT_ID(N'');
declare curCursor cursor for
select [name], [type_desc],[is_unique],[object_id], [INDEX_ID]
from sys.indexes
where object_id in (select id from sysobjects where xtype = 'u')
open curCursor
fetch next from curCursor into @indexname, @type, @UNIQUE, @object_id1,@INDEXID
while (@@FETCH_STATUS = 0)
begin
set @OBJECTNAME = (select [name] from sysobjects where xtype = 'u' and id = @object_id1)
declare curCursor1 cursor for
SELECT [COLID] FROM sysindexkeys WHERE [INDID]=@INDEXID AND ID = @object_id1
open curCursor1
fetch next from curCursor1 into @COLID
while (@@FETCH_STATUS = 0)
begin
IF @db_id IS NULL
BEGIN;
PRINT N'Invalid database';
END;
IF @UNIQUE =1 SET @UNIQUEVALUE = 'UNIQUE'
IF @UNIQUE =0 SET @UNIQUEVALUE = ''
BEGIN;
if @UNIQUEVALUE = 'Unique' SELECT @SQL = 'CREATE '+ @UNIQUEVALUE + ' INDEX ' + @TYPE+ ' INDEX ' + ' ' + '['+@INDEXNAME +']'+ ' ON '+@OBJECTNAME+' WITH (FILLFACTOR = 1,DROP_EXISTING = ON)'
if @UNIQUEVALUE = '' SELECT @SQL = 'ALTER INDEX '+ @INDEXNAME + ' ON '+@OBJECTNAME+' REBUILD'
PRINT @SQL
END;
fetch next from curCursor1 into @COLID
fetch next from curCursor into @indexname,@type, @UNIQUE,@object_id1,@INDEXID
end
close curCursor
deallocate curCursor
close curCursor1
deallocate curCursor1
END;