[Solved] Does mutliple functions in GROUP BY like concat or cast or dateformat affect the query time?

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 Calling Functions With Indexed Columns (query line: 68): When a function is used directly on an indexed column, the database's optimizer won’t be able to use the index. For example, if the column `id` is indexed, the index won’t be used as it’s wrapped with the function `concat`. If you can’t find an alternative condition that won’t use a function call, a possible solution is to store the required value in a new indexed column.
  2. Avoid Calling Functions With Indexed Columns (query line: 68): When a function is used directly on an indexed column, the database's optimizer won’t be able to use the index. For example, if the column `name` is indexed, the index won’t be used as it’s wrapped with the function `concat`. If you can’t find an alternative condition that won’t use a function call, a possible solution is to store the required value in a new indexed column.
  3. Avoid Calling Functions With Indexed Columns (query line: 68): When a function is used directly on an indexed column, the database's optimizer won’t be able to use the index. For example, if the column `createddate` is indexed, the index won’t be used as it’s wrapped with the function `concat`. If you can’t find an alternative condition that won’t use a function call, a possible solution is to store the required value in a new indexed column.
  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. 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'.
Optimal indexes for this query:
ALTER TABLE `TABLE1` ADD INDEX `table1_idx_bltid` (`bltid`);
ALTER TABLE `TABLE2` ADD INDEX `table2_idx_id_status` (`id`,`status`);
ALTER TABLE `TABLE3` ADD INDEX `table3_idx_schdlid` (`schdlid`);
ALTER TABLE `TABLE5` ADD INDEX `table5_idx_id` (`id`);
ALTER TABLE `TABLE6` ADD INDEX `table6_idx_id` (`id`);
ALTER TABLE `TABLE7` ADD INDEX `table7_idx_id` (`id`);
The optimized query:
SELECT
        sum(this_.a + this_.b + this_.c + this_.d + this_.e + this_.f + this_.g + this_.h + this_.i + this_.j) AS sent,
        sum(this_.a + this_.b + this_.c + this_.d + this_.e) AS delivered,
        d2_.name AS y2_,
        d2_.id AS y3_,
        concat(CAST(b4_.id AS char),
        '.',
        c1_.name,
        '.',
        cs5_.name,
        '_',
        date_format(b4_.createddate,
        '%Y-%b-%d %H:%i:%s')) AS allias_concat,
        b4_.id AS y5_,
        this_.year AS y6_,
        this_.month AS y7_,
        this_.day AS y8_,
        this_.weekday AS y9_,
        this_.weekofmonth AS y10_,
        this_.bltid AS y11_,
        b4_.id AS y12_,
        c1_.name AS y13_,
        cs5_.name AS y14_,
        b4_.createddate AS y15_,
        cs5_.subject AS y16_,
        d2_.name AS y17_ 
    FROM
        TABLE1 this_ 
    INNER JOIN
        TABLE2 c1_ 
            ON this_.cgnid = c1_.id 
    INNER JOIN
        TABLE3 b4_ 
            ON this_.bltid = b4_.id 
    INNER JOIN
        TABLE4 csc6_ 
            ON b4_.schdlid = csc6_.id 
    INNER JOIN
        TABLE5 cs5_ 
            ON this_.constid = cs5_.id 
    LEFT OUTER JOIN
        TABLE6 f3_ 
            ON this_.fid = f3_.id 
    INNER JOIN
        TABLE7 d2_ 
            ON this_.dptid = d2_.id 
    WHERE
        (
            f3_.name <> 'TRASH' 
            OR c1_.fid = 0
        ) 
        AND c1_.status <> 'K' 
        AND d2_.id IN (
            1
        ) 
        AND (
            (
                b4_.createddate BETWEEN '2015-02-01 10:30:00' AND '2015-05-13 10:29:59' 
                AND csc6_.isrealtime = 'N'
            ) 
            OR (
                csc6_.isrealtime = 'Y' 
                AND this_.bltdate BETWEEN '2015-02-01 10:30:00' AND '2015-05-13 10:29:59'
            )
        ) 
    GROUP BY
        d2_.id,
        concat(CAST(b4_.id AS char),
        '.',
        c1_.name,
        '.',
        cs5_.name,
        '_',
        date_format(b4_.createddate,
        '%Y-%b-%d %H:%i:%s')),
        b4_.id,
        this_.year,
        this_.month,
        this_.day,
        this_.bltid 
    ORDER BY
        NULL LIMIT 20001

Related Articles



* original question posted on StackOverflow here.