【问题标题】:SPARQL query with multiple aggregates exceeds memory limit具有多个聚合的 SPARQL 查询超出内存限制
【发布时间】:2014-07-08 21:00:22
【问题描述】:

我正在尝试使用 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”(另请参阅我的相关@​​987654321@)。由于存储库仅包含大约 200k 三元组,这些三元组很容易放入 ca 的(ntriples)输入文件中。 60 MB,我想知道执行查询结果如何需要超过 4 GB 的 RAM,这大约高出两个数量级。

【问题讨论】:

标签: optimization sparql


【解决方案1】:

尝试将计算拆分为子查询,例如:

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{} 模式,这也会影响性能

【讨论】:

  • 这没有什么问题。根据资料,提问者两小时前就来了,所以他可能很快就会看到答案并收到通知。
  • 那时候,我想我终于得到了这样的解决方案。
猜你喜欢
  • 2012-04-25
  • 2010-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-26
  • 2018-12-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多