[SQL] Person statistics in "My students" take very long
SELECT DISTINCT "core_person"."id",
"core_person"."site_id",
"core_person"."extended_data",
"core_person"."user_id",
"core_person"."is_active",
"core_person"."first_name",
"core_person"."last_name",
"core_person"."additional_name",
"core_person"."short_name",
"core_person"."street",
"core_person"."housenumber",
"core_person"."postal_code",
"core_person"."place",
"core_person"."phone_number",
"core_person"."mobile_number",
"core_person"."email",
"core_person"."date_of_birth",
"core_person"."sex",
"core_person"."photo",
"core_person"."primary_group_id",
"core_person"."description",
COUNT(DISTINCT T8."id") FILTER (WHERE (T8."absent" AND ("chronos_validityrange"."school_term_id" = 2 OR "chronos_extralesson"."school_term_id" = 2 OR "chronos_event"."school_term_id" = 2) AND ("chronos_lesson_groups"."group_id" = ... OR "core_group_parent_groups"."to_group_id" = ... OR "chronos_extralesson_groups"."group_id" = ... OR T23."to_group_id" = ... OR "chronos_event_groups"."group_id" = ... OR T27."to_group_id" = ...))) AS "absences_count",
COUNT(DISTINCT T8."id") FILTER (WHERE (T8."absent" AND T8."excuse_type_id" IS NULL AND T8."excused" AND ("chronos_validityrange"."school_term_id" = 2 OR "chronos_extralesson"."school_term_id" = 2 OR "chronos_event"."school_term_id" = 2) AND ("chronos_lesson_groups"."group_id" = ... OR "core_group_parent_groups"."to_group_id" = ... OR "chronos_extralesson_groups"."group_id" = ... OR T23."to_group_id" = ... OR "chronos_event_groups"."group_id" = ... OR T27."to_group_id" = ...))) AS "excused",
COUNT(DISTINCT T8."id") FILTER (WHERE (T8."absent" AND NOT T8."excused" AND ("chronos_validityrange"."school_term_id" = 2 OR "chronos_extralesson"."school_term_id" = 2 OR "chronos_event"."school_term_id" = 2) AND ("chronos_lesson_groups"."group_id" = ... OR "core_group_parent_groups"."to_group_id" = ... OR "chronos_extralesson_groups"."group_id" = ... OR T23."to_group_id" = ... OR "chronos_event_groups"."group_id" = ... OR T27."to_group_id" = ...))) AS "unexcused",
(
SELECT DISTINCT SUM(U2."late") AS "tardiness"
FROM "core_person" U0
INNER JOIN "alsijil_personalnote" U2
ON (U0."id" = U2."person_id")
LEFT OUTER JOIN "chronos_lessonperiod" U3
ON (U2."lesson_period_id" = U3."id")
LEFT OUTER JOIN "chronos_lesson" U4
ON (U3."lesson_id" = U4."id")
LEFT OUTER JOIN "chronos_validityrange" U5
ON (U4."validity_id" = U5."id")
LEFT OUTER JOIN "chronos_extralesson" U7
ON (U2."extra_lesson_id" = U7."id")
LEFT OUTER JOIN "chronos_event" U9
ON (U2."event_id" = U9."id")
LEFT OUTER JOIN "chronos_lesson_groups" U11
ON (U4."id" = U11."lesson_id")
LEFT OUTER JOIN "core_group" U12
ON (U11."group_id" = U12."id")
LEFT OUTER JOIN "core_group_parent_groups" U13
ON (U12."id" = U13."from_group_id")
LEFT OUTER JOIN "chronos_extralesson_groups" U15
ON (U7."id" = U15."extralesson_id")
LEFT OUTER JOIN "core_group" U16
ON (U15."group_id" = U16."id")
LEFT OUTER JOIN "core_group_parent_groups" U17
ON (U16."id" = U17."from_group_id")
LEFT OUTER JOIN "chronos_event_groups" U19
ON (U9."id" = U19."event_id")
LEFT OUTER JOIN "core_group" U20
ON (U19."group_id" = U20."id")
LEFT OUTER JOIN "core_group_parent_groups" U21
ON (U20."id" = U21."from_group_id")
WHERE (U0."site_id" = 1 AND (U5."school_term_id" = 2 OR U7."school_term_id" = 2 OR U9."school_term_id" = 2) AND (U11."group_id" = ... OR U13."to_group_id" = ... OR U15."group_id" = ... OR U17."to_group_id" = ... OR U19."group_id" = ... OR U21."to_group_id" = ...) AND U0."id" = "core_person"."id")
GROUP BY U0."id"
) AS "tardiness",
COUNT(DISTINCT T8."id") FILTER (WHERE (NOT (T8."late" = 0 AND T8."late" IS NOT NULL) AND ("chronos_validityrange"."school_term_id" = 2 OR "chronos_extralesson"."school_term_id" = 2 OR "chronos_event"."school_term_id" = 2) AND ("chronos_lesson_groups"."group_id" = ... OR "core_group_parent_groups"."to_group_id" = ... OR "chronos_extralesson_groups"."group_id" = ... OR T23."to_group_id" = ... OR "chronos_event_groups"."group_id" = ... OR T27."to_group_id" = ...))) AS "tardiness_count",
COUNT(DISTINCT T8."id") FILTER (WHERE ("alsijil_personalnote_extra_marks"."extramark_id" = 1 AND ("chronos_validityrange"."school_term_id" = 2 OR "chronos_extralesson"."school_term_id" = 2 OR "chronos_event"."school_term_id" = 2) AND ("chronos_lesson_groups"."group_id" = ... OR "core_group_parent_groups"."to_group_id" = ... OR "chronos_extralesson_groups"."group_id" = ... OR T23."to_group_id" = ... OR "chronos_event_groups"."group_id" = ... OR T27."to_group_id" = ...))) AS "HA_count",
COUNT(DISTINCT T8."absent") FILTER (WHERE (T8."absent" AND T8."excuse_type_id" = 1 AND ("chronos_validityrange"."school_term_id" = 2 OR "chronos_extralesson"."school_term_id" = 2 OR "chronos_event"."school_term_id" = 2) AND ("chronos_lesson_groups"."group_id" = ... OR "core_group_parent_groups"."to_group_id" = ... OR "chronos_extralesson_groups"."group_id" = ... OR T23."to_group_id" = ... OR "chronos_event_groups"."group_id" = ... OR T27."to_group_id" = ...))) AS "s_count"
FROM "core_person"
INNER JOIN "core_persongroupthrough"
ON ("core_person"."id" = "core_persongroupthrough"."person_id")
INNER JOIN "alsijil_personalnote"
ON ("core_person"."id" = "alsijil_personalnote"."person_id")
INNER JOIN "alsijil_personalnote_groups_of_person"
ON ("alsijil_personalnote"."id" = "alsijil_personalnote_groups_of_person"."personalnote_id")
LEFT OUTER JOIN "alsijil_personalnote" T8
ON ("core_person"."id" = T8."person_id")
LEFT OUTER JOIN "chronos_lessonperiod"
ON (T8."lesson_period_id" = "chronos_lessonperiod"."id")
LEFT OUTER JOIN "chronos_lesson"
ON ("chronos_lessonperiod"."lesson_id" = "chronos_lesson"."id")
LEFT OUTER JOIN "chronos_validityrange"
ON ("chronos_lesson"."validity_id" = "chronos_validityrange"."id")
LEFT OUTER JOIN "chronos_extralesson"
ON (T8."extra_lesson_id" = "chronos_extralesson"."id")
LEFT OUTER JOIN "chronos_event"
ON (T8."event_id" = "chronos_event"."id")
LEFT OUTER JOIN "chronos_lesson_groups"
ON ("chronos_lesson"."id" = "chronos_lesson_groups"."lesson_id")
LEFT OUTER JOIN "core_group" T18
ON ("chronos_lesson_groups"."group_id" = T18."id")
LEFT OUTER JOIN "core_group_parent_groups"
ON (T18."id" = "core_group_parent_groups"."from_group_id")
LEFT OUTER JOIN "chronos_extralesson_groups"
ON ("chronos_extralesson"."id" = "chronos_extralesson_groups"."extralesson_id")
LEFT OUTER JOIN "core_group" T22
ON ("chronos_extralesson_groups"."group_id" = T22."id")
LEFT OUTER JOIN "core_group_parent_groups" T23
ON (T22."id" = T23."from_group_id")
LEFT OUTER JOIN "chronos_event_groups"
ON ("chronos_event"."id" = "chronos_event_groups"."event_id")
LEFT OUTER JOIN "core_group" T26
ON ("chronos_event_groups"."group_id" = T26."id")
LEFT OUTER JOIN "core_group_parent_groups" T27
ON (T26."id" = T27."from_group_id")
LEFT OUTER JOIN "alsijil_personalnote_extra_marks"
ON (T8."id" = "alsijil_personalnote_extra_marks"."personalnote_id")
WHERE ("core_person"."site_id" = 1 AND "core_persongroupthrough"."group_id" = ... AND "alsijil_personalnote_groups_of_person"."group_id" = ... AND ("chronos_validityrange"."school_term_id" = 2 OR "chronos_extralesson"."school_term_id" = 2 OR "chronos_event"."school_term_id" = 2))
GROUP BY "core_person"."id"
This query takes 18020 ms (example in production).
Edited by Nik | Klampfradler