[Solved] Selecting Max Value and Corresponding Columns

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 Subqueries (query line: 21): We advise against using subqueries as they are not optimized well by the optimizer. Therefore, it's recommended to join a newly created temporary table that holds the data, which also includes the relevant search index.
  2. Create Optimal Indexes (modified query below): The recommended indexes are an integral part of this optimization effort and should be created before testing the execution duration of the optimized query.
  3. Replace Left Join With Subquery (modified query below): The pattern of inflating the amount of data (using joins) and deflating (using GROUP BY) usually slows down queries. In this case, it can be avoided by moving some of the logic to the SELECT clause, and therefore removing some of the LEFT JOINs. In some cases, this transformation can lead to an obsolete GROUP BY clause, which can also be removed.
  4. Use Numeric Column Types For Numeric Values (query line: 37): Referencing a numeric value (e.g. 3) 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.
Optimal indexes for this query:
ALTER TABLE `clientdetails` ADD INDEX `clientdetails_idx_id` (`id`);
ALTER TABLE `customer` ADD INDEX `customer_idx_switchedoff_companyid` (`switchedoff`,`companyid`);
ALTER TABLE `members` ADD INDEX `members_idx_id` (`id`);
ALTER TABLE `updates` ADD INDEX `updates_idx_date` (`date`);
The optimized query:
SELECT
        customer.id,
        customer.name,
        customer.retainer_value,
        customer.customer_type,
        clientdetails.performance,
        clientdetails.url,
        members.fullname AS acc_manager,
        u.maxdate,
        u.fullname 
    FROM
        customer 
    LEFT JOIN
        clientdetails 
            ON clientdetails.id = customer.id 
    LEFT JOIN
        members 
            ON members.id = customer.consultant_name 
    LEFT JOIN
        (
            SELECT
                updates.clientid,
                Max(updates.`date`) AS MaxDate,
                (SELECT
                    members.fullname 
                FROM
                    members 
                WHERE
                    members.id = updates.consultant LIMIT 1) AS fullname 
            FROM
                updates 
            ORDER BY
                updates.date DESC) u 
                    ON customer.id = u.clientid 
            WHERE
                customer.switchedoff = 'N' 
                AND customer.companyid <> '3'

Related Articles



* original question posted on StackOverflow here.