【发布时间】:2018-06-14 11:18:49
【问题描述】:
我的数据库有关于文档的信息,其中每个文档都有一个类别,例如
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX: <http://example.com>
:doc1 :hasCategory :category1 .
:category1 rdfs:label "Law" .
大约有 100k 条这样的语句。
运行一个简单的查询来获取每个类别的文档数:
SELECT ?category (count(distinct ?doc) as ?count) WHERE {
?doc :hasCategory ?category .
} GROUP BY ?category
运行大约需要 0.1 秒。
但也要返回类别标签:
SELECT ?category ?label (count(distinct ?doc) as ?count) WHERE {
?doc :hasCategory ?category .
?category rdfs:label ?label .
} GROUP BY ?category ?label
此查询需要超过 7 秒才能运行。
为什么差异会这么大,有没有更优化的查询可以用来获取标签?
【问题讨论】:
-
您可以检查和比较两个查询的query plans。你真的需要在每个类别中按标签分组吗?如果每个类别只有一个标签,请尝试
SELECT ?category (sample(?label) as ?l) (count(distinct ?doc) as ?count) WHERE { ...} GROUP BY ?category -
两个查询的估计迭代次数为 39926。
sample将耗时减少了一秒,但仍然是 6 秒左右 -
其实第一个查询是39926,第二个查询是39926.4551683254。
-
第一条语句
?doc :hasCategory ?category .的唯一对象数只有 133 。我原以为它只需要找到 133 个类别的标签