[Solved] MySQL Query Redesign

EverSQL Database Performance Knowledge Base

MySQL Query Redesign

Database type:

I have the following query selecting everything from the notes table where the input(ex: bob) is not in the orders table.

SELECT * FROM `notes` WHERE notes.customer_email NOT IN 
(SELECT customers_email_address FROM orders) 
AND ((customer_phone LIKE '%bob%') 
OR (customer_name LIKE '%bob%') 
OR (customer_email LIKE '%bob%')) 
AND customers_id IS NULL 
GROUP BY `customer_email` 
ORDER BY `customer_name` 
DESC LIMIT 50

This fat boy of a query is taking ~80 seconds on my dev machine and ~7 seconds on the live server.

Two questions:

  1. What did I do wrong with this query? (I need to learn from my mistakes)
  2. How can I improve this query?

How to optimize this SQL query?

The following recommendations will help you in your 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 LIKE Searches With Leading Wildcard (query line: 18): The database will not use an index when using like searches with a leading wildcard (e.g. '%bob%'). Although it's not always a satisfactory solution, please consider using prefix-match LIKE patterns (e.g. 'TERM%').
  2. Avoid LIKE Searches With Leading Wildcard (query line: 21): The database will not use an index when using like searches with a leading wildcard (e.g. '%bob%'). Although it's not always a satisfactory solution, please consider using prefix-match LIKE patterns (e.g. 'TERM%').
  3. Avoid LIKE Searches With Leading Wildcard (query line: 24): The database will not use an index when using like searches with a leading wildcard (e.g. '%bob%'). Although it's not always a satisfactory solution, please consider using prefix-match LIKE patterns (e.g. 'TERM%').
  4. Avoid Selecting Unnecessary Columns (query line: 2): Avoid selecting all columns with the '*' wildcard, unless you intend to use them all. Selecting redundant columns may result in unnecessary performance degradation.
  5. 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.
  6. Replace In Subquery With Correlated Exists (modified query below): In many cases, an EXISTS subquery with a correlated condition will perform better than a non correlated IN subquery.
Optimal indexes for this query:
ALTER TABLE `notes` ADD INDEX `notes_idx_customers_id_customer_email` (`customers_id`,`customer_email`);
ALTER TABLE `orders` ADD INDEX `orders_idx_customers_address` (`customers_email_address`);
The optimized query:
SELECT
        * 
    FROM
        `notes` 
    WHERE
        NOT EXISTS (
            SELECT
                1 
            FROM
                orders 
            WHERE
                (
                    notes.customer_email = orders.customers_email_address
                )
        ) 
        AND (
            (
                `notes`.customer_phone LIKE '%bob%'
            ) 
            OR (
                `notes`.customer_name LIKE '%bob%'
            ) 
            OR (
                notes.customer_email LIKE '%bob%'
            )
        ) 
        AND `notes`.customers_id IS NULL 
    GROUP BY
        notes.`customer_email` 
    ORDER BY
        `notes`.`customer_name` DESC LIMIT 50

Related Articles



* original question posted on StackOverflow here.