From eaa7e56eceaacd8fabec236958ed1f397c8fa33d Mon Sep 17 00:00:00 2001 From: Seamus Lee Date: Fri, 11 Oct 2019 02:50:57 +1100 Subject: [PATCH] Improve ACL contact cache building performance by splitting insert and select --- CRM/Contact/BAO/Contact/Permission.php | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/CRM/Contact/BAO/Contact/Permission.php b/CRM/Contact/BAO/Contact/Permission.php index 80cbe37847b..f325915b933 100644 --- a/CRM/Contact/BAO/Contact/Permission.php +++ b/CRM/Contact/BAO/Contact/Permission.php @@ -242,14 +242,18 @@ public static function cache($userID, $type = CRM_Core_Permission::VIEW, $force $permission = CRM_ACL_API::whereClause($type, $tables, $whereTables, $userID, FALSE, FALSE, TRUE); $from = CRM_Contact_BAO_Query::fromClause($whereTables); - CRM_Core_DAO::executeQuery(" -INSERT INTO civicrm_acl_contact_cache ( user_id, contact_id, operation ) -SELECT DISTINCT $userID as user_id, contact_a.id as contact_id, '{$operation}' as operation + $contats = CRM_Core_DAO::excuteQuery("SELECT DISTINCT $userID as user_id, contact_a.id as contact_id $from LEFT JOIN civicrm_acl_contact_cache ac ON ac.user_id = $userID AND ac.contact_id = contact_a.id AND ac.operation = '{$operation}' WHERE $permission -AND ac.user_id IS NULL -"); +AND ac.user_id IS NULL"); + while ($contacts->fetch()) { + CRM_Core_DAO::executeQuery("INSERT INTO civicrm_acl_contact_cache (user_id, contact_id, operation) VALUES (%1, %2, %3)", [ + 1 => [$contacts->user_id, 'Positive'], + 2 => [$contacts->contact_id, 'Positive'], + 3 => [$operation, 'String'], + ]); + } // Add in a row for the logged in contact. Do not try to combine with the above query or an ugly OR will appear in // the permission clause.