Skip to content

Commit

Permalink
Refs #41980, Replace contactIDsSQL in group include searches with Gro…
Browse files Browse the repository at this point in the history
…upContactCache to optimize performance.
  • Loading branch information
apple843119 committed Dec 19, 2024
1 parent c82e179 commit 92393dc
Showing 1 changed file with 39 additions and 5 deletions.
44 changes: 39 additions & 5 deletions CRM/Contact/Form/Search/Custom/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -322,14 +322,48 @@ function from() {
if (in_array($values, $smartGroup)) {

$contactIdField = "contact_a.id";
$ssId = CRM_Utils_Array::key($values, $smartGroup);
$joinTable = "contact_a";
$groupIds = $values;
$group = new CRM_Contact_DAO_Group();
$group->id = $values;
$group->find(TRUE);
$smartSql = <<<EOT
SELECT contact_a.id
FROM civicrm_contact contact_a
WHERE
EOT;

if (!$this->_smartGroupCache || $group->cache_date == NULL) {
if (!empty($group->cache_date)) {
// refs #31308, do not refresh smart group too often
$config = CRM_Core_Config::singleton();
$minimalCacheTime = CRM_Contact_BAO_GroupContactCache::SMARTGROUP_CACHE_TIMEOUT_MINIMAL;
if (CRM_REQUEST_TIME - $minimalCacheTime > strtotime($group->cache_date)) {
CRM_Contact_BAO_GroupContactCache::load($group);
}
}
else {
CRM_Contact_BAO_GroupContactCache::load($group);
}
}

$smartSql = CRM_Contact_BAO_SavedSearch::contactIDsSQL($ssId);
if (strstr($smartSql, "contact_a.contact_id")) {
$contactIdField = "contact_a.contact_id";
if (!empty($groupIds)) {
if ($tableAlias == NULL) {
$alias = '`cgcc`';
}
else {
$alias = $tableAlias;
}
}

$smartSql .= " AND $contactIdField NOT IN (
$whereSql = <<<EOT
EXISTS (
SELECT 1 FROM civicrm_group_contact_cache {$alias}
WHERE {$alias}.contact_id = {$joinTable}.id AND {$alias}.group_id IN ({$groupIds})
)
EOT;

$smartSql .= $whereSql." AND $contactIdField NOT IN (
SELECT contact_id FROM civicrm_group_contact
WHERE civicrm_group_contact.group_id = {$values} AND civicrm_group_contact.status = 'Removed')";

Expand Down

0 comments on commit 92393dc

Please sign in to comment.