首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >这个SPARQL查询可以简化吗?

这个SPARQL查询可以简化吗?
EN

Stack Overflow用户
提问于 2017-11-28 00:02:09
回答 1查看 316关注 0票数 5

目前,我正在为LDBC基准实现SPARQL查询。我想出了一个双读-3查询的解决方案。数据模式的相关部分如下:

查询说明:

查找在给定年份的给定月份消息中使用的标记和下一个月使用的标记。在这两个月中,计算使用每个标记的消息的数量。

我的解决方案(带有一些语法突出显示):

代码语言:javascript
复制
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX sn: <http://www.ldbc.eu/ldbc_socialnet/1.0/data/>
PREFIX snvoc: <http://www.ldbc.eu/ldbc_socialnet/1.0/vocabulary/>
PREFIX sntag: <http://www.ldbc.eu/ldbc_socialnet/1.0/tag/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX dbpedia: <http://dbpedia.org/resource/>
PREFIX dbpedia-owl: <http://dbpedia.org/ontology/>


SELECT ?tagName (SUM(?countMonth1) as ?countMonth1) (SUM(?countMonth2) as ?countMonth2) (ABS( SUM(?countMonth1) - SUM(?countMonth2) ) as ?diff)
WHERE
{ 
  {
    SELECT ?tagName (COUNT(?message) as ?innerCountMonth1)
    WHERE {

      BIND ( 2010 as ?year1 ) .
      BIND ( 9 as ?month1 ) .

      {
        ?message rdf:type snvoc:Comment 
      } UNION {
        ?message rdf:type snvoc:Post 
      } .
      ?message snvoc:creationDate ?creationDate .
      FILTER ((year(?creationDate) = ?year1 && month(?creationDate) = ?month1) )

      ?message snvoc:hasTag ?tag .
      ?tag foaf:name ?tagName .

    }
    GROUP BY ?tagName
  } UNION {
    SELECT ?tagName (COUNT(?message) as ?innerCountMonth2)
    WHERE {

      BIND ( 2010 as ?year1 ) .
      BIND ( 9 as ?month1 ) .
      BIND ( ?year1 + FLOOR(?month1 / 12.0) as ?year2 ) .
      BIND ( IF (?month1 = 12, 1, ?month1 + 1) as ?month2 ) .
      {
        ?message rdf:type snvoc:Comment 
      } UNION {
        ?message rdf:type snvoc:Post 
      } .
      ?message snvoc:creationDate ?creationDate .
      FILTER (year(?creationDate) = ?year2 && month(?creationDate) = ?month2 ) 

      ?message snvoc:hasTag ?tag .
      ?tag foaf:name ?tagName .

    }
    GROUP BY  ?tagName
  }

  BIND ( COALESCE(?innerCountMonth1, 0) as ?countMonth1 )
  BIND ( COALESCE(?innerCountMonth2, 0) as ?countMonth2 )
}
GROUP BY ?tagName
ORDER BY DESC(?diff) ?tagName

我有一种感觉,有一个更简单的解决办法,但我想不出来。

我的问题是:这个查询可以以更简单/更有效的方式实现吗?例如:没有嵌套查询或只是一种更快的方式。

我是SPARQL的新手,所以我会感激每一个有用的评论或改进。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-01-14 16:14:03

代码语言:javascript
复制
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX snvoc: <http://www.ldbc.eu/ldbc_socialnet/1.0/vocabulary/>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>

SELECT
?tagName
(SUM(xsd:integer(?isFirst)) AS ?countMonth1)
(SUM(xsd:integer(?isSecond)) AS ?countMonth2)
(ABS(?countMonth1 - ?countMonth2) AS ?diff) 
WHERE
  {
  VALUES (?year1 ?month1) {(2010 9)}
  VALUES (?type) {(snvoc:Comment) (snvoc:Post)}
  ?message a ?type; snvoc:hasTag/foaf:name ?tagName; snvoc:creationDate ?creationDate .
  BIND (year(?creationDate) AS ?year) .
  BIND (month(?creationDate) AS ?month) .
  BIND (IF (?month1 = 12, ?year1 + 1, ?year1     ) AS ?year2) .
  BIND (IF (?month1 = 12,          1, ?month1 + 1) AS ?month2) .
  BIND (((?month1 = ?month) && (?year1 = ?year)) AS ?isFirst) .
  BIND (((?month2 = ?month) && (?year2 = ?year)) AS ?isSecond) .
  FILTER (?isFirst || ?isSecond)
  }
  GROUP BY ?tagName HAVING (bound(?tagName))

更新

见评论。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47521838

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档