Break reindexing into chunks

With 20,000 rows in programs, reindexing took around 6.5 minutes. 
With the old code, reindexing failed after 30 minutes with "MySQL has gone away".
When I increased CHUNK_SIZE to 1000 (instead of 100), reindexing took around 24 minutes.