4

我正在尝试使用 SPARQL 从三重商店生成一些用户统计信息。请参阅下面的查询。如何改进?我在这里做坏事吗?为什么会消耗这么多内存?(请参阅本文末尾的背景故事)

我更喜欢在三重存储内进行聚合和连接。拆分查询意味着我必须在数据库外部“手动”加入结果,从而失去三重存储的效率和优化。无需无缘无故地重新发明轮子。

查询

SELECT
    ?person
    (COUNT(DISTINCT ?sent_email) AS ?sent_emails)
    (COUNT(DISTINCT ?received_email) AS ?received_emails)
    (COUNT(DISTINCT ?receivedInCC_email) AS ?receivedInCC_emails)
    (COUNT(DISTINCT ?revision) AS ?commits)

WHERE {
  ?person rdf:type foaf:Person.

  OPTIONAL {
    ?sent_email rdf:type email:Email.
    ?sent_email email:sender ?person.
  }

  OPTIONAL {
    ?received_email rdf:type email:Email.
    ?received_email email:recipient ?person.
  }

  OPTIONAL {
    ?receivedInCC_email rdf:type email:Email.
    ?receivedInCC_email email:ccRecipient ?person.
  }

  OPTIONAL {
    ?revision rdf:type vcs:VcsRevision.
    ?revision vcs:committedBy ?person.
  }
}
GROUP BY ?person
ORDER BY DESC(?commits)

背景

问题是我在 AllegroGraph 中收到错误“QUERY MEMORY LIMIT REACHED”(另请参阅我的相关SO 问题)。由于存储库仅包含大约 200k 三元组,这些三元组很容易放入 ca 的(ntriples)输入文件中。60 MB,我想知道执行查询结果如何需要超过 4 GB 的 RAM,大约高出两个数量级。

4

1 回答 1

0

尝试在子查询中拆分计算,例如:

SELECT
    ?person
    (MAX(?sent_emails_) AS ?sent_emails_)
    (MAX(?received_emails_ AS ?received_emails_)
    (MAX(?receivedInCC_emails_ AS ?receivedInCC_emails_)
    (MAX(?commits_) AS ?commits)
WHERE {
  { 
   SELECT 
          ?person 
          (COUNT(DISTINCT ?sent_email) AS ?sent_emails_) 
          (0 AS ?received_emails_) 
          (0 AS ?commits_) 
   WHERE {
    ?sent_email rdf:type email:Email.
    ?sent_email email:sender ?person.
    ?person rdf:type foaf:Person.
   } GROUP BY ?person 
  } union {
     (similar pattern for the others)
     ....
  }
}
GROUP BY ?person
ORDER BY DESC(?commits)

目标是:

  • 避免在结果集中生成大量需要处理以进行聚合的行
  • 避免使用 OPTIONAL{} 模式,这也会影响性能
于 2014-07-08T13:00:22.110 回答