Error: Interrupted. Compiling: - pre-evaluating db:open("large_products") - applying attribute index for $diffCatId_0 - inlining $diffCatId_0 Query: declare namespace p = "a:b:c"; declare variable $prods := /products/*[@catid]; let $diffCatId := distinct-values($prods/@catid) let $catGroup := db:open('large_products')/products/p:category[@id = $diffCatId] return $prods[@catid = $catGroup/@id] Optimized Query: let $catGroup_1 := db:attribute("large_products", distinct-values((db:open-pre("large_products",3300002), ...)/@catid))/self::id/parent::p:category[parent::products/parent::document-node()] return ((db:open-pre("large_products",3300002), ...))[(@catid = $catGroup_1/@id)] Query plan: