     WITH RECURSIVE expand_to AS (
         WITH RECURSIVE cte AS (
             SELECT
-                "locations_locationtype"."parent_type_id",
+                expand_to_type."parent_type_id",
                 0 AS "depth",
-                "locations_locationtype"."id" AS "expand_to_type"
-            FROM "locations_locationtype"
-            WHERE "locations_locationtype"."id" IN (
-                SELECT U1."expand_to_id"
-                FROM "locations_sqllocation" U0
-                INNER JOIN "locations_locationtype" U1 ON U0."location_type_id" = U1."id"
-                WHERE
-                    U0."id" = ANY(user_location_ids_array)
-                    AND U1."expand_to_id" IS NOT NULL
-            )
-
-            UNION ALL
-
-            SELECT
-                "locations_locationtype"."parent_type_id",
+                expand_to_type."id" AS "expand_to_type"
+            FROM "locations_sqllocation" loc
+            INNER JOIN "locations_locationtype" loc_type
+                ON loc."location_type_id" = loc_type."id"
+            INNER JOIN "locations_locationtype" expand_to_type
+                ON expand_to_type."id" = loc_type."expand_to_id"
+            WHERE
+                loc."id" = ANY(user_location_ids_array)
+                AND loc_type."expand_to_id" IS NOT NULL
+
+            UNION ALL
+
+            SELECT
+                iwe_type."parent_type_id",
                 0 AS "depth",
                 -1 AS "expand_to_type"
-            FROM "locations_locationtype"
-            WHERE "locations_locationtype"."id" IN (
-                SELECT U1."include_without_expanding_id"
-                FROM "locations_sqllocation" U0
-                INNER JOIN "locations_locationtype" U1 ON U0."location_type_id" = U1."id"
-                WHERE
-                    U0."id" = ANY(user_location_ids_array)
-                    AND U1."include_without_expanding_id" IS NOT NULL
-            )
-
-            UNION ALL
-
-            SELECT
-                "locations_locationtype"."parent_type_id",
+            FROM "locations_sqllocation" loc
+            INNER JOIN "locations_locationtype" loc_type
+                ON loc."location_type_id" = loc_type."id"
+            INNER JOIN "locations_locationtype" iwe_type
+                ON loc_type."include_without_expanding_id" = iwe_type."id"
+            WHERE
+                loc."id" = ANY(user_location_ids_array)
+                AND loc_type."include_without_expanding_id" IS NOT NULL
+
+            UNION ALL
+
+            SELECT
+                loc_type."parent_type_id",
                 "cte"."depth" + 1 AS "depth",
                 "cte"."expand_to_type" AS "expand_to_type"
-            FROM "locations_locationtype", "cte"
-            WHERE "locations_locationtype"."id" = "cte"."parent_type_id"
+            FROM "locations_locationtype" loc_type
+            INNER JOIN "cte" ON loc_type."id" = "cte"."parent_type_id"
         )
 
         SELECT

                    THEN -1
                    ELSE "cte"."depth"
                END AS "depth"
            FROM "locations_sqllocation", "cte"
            WHERE
                "locations_sqllocation"."is_archived" = FALSE
                AND "locations_sqllocation"."id" = "cte"."parent_id"
        )

        SELECT DISTINCT "cte"."loc_id", "cte"."depth" FROM "cte"

    ), fixture_ids AS (
        SELECT
            "locations_sqllocation"."id",
            "locations_sqllocation"."parent_id",
            Array ["locations_sqllocation"."id"] AS "path",
            0 AS "depth"
        FROM "locations_sqllocation"
        WHERE
            "locations_sqllocation"."is_archived" = FALSE
            AND "locations_sqllocation"."domain" = domain_name
            AND "locations_sqllocation"."parent_id" IS NULL
            AND EXISTS (
                SELECT 1
                FROM "expand_from" U0

            UNION ALL

            SELECT
                loc."parent_id",
                CASE
                    WHEN (
                        loc_type."expand_from_root" = FALSE
                        AND loc_type."expand_from" IS NOT NULL
                        AND NOT (
                            loc_type."expand_from" = loc."location_type_id"
                            AND loc_type."expand_from" IS NOT NULL
                        )
                        AND NOT EXISTS (
                            SELECT 1
                            FROM "locations_locationtype" U0
                            INNER JOIN "locations_locationtype_include_only" U1 ON U0."id" = U1."to_locationtype_id"
                            WHERE U1."from_locationtype_id" = loc."location_type_id"
                        )
                    )
                    THEN loc_type."expand_from"
                    ELSE NULL
                END AS "expand_from_type",
                CASE
                    WHEN loc_type."expand_from_root" = TRUE THEN NULL
                    ELSE loc."id"
                END AS "loc_id",
                CASE
                    WHEN loc_type."expand_to_id" IS NOT NULL THEN (
                        SELECT U0."expand_to_depth"
                        FROM "expand_to" U0
                        WHERE U0."expand_to_type" = loc_type."expand_to_id"
                    )
                    WHEN EXISTS (
                        SELECT 1
                        FROM "locations_locationtype" U0
                        INNER JOIN "locations_locationtype_include_only" U1
                            ON U0."id" = U1."to_locationtype_id"
                        WHERE U1."from_locationtype_id" = loc."location_type_id"
                    )
                    THEN -3
                    ELSE -2
                END AS "depth"
            FROM "locations_sqllocation" loc
            INNER JOIN "locations_locationtype" loc_type ON loc."location_type_id" = loc_type."id"
            WHERE
                loc."is_archived" = FALSE
                AND loc."domain" = domain_name
                AND loc."id" = ANY(user_location_ids_array)

            UNION ALL

            SELECT
                loc."parent_id",
                CASE
                    WHEN (
                        "cte"."expand_from_type" IS NOT NULL
                        AND "cte"."expand_from_type" <> loc."location_type_id"
                    )
                    THEN "cte"."expand_from_type"
                    ELSE NULL
                END AS "expand_from_type",
                CASE
                    WHEN "cte"."loc_id" IS NULL THEN NULL
                    ELSE loc."id"
                END AS "loc_id",
                CASE
                    WHEN (
