From 56881b50a012f9c524dbc85f096bf932cdd75f38 Mon Sep 17 00:00:00 2001 From: Civiware Solutions Date: Thu, 10 Jan 2019 22:33:22 +0000 Subject: [PATCH] optmized query --- CRM/ConstituentsOnly/BAO/ConstituentsOnly.php | 41 +++++++++++++------ 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/CRM/ConstituentsOnly/BAO/ConstituentsOnly.php b/CRM/ConstituentsOnly/BAO/ConstituentsOnly.php index cfb6655..0b86203 100644 --- a/CRM/ConstituentsOnly/BAO/ConstituentsOnly.php +++ b/CRM/ConstituentsOnly/BAO/ConstituentsOnly.php @@ -61,19 +61,37 @@ class CRM_ConstituentsOnly_BAO_ConstituentsOnly { */ public static function getConstituentSql($contactIds) { return " - SELECT MAX(contact_a.id) AS contact_id, cc.id AS og_contact_id + SELECT contact_id, MAX(og_contact_id) AS og_contact_id FROM ( + SELECT MAX(contact_a.id) AS contact_id, cc.id AS og_contact_id FROM civicrm_contact contact_a - INNER JOIN civicrm_relationship cr - ON (cr.contact_id_a = contact_a.id OR cr.contact_id_b = contact_a.id) - AND contact_a.is_deleted = 0 - AND (contact_a.do_not_trade = 0 OR contact_a.do_not_trade IS NULL) - AND cr.is_active = 1 - INNER JOIN civicrm_contact cc - ON (cr.contact_id_a = cc.id OR cr.contact_id_b = cc.id) - AND contact_a.id <> cc.id - AND cc.id IN (" . implode(',', $contactIds) . ") - AND cc.do_not_trade = 1 + INNER JOIN civicrm_relationship cr + ON (cr.contact_id_a = contact_a.id) + AND contact_a.is_deleted = 0 + AND (contact_a.do_not_trade = 0 OR contact_a.do_not_trade IS NULL) + AND cr.is_active = 1 + INNER JOIN civicrm_contact cc + ON (cr.contact_id_b = cc.id) + AND contact_a.id <> cc.id + AND cc.id IN (" . implode(',', $contactIds) . ") + AND cc.do_not_trade = 1 GROUP BY cc.id + + UNION + + SELECT MAX(contact_a.id) AS contact_id, cc.id AS og_contact_id + FROM civicrm_contact contact_a + INNER JOIN civicrm_relationship cr + ON (cr.contact_id_b = contact_a.id) + AND contact_a.is_deleted = 0 + AND (contact_a.do_not_trade = 0 OR contact_a.do_not_trade IS NULL) + AND cr.is_active = 1 + INNER JOIN civicrm_contact cc + ON (cr.contact_id_a = cc.id) + AND contact_a.id <> cc.id + AND cc.id IN (" . implode(',', $contactIds) . ") + AND cc.do_not_trade = 1 + GROUP BY cc.id + ) AS temp GROUP BY contact_id "; } @@ -147,7 +165,6 @@ class CRM_ConstituentsOnly_BAO_ConstituentsOnly { public static function updateSearchRows($rows, $headers) { $contactIds = array_keys($rows); $sql = self::getConstituentSql($contactIds); - $result = CRM_Core_DAO::executeQuery($sql); $relContactIds = [];