[Solved] Getting Count(*) vs Actual Data has performance implications

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: 42): 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 OFFSET In LIMIT Clause (query line: 23): 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.
  3. Avoid OFFSET In LIMIT Clause (query line: 38): 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.
  4. Avoid OR Conditions By Using UNION (modified query below): In mosts cases, filtering using the OR operator cannot be applied using indexes. A more optimized alternative will be to split the query to two parts combined with a UNION clause, while each query holds one part of the original OR condition.
  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. Use UNION ALL instead of UNION (query line: 26): Always use UNION ALL unless you need to eliminate duplicate records. By using UNION ALL, you'll avoid the expensive distinct operation the database applies when using a UNION clause.
Optimal indexes for this query:
ALTER TABLE `table1` ADD INDEX `table1_idx_type_timestamp` (`type`,`timestamp`);
ALTER TABLE `table1` ADD INDEX `table1_idx_timestamp` (`timestamp`);
The optimized query:
SELECT
        table1_id,
        table1_title,
        table1_desciption,
        table1_timestamp 
    FROM
        ((SELECT
            table1.id AS table1_id,
            table1.title AS table1_title,
            table1.desciption AS table1_desciption,
            table1.timestamp AS table1_timestamp 
        FROM
            table1 
        WHERE
            1 = 1 
            AND table1.type IN (
                'category1', 'category3', 'category2'
            ) 
            AND (
                MATCH (table1.description) AGAINST (' +"apple"' IN BOOLEAN MODE)
            ) 
        ORDER BY
            table1.timestamp DESC LIMIT 0,
            10) 
    UNION
    DISTINCT (SELECT
        table1.id AS table1_id,
        table1.title AS table1_title,
        table1.desciption AS table1_desciption,
        table1.timestamp AS table1_timestamp 
    FROM
        table1 
    WHERE
        1 = 1 
        AND table1.type IN ('category1', 'category3', 'category2') 
        AND (MATCH (table1.title) AGAINST (' +"apple"' IN BOOLEAN MODE)) 
    ORDER BY
        table1.timestamp DESC LIMIT 0,
        10)
) AS union1 
ORDER BY
union1.table1_timestamp DESC LIMIT 0,
10

Related Articles



* original question posted on StackOverflow here.