=== etc/upgrade/shrink_cgm_table.pl
==================================================================
--- etc/upgrade/shrink_cgm_table.pl	(revision 14666)
+++ etc/upgrade/shrink_cgm_table.pl	(local)
@@ -43,7 +43,8 @@
     ENTRYAGGREGATOR => 'AND',
 );
 
-while ( my $rec = $cgms->Next ) {
+FetchNext( $cgms, 'init' );
+while ( my $rec = FetchNext( $cgms ) ) {
     $RT::Handle->BeginTransaction;
     my ($status) = $rec->Delete;
     unless ($status) {
@@ -53,3 +54,19 @@
     $RT::Handle->Commit;
 }
 
+use constant PAGE_SIZE => 1000;
+sub FetchNext {
+    my ($objs, $init) = @_;
+    if ( $init ) {
+        $objs->RowsPerPage( PAGE_SIZE );
+        $objs->FirstPage;
+        return;
+    }
+
+    my $obj = $objs->Next;
+    return $obj if $obj;
+    $objs->RedoSearch;
+    $objs->FirstPage;
+    return $objs->Next;
+}
+
