首页 > 解决方案 > 在查询中使用 VALUES 关键字和直接使用 URI 之间的性能差异?

问题描述

我有一个相当复杂的 SPARQL 查询,其结构如下所述,涉及多个图形模式,UNION并且嵌套了FILTER NOT EXISTS.

我希望查询保持通用,并且我希望能够在执行时为某些变量注入值,我的想法是VALUES在查询末尾附加一个关键字来指定查询中某些变量的值。在下面的结构中,我设置了 的值?x,并说明了查询中?x适用的所有位置。

但是,在 Fuseki 中,我看到执行这样的查询大约需要 4 到 5 秒,但是用?xURI 手动替换查询中的变量,而不是指定VALUES子句,使它运行得非常快。

谢谢 !

SELECT DISTINCT ...
WHERE {
    # ?x ...
    # ... basic graph pattern here 

    {
      {
        # ... basic graph pattern here 

        FILTER NOT EXISTS {
            # ?x ...
            # ... basic graph pattern here
        }

        FILTER NOT EXISTS {
            # ... basic graph pattern here
            FILTER NOT EXISTS {
                # ?x ...
                # ... basic graph pattern here
            }
        }       
      }
      UNION
      {
        ?x ...
        # ... basic graph pattern here
      }
      UNION
      {
        # ... basic graph pattern here

        FILTER NOT EXISTS {
            ?x ...
            # ... basic graph pattern here
        }

        FILTER NOT EXISTS {
            # ... basic graph pattern here
            FILTER NOT EXISTS {
                ?x ...
                # ... basic graph pattern here
            }
        }
      }
      UNION
      {
        ?x ...
      }
    }
}
VALUES ?x { <http://example.com/Foo> }

标签: sparqlrdfjenafuseki

解决方案


不应该是一个答案,但在评论中格式化是不可能的......

代数树至少有一些明显的区别。如何处理这可能是特定于实现的。安迪比我更了解,希望能给出更有用的答案。

没有VALUES

询问

SELECT  ?s ?o
WHERE
  {   { <test_val>  <p>  ?o }
    UNION
      { <test_val>  <p>  ?o
        FILTER NOT EXISTS { <test_val>  a                   ?type }
      }
  }

代数树(优化)

(base <http://example/base/>
  (project (?s ?o)
    (union
      (bgp (triple <test_val> <p> ?o))
      (filter (notexists (bgp (triple <test_val> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ?type)))
        (bgp (triple <test_val> <p> ?o))))))

VALUES

询问

SELECT  ?s ?o
WHERE
  {   { ?s  <p>  ?o }
    UNION
      { ?s  <p>  ?o
        FILTER NOT EXISTS { ?s  a                     ?type }
      }
  }
VALUES ?s { <test_val> }

代数树

(base <http://example/base/>
  (project (?s ?o)
    (join
      (union
        (bgp (triple ?s <p> ?o))
        (filter (notexists (bgp (triple ?s <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ?type)))
          (bgp (triple ?s <p> ?o))))
      (table (vars ?s)
        (row [?s <test_val>])
      ))))

代数树(优化)

(base <http://example/base/>
  (project (?s ?o)
    (sequence
      (table (vars ?s)
        (row [?s <test_val>])
      )
      (union
        (bgp (triple ?s <p> ?o))
        (filter (notexists (bgp (triple ?s <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> ?type)))
          (bgp (triple ?s <p> ?o)))))))

推荐阅读