[Solved] Multi-column and single-column index on the same column

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 OFFSET In LIMIT Clause (query line: 21): OFFSET clauses can be very slow when used with high offsets (e.g. with high page numbers when implementing paging). Instead, use the following \u003ca target\u003d"_blank" href\u003d"http://www.eversql.com/faster-pagination-in-mysql-why-order-by-with-limit-and-offset-is-slow/"\u003eseek method\u003c/a\u003e, which provides better and more stable response rates.
  2. 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.
  3. Avoid Selecting Unnecessary Columns (query line: 7): Avoid selecting all columns with the '*' wildcard, unless you intend to use them all. Selecting redundant columns may result in unnecessary performance degradation.
  4. 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.
  5. Sort and Limit Before Joining (modified query below): In cases where the joins aren't filtering any rows, it's possible to sort and limit the amount of rows using a subquery in the FROM clause, before applying the joins to all other tables.
  6. Use Numeric Column Types For Numeric Values (query line: 14): Referencing a numeric value (e.g. 4494) 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 `shopper_group` ADD INDEX `shopper_group_idx_id` (`id`);
ALTER TABLE `shoppers` ADD INDEX `shoppers_idx_store_id_deleted_created_on` (`store_id`,`deleted`,`created_on`);
ALTER TABLE `shoppers` ADD INDEX `shoppers_idx_created_on` (`created_on`);
The optimized query:
SELECT
        s.*,
        t.*,
        g.* 
    FROM
        (SELECT
            s.*,
            s.id AS s_id,
            s.group_id AS s_group_id 
        FROM
            shoppers s 
        WHERE
            (
                s.store_id = '4494'
            ) 
            AND (
                s.deleted = 0 
                OR s.deleted IS NULL
            ) 
        ORDER BY
            s.created_on DESC LIMIT 10 OFFSET 0) s 
    LEFT JOIN
        shopper_tag t 
            ON s.s_id = t.shopper_id 
    LEFT JOIN
        shopper_group g 
            ON s.s_group_id = g.id 
    WHERE
        1 = 1 
        AND (
            1 = 1
        ) LIMIT 10

Related Articles



* original question posted on StackOverflow here.