Details
-
Type: Bug
-
Status: Done/Fixed
-
Priority: Minor
-
Resolution: Fixed/Completed
-
Affects Version/s: 4.6.10
-
Fix Version/s: 4.7
-
Component/s: None
-
Labels:
-
Documentation Required?:None
-
Funding Source:Needs Funding
Description
We have been experiencing unacceptably slow queries on processing refunds
$result = civicrm_api3('Contribution', 'getcount', array(
'sequential' => 1,
'creditnote_id' => $creditNoteId,
));
The query generated has multiple issues (from clause pasted below)
1) it is selecting a large range of fields not just count as it should for a count. I believe this is fixed in contact and probably all api EXCEPT contribution & pledge. Along with the SELECT there are a load of extra rows being pulled in.
2) Unindexed Join that should be replaced with a resolved variable:
INNER JOIN civicrm_option_value cov ON cov.value = civicrm_entity_financial_account.account_relationship AND cov.name = 'Income Account is'
3) Similar joins on payment instrument = should go in favour of pseudoconstant (also contribution status). All these option_value table joins are intrinsically unindexed.
4) Not yet diagnosed. The query is taking 27 seconds and it appears the financial type joins are causing this but I haven't quite gotten to the bottom of it
ANALYSIS
The query returns 9 rows and takes 27 seconds
FROM civicrm_contact contact_a LEFT JOIN civicrm_contribution ON civicrm_contribution.contact_id = contact_a.id
INNER JOIN civicrm_financial_type ON civicrm_contribution.financial_type_id = civicrm_financial_type.id
LEFT JOIN civicrm_entity_financial_account ON civicrm_entity_financial_account.entity_id = civicrm_contribution.financial_type_id AND civicrm_entity_financial_account.entity_table = 'civicrm_financial_type'
INNER JOIN civicrm_financial_account ON civicrm_financial_account.id = civicrm_entity_financial_account.financial_account_id
INNER JOIN civicrm_option_value cov ON cov.value = civicrm_entity_financial_account.account_relationship AND cov.name = 'Income Account is'
INNER JOIN civicrm_option_group cog ON cog.id = cov.option_group_id AND cog.name = 'account_relationship'
LEFT JOIN civicrm_campaign ON civicrm_campaign.id = civicrm_contribution.campaign_id
LEFT JOIN civicrm_contribution_product ON civicrm_contribution_product.contribution_id = civicrm_contribution.id
LEFT JOIN civicrm_product ON civicrm_contribution_product.product_id =civicrm_product.id
LEFT JOIN civicrm_entity_financial_trxn ON (
civicrm_entity_financial_trxn.entity_table = 'civicrm_contribution'
AND civicrm_contribution.id = civicrm_entity_financial_trxn.entity_id )
LEFT JOIN civicrm_financial_trxn ON (
civicrm_entity_financial_trxn.financial_trxn_id = civicrm_financial_trxn.id )
LEFT JOIN civicrm_entity_batch ON ( civicrm_entity_batch.entity_table = 'civicrm_financial_trxn'
AND civicrm_financial_trxn.id = civicrm_entity_batch.entity_id )
LEFT JOIN civicrm_batch ON civicrm_entity_batch.batch_id = civicrm_batch.id
LEFT JOIN civicrm_note ON ( civicrm_note.entity_table = 'civicrm_contribution' AND
civicrm_contribution.id = civicrm_note.entity_id )
LEFT JOIN civicrm_option_group option_group_payment_instrument ON ( option_group_payment_instrument.name = 'payment_instrument') LEFT JOIN civicrm_option_value contribution_payment_instrument ON (civicrm_contribution.payment_instrument_id = contribution_payment_instrument.value
AND option_group_payment_instrument.id = contribution_payment_instrument.option_group_id ) LEFT JOIN civicrm_option_group option_group_contribution_status ON (option_group_contribution_status.name = 'contribution_status')
LEFT JOIN civicrm_option_value contribution_status ON (civicrm_contribution.contribution_status_id = contribution_status.value
AND option_group_contribution_status.id = contribution_status.option_group_id )
WHERE ( civicrm_contribution.is_test = 0 AND LOWER(civicrm_contribution.creditnote_id) = '1' ) AND (contact_a.is_deleted = 0) LIMIT 0, 25