These are two very different queries. OPTIONAL is order-dependent. (SQL leftjoin is order dependent.)
Triple order where triples are adjacent does not matter:
{ ?s :predicate1 ?v1 . ?s :predicate2 ?v2 . }
{ ?s :predicate2 ?v2 . ?s :predicate1 ?v1 . }
But
{ ?s :predicate1 ?v1 .
OPTIONAL { ?s :predciate2 ?v2 }
}does not have adjacent triples and it is different to
{
OPTIONAL { ?s :predciate2 ?v2 }
?s :predicate1 ?v1 .
}The first is, in SPARQL algebra terms is:
(leftjoin
(bgp (triple ?s :predicate1 ?v1))
(bgp (triple ?s :predicate2 ?v2))) )the second is
(join
(leftjoin
(table unit)
(bgp (triple ?s :predicate2 ?v2)))
(bgp (triple ?s :predicate1 ?v1))))because OPTIONAL is really a binary operator - it "optionals" to add matches to what went before.
In the second case, nothing goes before; in the first it's the sub-SELECT
You can experiment with the SPARQL algebra at
http://sparql.org/validate/queryYes - the inner select is evaluated before combining with the rest of the patterns. But in the first it is left-hand side of the OPTIONAL and in the second it is joined tot he results of OPTIONAL then BIND being evaluated.
Andy