SPARQL查询中的表达式顺序

时间:2018-02-16 13:48:40

标签: sparql rdf semantic-web rdfs triplestore

以下两个问题之间有什么区别吗?

select distinct ?i 
where{
    ?i rdf:type <http://foo/bar#A>. 
    FILTER EXISTS {
        ?i <http://foo/bar#hasB> ?b.
        ?b rdf:type <http://foo/bar#B1>.
    }            
}


select distinct ?i 
    where{
        FILTER EXISTS {
            ?i <http://foo/bar#hasB> ?b.
            ?b rdf:type <http://foo/bar#B1>.
        }
        ?i rdf:type <http://foo/bar#A>.             
    }

在绩效或结果方面存在差异?

1 个答案:

答案 0 :(得分:4)

首先,您不需要FILTER EXISTS。您可以使用基本图形模式(一组常规三重模式)重写查询。但是,假设您正在使用FILTER NOT EXISTS或类似的东西。

结果

一般来说,order matters

然而,自上而下的评估语义主要在OPTIONAL的情况下发挥作用,而这不是你的情况。因此,结果应该是相同的。

自上而下的评估语义可以被bottom-up评估语义覆盖。幸运的是,自下而上的语义doesn't prescribe首先在逻辑上对FILTER进行评估,但在FILTER EXISTSFILTER NOT EXISTS的情况下可能会这样做。

SPARQL代数representation对于两个查询都是相同的:

(prefix ((rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>)
         (foobar: <http://foo/bar#>))
  (distinct
    (project (?i)
      (filter (exists
                 (bgp
                   (triple ?i foobar:B ?b)
                   (triple ?b rdf:type foobar:B1)
                 ))
        (bgp (triple ?i rdf:type foobar:A))))))

效果

天真地遵循自上而下的语义,引擎应首先评估?i a foobar:A

  • 如果?i只有一个绑定,那么你很幸运。
  • 你不是那么幸运,如果?i存在数百万个绑定,而子模式更具选择性。

幸运的是,优化器尝试根据其选择性重新排序模式。但是,预测可能是错误的。

顺便说一下,rdf:type谓词is said to be是Virtuoso的表演杀手。

结果与绩效

如果端点具有查询执行时间限制并且在达到超时时刷新部分结果,则结果可能不同:an example

相关问题