Delete from the group contact cache in chunks of 1000. stable
authorDavid Thompson <davet@gnu.org>
Tue, 13 Jan 2015 21:19:03 +0000 (16:19 -0500)
committerDavid Thompson <davet@gnu.org>
Thu, 22 Jan 2015 16:04:02 +0000 (11:04 -0500)
CRM/Contact/BAO/GroupContactCache.php

index be50a15fb4b64cbff433f3d1641f0d72a70ea789..3d612afb8d5cbbde51d1c0faa61b17c5d9df7eab 100644 (file)
@@ -296,11 +296,13 @@ WHERE  id IN ( $groupIDs )
 
     $now         = CRM_Utils_Date::getUTCTime();
     $refreshTime = CRM_Utils_Date::getUTCTime($smartGroupCacheTimeout * 60);
+    // HACK: Hardcoded delete limit.
+    $deleteLimit = 1000;
 
     if (!isset($groupID)) {
       if ($smartGroupCacheTimeout == 0) {
         $query = "
-TRUNCATE civicrm_group_contact_cache
+DELETE FROM civicrm_group_contact_cache LIMIT $deleteLimit
 ";
         $update = "
 UPDATE civicrm_group g
@@ -311,9 +313,14 @@ SET    cache_date = null,
       else {
         $query = "
 DELETE     gc
-FROM       civicrm_group_contact_cache gc
-INNER JOIN civicrm_group g ON g.id = gc.group_id
-WHERE      TIMESTAMPDIFF(MINUTE, g.cache_date, $now) >= $smartGroupCacheTimeout
+FROM       civicrm_group_contact_cache AS gc
+INNER JOIN (
+      SELECT id
+      FROM   civicrm_group
+      WHERE  TIMESTAMPDIFF(MINUTE, cache_date, $now) >= $smartGroupCacheTimeout
+      LIMIT  $deleteLimit
+     ) AS g
+ON g.id=gc.group_id
 ";
         $update = "
 UPDATE civicrm_group g
@@ -335,6 +342,7 @@ AND    refresh_date IS NULL
 DELETE     g
 FROM       civicrm_group_contact_cache g
 WHERE      g.group_id IN ( $groupIDs )
+LIMIT      $deleteLimit
 ";
       $update = "
 UPDATE civicrm_group g
@@ -345,9 +353,10 @@ WHERE  id IN ( $groupIDs )
     }
     else {
       $query = "
-DELETE     g
-FROM       civicrm_group_contact_cache g
-WHERE      g.group_id = %1
+DELETE
+FROM       civicrm_group_contact_cache
+WHERE      group_id = %1
+LIMIT      $deleteLimit
 ";
       $update = "
 UPDATE civicrm_group g
@@ -358,7 +367,10 @@ WHERE  id = %1
       $params = array(1 => array($groupID, 'Integer'));
     }
 
-    CRM_Core_DAO::executeQuery($query, $params);
+    // Nibble at the rows, don't try to delete everything at once!
+    do {
+      $dao = CRM_Core_DAO::executeQuery($query, $params);
+    } while($dao->affectedRows() != 0);
 
     if ($refresh) {
       CRM_Core_DAO::executeQuery($refresh, $params);
@@ -623,4 +635,3 @@ ORDER BY   gc.contact_id, g.children
   }
 
 }
-