Theory
Writing correct SPARQL means knowing the evaluation model, not just the syntax. A query is a tree of algebra operators; the engine evaluates them in this logical order:
BGP join → OPTIONAL (LeftJoin) → UNION → FILTER → GROUP BY →
aggregates → HAVING → BIND/project → ORDER BY → DISTINCT → OFFSET → LIMIT
Three consequences professionals rely on:
- A FILTER constrains its whole group, regardless of where you type it (from the FILTER lesson) — but the planner is free to push a selective filter down so it runs early. You write for clarity; the engine reorders for speed.
- OPTIONAL and UNION block some optimisations. They produce unbound variables and larger intermediate results, so an over-used OPTIONAL is a common cause of slow queries. Ask whether you truly need the row kept when the part is missing.
- Selectivity first. The cost of a join is driven by the size of intermediate bindings. Put the most selective pattern (a constant subject, a rare type) first so every later pattern joins against a small set.