EXPLAIN ANALYZE SELECT count(attachments.attachments.id) AS count_1
FROM attachments.attachments
JOIN attachments.folders ON attachments.folders.id = attachments.attachments.folder_id
JOIN events.events ON events.events.id = attachments.folders.event_id
LEFT OUTER JOIN events.sessions ON events.sessions.id = attachments.folders.session_id
LEFT OUTER JOIN events.contributions ON events.contributions.id = attachments.folders.contribution_id
LEFT OUTER JOIN events.subcontributions ON events.subcontributions.id = attachments.folders.subcontribution_id
LEFT OUTER JOIN events.contributions AS contributions_1 ON contributions_1.id = events.subcontributions.contribution_id
WHERE attachments.folders.link_type != 1
  AND NOT attachments.attachments.is_deleted
  AND NOT attachments.folders.is_deleted
  AND NOT events.events.is_deleted
  AND NOT coalesce(events.sessions.is_deleted, events.contributions.is_deleted, events.subcontributions.is_deleted, false)
  AND (contributions_1.is_deleted IS NULL
        OR NOT contributions_1.is_deleted)
