Hello Everyone,I am stumped that adding just an index to a column changed the data. I am using a while loop code below to recursively flatten out the hierarchy in the data. declare @lastcount intdeclare @lastcycle intSelect HierarchyData.Name, HierarchyData.MemberName, 0 as [Cycle] INTO #listFROM HierarchyDataSET @lastcount = @@rowcountSET @lastcycle = 0while @lastcount > 0BEGIN INSERT INTO #list SELECT Members.Name, Child.MemberName as [MemberName], @lastcycle+1 as [Cycle] FROM #list Members JOIN HierarchyData Child ON Members.MemberName = Child.Name LEFT JOIN #list cycletest ON Members.Name = cycletest.Name AND Child.MemberName = cycletest.Membername WHERE Members.Cycle = @lastcycle AND NOT (Members.Name = Child.MemberName) AND cycletest.Name is null SET @lastcount = @@rowcount SET @lastcycle = @lastcycle + 1END
This works for us and gives us the results that we need. However it runs for a very long time. So we thought of adding indexes to the columns.I added the indexes on the temp table #list just before putting it through the while loop like shown below. create index idx_list on #list(Name)create index idx_listmem on #list(MemberName)
It runs very fast but all the data do not get populated. Looks like adding a index to the temp table changes the way the script runs.Any ideas suggestions would be greatly appreciated. Thanksedyl