marklogicmarklogic-optic-api

op:and versus multiple op:where clauses


With the Optic API, you can express three conditions using multiple op:where clauses or by nesting op:ands. Looking at the plans, the first approach uses multiple FILTER statements, while the second ANDs the conditions in a single FILTER statement. Is there a difference in efficiency between these?

Query:

op:from-view("stuff", "table")
  => op:where($cond1)
  => op:where($cond2)
  => op:where($cond3)

Plan (reformatted for legibility):

<map:entry key="_plan">
  <map:value xsi:type="plan:query" xmlns:plan="http://marklogic.com/plan">
    plan:sparql(
      "{ SELECT field1 field2 field3 {  <http://marklogic.com/templateview>  . } }
         FILTER (field1 eq Q{http://www.w3.org/2001/XMLSchema}string(&quot;value1&quot;)) 
         FILTER (field2 eq Q{http://www.w3.org/2001/XMLSchema}string(&quot;value2&quot;)) 
         FILTER (field3 eq Q{http://www.w3.org/2001/XMLSchema}string(&quot;value3&quot;))"
    )
  </map:value>
</map:entry>

Query:

op:from-view("stuff", "table")
  => op:where(op:and(op:and($cond1, $cond2), $cond3))

Plan:

<map:entry key="_plan">
  <map:value xsi:type="plan:query" xmlns:plan="http://marklogic.com/plan">
    plan:sparql(
      "{ SELECT field1 field2 field3 {  <http://marklogic.com/templateview>  . } } 
         FILTER (
           field1 eq Q{http://www.w3.org/2001/XMLSchema}string(&quot;value1&quot;) and 
           field2 eq Q{http://www.w3.org/2001/XMLSchema}string(&quot;value2&quot;) and 
           field3 eq Q{http://www.w3.org/2001/XMLSchema}string(&quot;value3&quot;)
         )"
    )
  </map:value>
</map:entry>

Solution

  • They'll behave identically - there's no reason to choose one over the other.