From ad9597b829a50a88004d89bb6f50331350193b3c Mon Sep 17 00:00:00 2001 From: Lisa Marie Maginnis Date: Mon, 21 Sep 2015 13:19:00 -0400 Subject: [PATCH] Added chunking to GroupContactCache --- CRM/Contact/BAO/GroupContactCache.php | 35 ++++++++++++++++++--------- 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/CRM/Contact/BAO/GroupContactCache.php b/CRM/Contact/BAO/GroupContactCache.php index be50a15fb4..a1ce087b68 100644 --- a/CRM/Contact/BAO/GroupContactCache.php +++ b/CRM/Contact/BAO/GroupContactCache.php @@ -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 @@ -332,9 +339,10 @@ AND refresh_date IS NULL elseif (is_array($groupID)) { $groupIDs = implode(', ', $groupID); $query = " -DELETE g -FROM civicrm_group_contact_cache g -WHERE g.group_id IN ( $groupIDs ) +DELETE +FROM civicrm_group_contact_cache +WHERE 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 } } - -- 2.25.1