[Solved] mysql query performance improve

How to optimize this SQL query?

In case you have your own slow SQL query, you can optimize it automatically here.

For the query above, the following recommendations will be helpful as part of the SQL tuning process.
You'll find 3 sections below:

  1. Description of the steps you can take to speed up the query.
  2. The optimal indexes for this query, which you can copy and create in your database.
  3. An automatically re-written query you can copy and execute in your database.
The optimization process and recommendations:
  1. Avoid Using Date Functions In Conditions (query line: 25): When a function is used directly on an indexed column, the database's optimizer won’t be able to use the index. An alternative way is to use a range condition instead of a function call.
  2. Explicitly ORDER BY After GROUP BY (modified query below): By default, the database sorts all 'GROUP BY col1, col2, ...' queries as if you specified 'ORDER BY col1, col2, ...' in the query as well. If a query includes a GROUP BY clause but you want to avoid the overhead of sorting the result, you can suppress sorting by specifying 'ORDER BY NULL'.
  3. Use Numeric Column Types For Numeric Values (query line: 26): Referencing a numeric value (e.g. 31) as a string in a WHERE clause might result in poor performance. Possible impacts of storing numbers as varchars: more space will be used, you won't be able to perform arithmetic operations, the data won't be self-validated, aggregation functions like SUM won't work, the output may sort incorrectly and more. If the column is numeric, remove the quotes from the constant value, to make sure a numeric comparison is done.
  4. Use Numeric Column Types For Numeric Values (query line: 28): Referencing a numeric value (e.g. 40) as a string in a WHERE clause might result in poor performance. Possible impacts of storing numbers as varchars: more space will be used, you won't be able to perform arithmetic operations, the data won't be self-validated, aggregation functions like SUM won't work, the output may sort incorrectly and more. If the column is numeric, remove the quotes from the constant value, to make sure a numeric comparison is done.
The optimized query:
SELECT
        t.local_branch_revenue,
        t.total_payment,
        (SELECT
            SUM(IF(cpo.real_account_type = 'HQ',
            0,
            cpo.payment_amount)) AS cpo_payment_amount 
        FROM
            customer_payment_options cpo 
        WHERE
            cpo.tran_id = t.id 
            AND cpo.payment_type != 'WALLET' 
            AND cpo.payment_type != 'REWARD_CREDIT' 
        GROUP BY
            cpo.tran_id 
        ORDER BY
            NULL) AS cpo_payment_amount,
        b.ben_firstname,
        b.ben_lastname 
    FROM
        transaction t 
    LEFT JOIN
        beneficiary b 
            ON b.id = t.ben_id 
    WHERE
        t.local_branch_id = '31' 
        AND t.date_added < '2016-04-07 00:00:00' 
        AND source_country_id = '40' 
        AND t.transaction_status != 'CANCELLED'

Related Articles



* original question posted on StackOverflow here.