0

I have this query which runs forever, its already running for 30 minutes in MySQL Workbench and counting... Is it possible to optimize it?

SELECT `p`.`id`, `ev`.`id`
FROM `participants` as `p`
JOIN `tbl_student` as `tst` on `p`.`First Name` = `tst`.`First Name` AND `p`.`Last Name` = `tst`.`Last Name`
JOIN `tbl_student_session` as `tsts` on `tst`.`id` = `tsts`.`studentId`
JOIN `tbl_courses` as `tc` on `tc`.`sessions` LIKE CONCAT('%',`tsts`.`sessionId`,'%') 
JOIN `events` as `ev` on `tc`.`description` = `ev`.`body`

Tables:

participants: id, firstname, lastname
tbl_student: id, firstname, lastname 
tbl_student_session: sessionId, studentId
tbl_courses: id, sessions, description
events: id, body

Where tbl_courses.sessions containts the sessions in this format: session1, session2, .... ,sessionn etc

UPDATE

I set limit to 10 rows and here is the execution plan:

enter image description here

16
  • 1
    Any query optimization question requires the result of EXPLAIN .. statement for the query. Moreover, LIKE %% in your query is definitely not going to be able to use any index whatsoever. Commented Aug 29, 2019 at 6:54
  • 30 mins? How big is db? Commented Aug 29, 2019 at 6:57
  • 2
    Also there is clear evidence that you are using comma-separated values in a single field (tc.sessions), instead of having an actual relationship table that has one ID per row. This will never be fast until you fix your database design mistakes. Commented Aug 29, 2019 at 7:06
  • 1
    Sorry, there's no way to optimize the query unless the tables are designed in a way that can do the join lookups with indexes. Your insistence on the denormalized design is what's causing the poor performance. Commented Aug 29, 2019 at 7:20
  • 2
    Also it can lead to false results. Imagine your tc.sessions value is 11, and the tsts.sessionId is 123, 11411, 456. Probably not what you want, isn't it? Commented Aug 29, 2019 at 7:33

2 Answers 2

1

Inefficient: ON tc.sessions LIKE CONCAT('%', tsts.sessionId, '%'). Are you trying to put an array into a cell? Don't. Instead, have rows in another table.

But, assuming your current structure, here are some indexes to help with performance:

participants:  INDEX(`Last Name`, `First Name`)
student:  INDEX(`Last Name`, `First Name`)
student_session:  INDEX(studentId)
courses:  INDEX(description)
courses:  INDEX(sessions)
events:  INDEX(body)

(You will eventually see why I removed "tbl_".)

Sign up to request clarification or add additional context in comments.

Comments

0

try this query. if there are any improvements. I believe your session table is quite huge.

SELECT `p`.`id`, `ev`.`id`
FROM `participants` as `p`
JOIN `tbl_student` as `tst` on `p`.`First Name` = `tst`.`First Name` AND `p`.`Last Name` = `tst`.`Last Name`
JOIN `tbl_student_session` as `tsts` on `tst`.`id` = `tsts`.`studentId`
JOIN `events` as `ev` on `tc`.`description` = `ev`.`body`
where exists (
   select 1 from `tbl_courses`
where `sessions` LIKE CONCAT('%',`tsts`.`sessionId`,'%'))

1 Comment

tbl_student_session has ~70000 entries

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.