Link to home
Start Free TrialLog in
Avatar of vensali
vensaliFlag for India

asked on

MySql Qry performance (Ver 5.5.17)

I have the following qry to get the amont & tax where unit_is_pharmacy = 1

Scenario 1

SELECT SUM(Amount),SUM(tax)

FROM

 (
  SELECT unit_code,unit_is_pharmacy,SUM(IF(bad_payer_type = 'A', 0, bad_gross_amt-bad_package_amt - (IF(bah_tran_type = 40103,-1 * IFNULL(ROUND((issd_gross_amt-(issd_gross_amt*issd_gross_amt)/(issd_gross_amt*issd_sale_tax_perc/100+issd_gross_amt)), 3),0),IFNULL(ROUND((issd_gross_amt-(issd_gross_amt*issd_gross_amt)/(issd_gross_amt*issd_sale_tax_perc/100+issd_gross_amt)), 3),0))))) amount,
      SUM(IF(bad_payer_type='N',IF(bah_tran_type = 40103,-1 * ROUND((issd_gross_amt-(issd_gross_amt*issd_gross_amt)/(issd_gross_amt*issd_sale_tax_perc/100+issd_gross_amt)), 3),ROUND((issd_gross_amt-(issd_gross_amt*issd_gross_amt)/(issd_gross_amt*issd_sale_tax_perc/100+issd_gross_amt)), 3)),0)) tax
  FROM
    bill_accounting_details
      JOIN  bill_accounting_header ON bah_rid = bad_bah_rid
      JOIN bill_header ON (bad_bd_bill_rid = bill_rid)
      LEFT JOIN pharmacy_issue_d ON bad_order_rid = issd_rid AND bad_charge_type IN ('D','M')
      LEFT JOIN pharmacy_issue_h ON (issh_rid = issd_issh_rid)
        LEFT JOIN  u_unit drugprocessingunit ON (drugprocessingunit.unit_rid = issh_location_rid AND  unit_is_pharmacy= 1 )
      WHERE bah_effective_date  BETWEEN  '2016-07-01' AND '2016-07-31'
      AND bah_entity_rid = 4       AND bah_category = 'R'  
      GROUP BY  unit_code,unit_is_pharmacy
  ) a  
  WHERE unit_is_pharmacy = 1

This qry runs pretty fast  < 2 sec  


Scenario 2

But the same qry written as below takes ages

SELECT unit_code,unit_is_pharmacy,SUM(IF(bad_payer_type = 'A', 0, bad_gross_amt-bad_package_amt - (IF(bah_tran_type = 40103,-1 * IFNULL(ROUND((issd_gross_amt-(issd_gross_amt*issd_gross_amt)/(issd_gross_amt*issd_sale_tax_perc/100+issd_gross_amt)), 3),0),IFNULL(ROUND((issd_gross_amt-(issd_gross_amt*issd_gross_amt)/(issd_gross_amt*issd_sale_tax_perc/100+issd_gross_amt)), 3),0))))) amount,
      SUM(IF(bad_payer_type='N',IF(bah_tran_type = 40103,-1 * ROUND((issd_gross_amt-(issd_gross_amt*issd_gross_amt)/(issd_gross_amt*issd_sale_tax_perc/100+issd_gross_amt)), 3),ROUND((issd_gross_amt-(issd_gross_amt*issd_gross_amt)/(issd_gross_amt*issd_sale_tax_perc/100+issd_gross_amt)), 3)),0)) tax
  FROM
    bill_accounting_details
      JOIN  bill_accounting_header ON bah_rid = bad_bah_rid
      JOIN bill_header ON (bad_bd_bill_rid = bill_rid)
      LEFT JOIN pharmacy_issue_d ON bad_order_rid = issd_rid AND bad_charge_type IN ('D','M')
      LEFT JOIN pharmacy_issue_h ON (issh_rid = issd_issh_rid)
        LEFT JOIN  u_unit drugprocessingunit ON (drugprocessingunit.unit_rid = issh_location_rid AND  unit_is_pharmacy= 1 )
      WHERE bah_effective_date  BETWEEN  '2016-07-01' AND '2016-07-31'
      AND bah_entity_rid = 4       AND bah_category = 'R'   AND unit_is_pharmacy= 1
      GROUP BY  unit_code,unit_is_pharmacy

Or

SELECT unit_code,unit_is_pharmacy,SUM(IF(bad_payer_type = 'A', 0, bad_gross_amt-bad_package_amt - (IF(bah_tran_type = 40103,-1 * IFNULL(ROUND((issd_gross_amt-(issd_gross_amt*issd_gross_amt)/(issd_gross_amt*issd_sale_tax_perc/100+issd_gross_amt)), 3),0),IFNULL(ROUND((issd_gross_amt-(issd_gross_amt*issd_gross_amt)/(issd_gross_amt*issd_sale_tax_perc/100+issd_gross_amt)), 3),0))))) amount,
      SUM(IF(bad_payer_type='N',IF(bah_tran_type = 40103,-1 * ROUND((issd_gross_amt-(issd_gross_amt*issd_gross_amt)/(issd_gross_amt*issd_sale_tax_perc/100+issd_gross_amt)), 3),ROUND((issd_gross_amt-(issd_gross_amt*issd_gross_amt)/(issd_gross_amt*issd_sale_tax_perc/100+issd_gross_amt)), 3)),0)) tax
  FROM
    bill_accounting_details
      JOIN  bill_accounting_header ON bah_rid = bad_bah_rid
      JOIN bill_header ON (bad_bd_bill_rid = bill_rid)
      LEFT JOIN pharmacy_issue_d ON bad_order_rid = issd_rid AND bad_charge_type IN ('D','M')
      LEFT JOIN pharmacy_issue_h ON (issh_rid = issd_issh_rid)
        JOIN  u_unit drugprocessingunit ON (drugprocessingunit.unit_rid = issh_location_rid AND  unit_is_pharmacy= 1 )
      WHERE bah_effective_date  BETWEEN  '2016-07-01' AND '2016-07-31'
      AND bah_entity_rid = 4       AND bah_category = 'R'  
      GROUP BY  unit_code,unit_is_pharmacy

I ran the explain Select - and the o/p is enclosed for each scenario.  

Request an expert to  explain the  difference in the qry performance.
Explain-Qry-op-mysql.xlsx
ASKER CERTIFIED SOLUTION
Avatar of John Tsioumpris
John Tsioumpris
Flag of Greece image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial