Queries of the form
CONSTRUCT {...} WHERE {...}
are evaluated with a three-stage pipeline. First, the query
SELECT * WHERE {...}
is executed. Second, the CONSTRUCT template is applied to each result row
(producing no triple for any triple pattern that has a variable without value
in that row). Third, any duplicate triples produced in step 2 are removed.
If you run both versions of your query in the SELECT * WHERE {...} form, you
will see that the version without UNION produces a much larger intermediate
result. It is only due to the duplicate removal in step 3 that you get the same
final result from both versions. This larger intermediate result explains why
the version without UNION is much slower.
Hope that helps,
Richard
> On 6 Oct 2021, at 21:59, Élie Roux <[email protected]> wrote:
>
> After long hours of anxiety, I discovered that using unions as in
>
> CONSTRUCT
> {
> bdr:MW23703_1183 ?instp ?insto .
> ?t ?tp ?to .
> ?ancestor :hasPart ?ancestorPart .
> }
> WHERE
> {
> {
> bdr:MW23703_1183 ?instp ?insto . # 200ms alone
> } union {
> bdr:MW23703_1183 :hasTitle ?t . ?t ?tp ?to . #245ms alone
> } union {
> bdr:MW23703_1183 :partOf+ ?ancestor . ?ancestor :hasPart
> ?ancestorPart . # 200ms alone
> }
> }
> is much faster than using the bgp in the same group... is there any
> reason for that?
>
> Best,
> --
> Elie