I am wondering how I can improve the speed of this query:
SELECT
caf.user_id,
caf.family_number,
caf.card_number,
caf.suspension_start_date,
caf.suspension_end_date,
caf.balance,
util.room,
caf.meal_plan_type,
student.special_diet,
student.specify_other,
users.surname,
users.type,
users.name,
users.contact
FROM
(SELECT
*
FROM
caf_student_cards) AS caf
LEFT JOIN
(SELECT
user_id, special_diet, specify_other
FROM
students) AS student ON caf.user_id = student.user_id
LEFT JOIN
(SELECT
resource_id AS room, user_id
FROM
utilizations) AS util ON caf.user_id = util.user_id
LEFT JOIN
(SELECT
surname, name, type, user_id, contact
FROM
system_users) AS users ON caf.user_id = users.user_id
WHERE
contact > 0;
contact
is from theusers
table. Then the WHERE-condition will change the result of the last join to an Inner Join, thus you can remove the lastLEFT
.