I have the following query. On MySQL 5.7 it takes over 30 mins minutes to run but on MySQL 8 it takes less than 10 seconds. There are around 20k records in students_t, 500 in classes_t and 1100 in groups_t. student_enrolled_classes_t contains 100288 records and 160156 records.There are no foreign keys in place.
SELECT DISTINCT
s.forename
, s.surname
, count(c.id) as 'classes'
, group_concat(DISTINCT g.name) 'groups'
FROM students_t s
, student_enrolled_classes_t c
, student_enrolled_groups_t eg
, groups_t g
WHERE s.id = c.student_id
AND s.id = eg.student_id
AND eg.group_id = g.id
GROUP BY s.id, c.id
ORDER BY s.forename ASC;
The table structures are (generated with Hibernate):
CREATE TABLE classes_t
(id BIGINT auto_increment PRIMARY KEY);
CREATE TABLE groups_t
(id BIGINT auto_increment PRIMARY KEY
,name VARCHAR(255) NULL);
CREATE TABLE student_enrolled_classes_t
(id BIGINT auto_increment PRIMARY KEY
,class_id BIGINT NULL
,student_id BIGINT NULL);
CREATE TABLE student_enrolled_groups_t
(id BIGINT NOT NULL PRIMARY KEY
,group_id BIGINT NULL
,student_id BIGINT NULL);
CREATE TABLE students_t
(id BIGINT auto_increment PRIMARY KEY
,forename VARCHAR(255) NULL
,surname VARCHAR(255) NULL);
Thank you