This query takes about 2.7 seconds, which is too long I think:
SELECT m.*, mcm.userid as useridto
FROM mdl_messages m
INNER JOIN mdl_message_conversations mc
ON m.conversationid = mc.id
INNER JOIN mdl_message_conversation_members mcm
ON mcm.conversationid = mc.id
WHERE mcm.userid != m.useridfrom
AND mcm.userid != '-10' AND mcm.userid != '-20' AND m.timecreated >= '1599197406'
ORDER BY m.timecreated ASC;
While simplifying the query the following difference astonished me:
This query takes about 2.5 seconds:
SELECT useridfrom FROM mdl_messages WHERE timecreated >= 1599197406 LIMIT 1
This query takes about 0.1 seconds:
SELECT id FROM mdl_messages WHERE timecreated >= 1599197406 LIMIT 1
SELECT useridfrom FROM mdl_messages WHERE timecreated >= 1599197406 LIMIT 1
This query takes about 0.1 seconds:
SELECT id FROM mdl_messages WHERE timecreated >= 1599197406 LIMIT 1
The mdl_messages table consists of about 300000 rows and is in the innodb format.