Loading TOC...
Semantic Graph Developer's Guide (PDF)

MarkLogic 10 Product Documentation
Semantic Graph Developer's Guide
— Chapter 14

Execution Plan

This section describes how to interpret a query execution plan output from the sem:sparql-plan function. The generated query execution plan shows how the supplied query will be handled by the SPARQL parser. The query execution plan for SPARQL is designed to work with all SPARQL queries, including SELECT, CONSTRUCT, ASK and DESCRIBE.

Generating an Execution Plan

You can use sem:sparql-plan to generate a query execution plan for a SPARQL query to see how the query will be handled internally.

For example, this SPARQL query produces an execution plan:

sem:sparql-plan("select * { ?s ?p ?o }",(),"optimize=1")

The query outputs the following execution plan:

<plan:plan xmlns:plan="http://marklogic.com/plan">
 <plan:select>
  <plan:project order="1,0,2">
   <plan:variable name="s" column-index="0" static-type="NONE">
   </plan:variable>
   <plan:variable name="p" column-index="1" static-type="NONE">
   </plan:variable>
   <plan:variable name="o" column-index="2" static-type="NONE">
   </plan:variable>
   <plan:triple-index order="1,0,2" permutation="PSO" dedup="true">
    <plan:subject>
      <plan:variable name="s" column-index="0" static-type="NONE">
     </plan:variable>
    </plan:subject>
    <plan:predicate>
     <plan:variable name="p" column-index="1" static-type="NONE">
     </plan:variable>
    </plan:predicate>
    <plan:object>
     <plan:variable name="o" column-index="2" static-type="NONE">
     </plan:variable>
    </plan:object>
   </plan:triple-index>
  </plan:project>
 </plan:select>
</plan:plan>

Parsing an Execution Plan

This section breaks down and describes each portion of the execution plan.

The intro specifies the type of plan, in this case for a SELECT statement:

<plan:plan xmlns:plan="http://marklogic.com/plan">
 <plan:select>

This section identifies the projected order of the elements of the triples...Äìsubject, predicate, object...Äìwith their variable names (s,p,o) and column indexes:

<plan:project order="1,0,2">
   <plan:variable name="s" column-index="0" static-type="NONE">
   </plan:variable>
   <plan:variable name="p" column-index="1" static-type="NONE">
   </plan:variable>
   <plan:variable name="o" column-index="2" static-type="NONE">
   </plan:variable>

At the end is the order of the triple variables in the triple index - predicate, subject, object (p,s,o).

   <plan:triple-index order="1,0,2" permutation="PSO" dedup="true">
    <plan:subject>
      <plan:variable name="s" column-index="0" static-type="NONE">
     </plan:variable>
    </plan:subject>
    <plan:predicate>
     <plan:variable name="p" column-index="1" static-type="NONE">
     </plan:variable>
    </plan:predicate>
    <plan:object>
     <plan:variable name="o" column-index="2" static-type="NONE">
     </plan:variable>
    </plan:object>
   </plan:triple-index>
  </plan:project>
 </plan:select>
</plan:plan>

If you run this query, the variables and the values are projected in three columns (s, p, o):

[{"s":"<http://example.com/ns/directory#jp>","p":"<http://example.com/ns/person#firstName>","o":"\"John-Paul\""}]

Here is an example of a more complicated SPARQL SELECT query, which includes prefixes:

sem:sparql-plan("
PREFIX rdf:    <http://www.w3.org/1999/02/22-rdf-syntax-ns#> 
PREFIX rdfs:   <http://www.w3.org/2000/01/rdf-schema#>
PREFIX prod:   <http://example.com/products/> 
PREFIX ex:     <http://example.com/>

SELECT ?product
FROM <http://marklogic.com/semantics/products/>
WHERE 
  { 
    ?product  rdf:type  ex:Shirt ;
              ex:color  'blue' }")

Here is the output for this query execution plan. The intro specifies the namespace and the type of plan:

<plan:plan xmlns:plan="http://marklogic.com/plan">
 <plan:select>

The first section identifies the order of projected values, in this case just one product.

  <plan:project order="order(0 ASC)">
   <plan:variable name="product" column-index="0" static-type="NONE">
    </plan:variable>

This section describes what sort of hash join order.

   <plan:hash-join order="order(0 ASC)">
    <plan:hash left="0" right="0" operator="=">
     </plan:hash>

Here is the projected order of triple elements...Äìobject, predicate, subject (o, p, s)...Äìfirst for the triple for product of type Shirt:

<plan:triple-index order="order(0 ASC)" permutation="OPS" dedup="true">
  <plan:subject>
    <plan:variable name="product" column-index="0" static-type="NONE">
    </plan:variable>
  </plan:subject>
  <plan:predicate>
    <plan:iri name="http://www.w3.org/1999/02/22-rdf-syntax-ns#type" static-type="NONE">
    </plan:iri>
  </plan:predicate>
  <plan:object>
    <plan:iri name="http://example.com/Shirt" static-type="NONE">
    </plan:iri>
  </plan:object>
</plan:triple-index>

And for the product with the color value blue:

<plan:triple-index order="order(0 ASC)" permutation="OPS" dedup="true">
  <plan:subject>
    <plan:variable name="product" column-index="0" static-type="NONE">
    </plan:variable>
  </plan:subject>
  <plan:predicate>
    <plan:iri name="http://example.com/color" static-type="NONE">
    </plan:iri>
  </plan:predicate>
  <plan:object>
    <plan:value datatype="http://www.w3.org/2001/XMLSchema#string" value="blue">
    </plan:value>
  </plan:object>
</plan:triple-index>

And then the close of the plan:

    </plan:hash-join>
   </plan:project>
  </plan:select>
</plan:plan>

For more about query execution plans, see Execution Plan in the SQL Data Modeling Guide.

« Previous chapter