Wanted to share some learnings about multiselect f...
# suiteql
w
Wanted to share some learnings about multiselect fields. • Adding the column directly in a query appears to add some type of subquery in the background to get the comma-delimited list of IDs of the selected values. It appears that the value is not stored. • The above behind-the-scenes-subquery can add substantial overhead that you are not in control of • It appears to fetch all values from the MAP-table, regardless if you're only interested in the active records in both tables • If you have a lot of inactive records in the tables it is beneficial to do your own CTE to fetch the comma-separated list (if that's how you want it) and filter out the inactive ones directly. In my case below is almost twice as fast as querying the field directly (~1,1s compared to ~2s)
Copy code
WITH
    S_MAP as (
        SELECT
    	    s_map.classification
    	    ,LISTAGG(s_map.subsidiary, ', ' ON OVERFLOW TRUNCATE '...') WITHIN GROUP (ORDER BY s_map.subsidiary) as subsidiaries
        FROM
    	    classificationSubsidiaryMap s_map
            LEFT JOIN classification C on C.id = s_map.classification
            LEFT JOIN subsidiary S on s_map.subsidiary = S.id
        WHERE
            C.isinactive = 'F'
            AND S.isinactive = 'F'
        GROUP BY classification
        ORDER BY classification
    )
    SELECT
        C.id
        ,S.subsidiaries
    FROM
        classification C
        LEFT JOIN S_MAP S on C.id = S.classification
    WHERE
        C.isinactive = 'F'
    ORDER BY C.id
compared to
Copy code
SELECT
    C.id
    ,C.subsidiary
FROM
    classification C
WHERE
    C.isinactive = 'F'
ORDER BY C.id
❤️ 2
👍 1