Skip to content

Commit

Permalink
CRM-21677 reduce unnecessary joins in reports
Browse files Browse the repository at this point in the history
  • Loading branch information
eileenmcnaughton committed Mar 28, 2018
1 parent 1f1d0ed commit 185c566
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 49 deletions.
16 changes: 5 additions & 11 deletions CRM/Report/Form/Contact/CurrentEmployer.php
Original file line number Diff line number Diff line change
Expand Up @@ -252,17 +252,11 @@ public function from() {
LEFT JOIN civicrm_relationship {$this->_aliases['civicrm_relationship']}
ON ( {$this->_aliases['civicrm_relationship']}.contact_id_a={$this->_aliases['civicrm_contact']}.id
AND {$this->_aliases['civicrm_relationship']}.contact_id_b={$this->_aliases['civicrm_contact']}.employer_id
AND {$this->_aliases['civicrm_relationship']}.relationship_type_id={$relType})
LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']}
ON ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_address']}.contact_id
AND {$this->_aliases['civicrm_address']}.is_primary = 1 )
LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
ON ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id
AND {$this->_aliases['civicrm_phone']}.is_primary = 1)
LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
ON ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id
AND {$this->_aliases['civicrm_email']}.is_primary = 1) ";
AND {$this->_aliases['civicrm_relationship']}.relationship_type_id={$relType}) ";

$this->joinAddressFromContact();
$this->joinPhoneFromContact();
$this->joinEmailFromContact();
}

public function where() {
Expand Down
3 changes: 0 additions & 3 deletions CRM/Report/Form/Contribute/Bookkeeping.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@
* @copyright CiviCRM LLC (c) 2004-2018
*/
class CRM_Report_Form_Contribute_Bookkeeping extends CRM_Report_Form {
protected $_addressField = FALSE;

protected $_emailField = FALSE;

protected $_summary = NULL;

Expand Down
13 changes: 4 additions & 9 deletions CRM/Report/Form/Event/ParticipantListCount.php
Original file line number Diff line number Diff line change
Expand Up @@ -462,17 +462,12 @@ public function from() {
{$this->_aclFrom}
LEFT JOIN civicrm_contact {$this->_aliases['civicrm_employer']}
ON ({$this->_aliases['civicrm_employer']}.id = {$this->_aliases['civicrm_contact']}.employer_id )
LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']}
ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_address']}.contact_id AND
{$this->_aliases['civicrm_address']}.is_primary = 1
LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
ON ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id AND
{$this->_aliases['civicrm_email']}.is_primary = 1)
LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id AND
{$this->_aliases['civicrm_phone']}.is_primary = 1
LEFT JOIN civicrm_line_item {$this->_aliases['civicrm_line_item']}
ON {$this->_aliases['civicrm_line_item']}.entity_table = 'civicrm_participant' AND {$this->_aliases['civicrm_participant']}.id ={$this->_aliases['civicrm_line_item']}.entity_id";

$this->joinAddressFromContact();
$this->joinPhoneFromContact();
$this->joinEmailFromContact();
}

public function storeWhereHavingClauseArray() {
Expand Down
14 changes: 5 additions & 9 deletions CRM/Report/Form/Event/ParticipantListing.php
Original file line number Diff line number Diff line change
Expand Up @@ -513,16 +513,12 @@ public function from() {
LEFT JOIN civicrm_contact {$this->_aliases['civicrm_contact']}
ON ({$this->_aliases['civicrm_participant']}.contact_id = {$this->_aliases['civicrm_contact']}.id )
{$this->_aclFrom}
LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']}
ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_address']}.contact_id AND
{$this->_aliases['civicrm_address']}.is_primary = 1
LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']}
ON ({$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id AND
{$this->_aliases['civicrm_email']}.is_primary = 1)
LEFT JOIN civicrm_phone {$this->_aliases['civicrm_phone']}
ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_phone']}.contact_id AND
{$this->_aliases['civicrm_phone']}.is_primary = 1
";

$this->joinAddressFromContact();
$this->joinPhoneFromContact();
$this->joinEmailFromContact();

if ($this->_contribField) {
$this->_from .= "
LEFT JOIN civicrm_participant_payment pp
Expand Down
20 changes: 3 additions & 17 deletions CRM/Report/Form/Membership/Summary.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ public function preProcess() {
* Generate select clause.
*/
public function select() {
// @todo remove this in favour of just using parent.
$select = array();
$this->_columnHeaders = array();
foreach ($this->_columns as $tableName => $table) {
Expand All @@ -156,14 +157,6 @@ public function select() {
if (!empty($field['required']) ||
!empty($this->_params['fields'][$fieldName])
) {
// to include optional columns address and email, only if checked
if ($tableName == 'civicrm_address') {
$this->_addressField = TRUE;
$this->_emailField = TRUE;
}
elseif ($tableName == 'civicrm_email') {
$this->_emailField = TRUE;
}
$select[] = "{$field['dbAlias']} as {$tableName}_{$fieldName}";
$this->_columnHeaders["{$tableName}_{$fieldName}"]['type'] = $field['type'];
$this->_columnHeaders["{$tableName}_{$fieldName}"]['title'] = $field['title'];
Expand All @@ -189,15 +182,8 @@ public function from() {
LEFT JOIN civicrm_contribution {$this->_aliases['civicrm_contribution']}
ON {$this->_aliases['civicrm_membership']}.contact_id = {$this->_aliases['civicrm_contribution']}.contact_id
";
// Include address table if address column is to be included.
if ($this->_addressField) {
$this->_from .= "LEFT JOIN civicrm_address {$this->_aliases['civicrm_address']} ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_address']}.contact_id AND {$this->_aliases['civicrm_address']}.is_primary = 1\n";
}

// Include email table if email column is to be included.
if ($this->_emailField) {
$this->_from .= "LEFT JOIN civicrm_email {$this->_aliases['civicrm_email']} ON {$this->_aliases['civicrm_contact']}.id = {$this->_aliases['civicrm_email']}.contact_id AND {$this->_aliases['civicrm_email']}.is_primary = 1\n";
}
$this->joinAddressFromContact();
$this->joinEmailFromContact();
}

/**
Expand Down

0 comments on commit 185c566

Please sign in to comment.