Optimize MySQL query for group_concat function

jskidd3
SELECT SQL_NO_CACHE link.stop, stop.common_name, locality.name, stop.bearing, stop.latitude, stop.longitude
FROM service
JOIN pattern ON pattern.service = service.code
JOIN link ON link.section = pattern.section
JOIN naptan.stop ON stop.atco_code = link.stop
JOIN naptan.locality ON locality.code = stop.nptg_locality_ref
GROUP BY link.stop

The above query takes roughly 800ms - 1000ms to run.

If I append a group_concat statement the query then takes 8 - 10 seconds:

SELECT SQL_NO_CACHE link.stop, link.stop, stop.common_name, locality.name, stop.bearing, stop.latitude, stop.longitude, group_concat(service.line) lines

How can I change this query so that it runs in less than 2 seconds with the group_concat statement?

SQL Fiddle: http://sqlfiddle.com/#!9/414fe

EXPLAIN statements for both queries: http://i.imgur.com/qrURgzV.png

Gordon Linoff

How long does this query take?

SELECT p.section, GROUP_CONCAT(s.line)
FROM pattern p join
     service s
     ON p.service = s.code
GROUP BY p.section

I am thinking that you can do the group_concat() in a subquery, so the outer query does not need an aggregation. This can speed queries when there is one table in the subquery. In your case, there are two.

The final results would be something like:

link.section = pattern.section

SELECT SQL_NO_CACHE . . .,
       (SELECT GROUP_CONCAT(s.line)
        FROM pattern p join
             service s
             ON p.service = s.code
        WHERE p.section = link.section
       ) as lines
FROM link JOIN
     naptan.stop
     ON stop.atco_code = link.stop JOIN
     naptan.locality
     ON locality.code = stop.nptg_locality_ref;

For this query, you want the following additional indexes: pattern(section, service) and service(code, line).

I don't know if this will work, but it is worth a try.

Note: this is assuming that you really don't need the group by for the rest of the columns.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Mysql GROUP_CONCAT and IN query

From Dev

mySQL GROUP_CONCAT - Query

From Dev

Optimize GROUP_CONCAT in SQL on MySQL

From Dev

Mysql JOIN with GROUP_CONCAT in a complex query

From Dev

Using MYSQL GROUP_CONCAT with sub query

From Dev

MySQL GROUP_CONCAT Query Excluding Records

From Dev

Mysql query using IN with group_concat result

From Dev

mysql query join group_concat

From Dev

Using MYSQL GROUP_CONCAT with sub query

From Dev

Mysql group_concat query gives error

From Dev

Select query with GROUP_CONCAT in mysql

From Dev

Using Mysql GROUP_CONCAT in JOIN Query

From Dev

Aggregate function GROUP_CONCAT(expr) in MySQL

From Dev

Aggregate function GROUP_CONCAT(expr) in MySQL

From Dev

Optimize MySQL range query with group by

From Dev

how to use select query in a Group_concat sub query in mysql

From Dev

Invalid use of group function (group_concat and MySQL)

From Dev

Error when I used GROUP_CONCAT on mysql query

From Dev

Issue with GROUP_CONCAT AND ORDER BY FIELD in MySQL query

From Dev

Spark SQL replacement for MySQL's GROUP_CONCAT aggregate function

From Dev

MySQL Group_Concat Not In

From Dev

How to optimize mysql query with COUNT(*) and GROUP BY

From Dev

MySQL: SUM/MAX/MIN GROUP BY query optimize

From Dev

Mysql group_concat of repeated keys and count of repetition of multiple columns in 1 query ( Query Optimization )

From Dev

Group_concat - query builder

From Dev

MySQL's Group_Concat function miss the nulls. How can group the rows including NULLs.

From Dev

MySQL: update with join using GROUP_CONCAT: ERROR 1111 (HY000): Invalid use of group function

From Dev

Optimize query with TO CHAR function

From Dev

MySQL optimize huge query

Related Related

HotTag

Archive