I've been struggling to get my hand around the following scenario where I can't find a similar question with a similar scenario asked, so here we go:
Let's say I have two tables:
Messages:
| P_ID | Message_ID | Message_sent_date |
|---|---|---|
| 123 | ABCD | 2020/03/01 |
| 123 | BCDE | 2020/07/01 |
| 234 | CDEF | 2020/01/01 |
| 234 | DEFG | 2020/05/01 |
People:
| P_ID | P_Achievement | Achievement_date |
|---|---|---|
| 123 | Level 1 | 2019/09/01 |
| 123 | Level 4 | 2020/06/01 |
| 234 | Level 2 | 2019/12/01 |
| 234 | Level 3 | 2020/04/01 |
I want to join people on messages with P_ID, BUT with the condition that only the most recent achievement relevant to the message_sent_date is displayed from P_ID. So it should look like this:
| P_ID | Message_ID | Message_sent_date | P_achievement | Achievment_date |
|---|---|---|---|---|
| 123 | ABCD | 2020/03/01 | Level 1 | 2019/09/01 |
| 123 | BCDE | 2020/07/01 | Level 4 | 2020/06/01 |
| 234 | CDEF | 2020/01/01 | Level 2 | 2019/12/01 |
| 234 | DEFG | 2020/05/01 | Level 3 | 2020/04/01 |
My current code actually works, however, the problem is that the messages and people data tables in my real-life problem are incredibly large so the query takes more than an hour to run (I actually never fully finished running it because it takes too long, but tried with a specific P_ID example where it works). Filtering the two original tables doesn't help much.
I know that the subquery in the where is causing the long run time so I was wondering if anyone knows how to solve this type of problem in a more efficient way?
Thanks already in advance!
SELECT *
FROM messages m
LEFT JOIN people p
ON m.P_ID = p.P_ID
WHERE Achievment_date = (SELECT MAX(Achievement_date)
FROM people
WHERE Message_sent_date >= Achievement_date
)