Although this has been fixed in more recent versions, some versions of MySQL do a poor job of optimizing in
with a subquery. You can replace it with exists
:
SELECT p.id, p.name, p.type, p.status
FROM procedures p LEFT JOIN
procedure_department pd
ON p.id = pd.procedureID
WHERE p.addedBy = 47 OR
exists (SELECT 1
FROM permissions
WHERE user = 47 AND module = 'info' AND permission = 'add' and
option = pd.departmentID
);
For best performance, add an index on permissions(option, user, module, permission)
.