From bbde790f4aed2ecfe9921ca1b506c7a2d1284258 Mon Sep 17 00:00:00 2001 From: Ravish Nair Date: Tue, 3 Sep 2013 20:24:38 +0530 Subject: [PATCH] -- CRM-13321 ---------------------------------------- * CRM-13321: Performance Issue counting Contributions Contact summary http://issues.civicrm.org/jira/browse/CRM-13321 --- CRM/Contribute/BAO/Contribution.php | 36 ++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/CRM/Contribute/BAO/Contribution.php b/CRM/Contribute/BAO/Contribution.php index d6decfbc42..bd30f5dfb0 100644 --- a/CRM/Contribute/BAO/Contribution.php +++ b/CRM/Contribute/BAO/Contribution.php @@ -1691,22 +1691,36 @@ LEFT JOIN civicrm_contribution contribution ON ( componentPayment.contribution_ return 0; } - $fromClause = "civicrm_contribution contribution"; - $whereConditions = array("contribution.contact_id = {$contactId}"); + $contactContributionsSQL = " + SELECT contribution.id AS id + FROM civicrm_contribution contribution + WHERE contribution.is_test = 0 AND contribution.contact_id = {$contactId} "; + + $contactHonoreeContributionsSQL = " + SELECT contribution.id + FROM civicrm_contribution contribution + WHERE contribution.is_test = 0 AND contribution.honor_contact_id = {$contactId} "; + + $contactSoftCreditContributionsSQL = " + SELECT contribution.id + FROM civicrm_contribution contribution INNER JOIN civicrm_contribution_soft softContribution + ON ( contribution.id = softContribution.contribution_id ) + WHERE contribution.is_test = 0 AND softContribution.contact_id = {$contactId} "; + $query = "SELECT count( x.id ) count FROM ( "; + $query .= $contactContributionsSQL; + if ($includeSoftCredit) { - $fromClause .= " LEFT JOIN civicrm_contribution_soft softContribution - ON ( contribution.id = softContribution.contribution_id )"; - $whereConditions[] = " softContribution.contact_id = {$contactId}"; + $query .= " UNION "; + $query .= $contactSoftCreditContributionsSQL; } + if ($includeHonoree) { - $whereConditions[] = " contribution.honor_contact_id = {$contactId}"; + $query .= " UNION "; + $query .= $contactHonoreeContributionsSQL; } - $whereClause = " contribution.is_test = 0 AND ( " . implode(' OR ', $whereConditions) . " )"; - $query = " - SELECT count( contribution.id ) count - FROM {$fromClause} - WHERE {$whereClause}"; + $query .= ") x"; + CRM_Core_Error::debug( '$query', $query ); return CRM_Core_DAO::singleValueQuery($query); } -- 2.25.1