I'm wading about in several tens of megabytes of health care data, trying to figure out what connects to which, as it were.  There are a few thousand relationship elements that define some of the connections, and often more than one such element per @type, so it's not enough to just group them by _this_ relationship elements type, I need all the relationship elements which have a particular type value.

let $relationships as element(relationship)+ := //relationship[@type]
let $relTypes as xs:string+ := distinct-values($relationships/@type/string())
let $relByType := map:merge(for $x in $relTypes return map:entry($x,$relationships[@type eq $x]))
return $relByType

82895.5 ms

let $relationships as element(relationship)+ := //relationship[@type]
let $relByType := map:merge(for $x in $relationships
                            let $key := $x/@type
                            group by $key
                            return map:entry($key,$x))
return $relByType

170.79ms

Holy Wow.

Offered just in case anyone else is wondering why their query is so slow.

-- Graydon