为什么我的 SPARQL 查询这么慢?

Why is my SPARQL query so slow?

我正在尝试通过 SPARQL 接口 (Interface here, schema here) 从欧盟全体会议辩论的数据库中获取一些查询。当我这样做时,我想检索演讲者的姓名、他们的祖国和他们的派对名称。 我需要 5 分钟来完成每个议程项目,这似乎很慢。我是否在查询中犯了明显的错误导致速度变慢?

SELECT ?text (SAMPLE(?speaker) AS ?speaker) (SAMPLE(?given) AS ?given) (SAMPLE(?surname) AS ?surname) (SAMPLE(?acronym) AS ?country) (SAMPLE(?partyLabel) AS ?partyLabel) (SAMPLE(?type) AS ?type)
WHERE {
   <http://purl.org/linkedpolitics/eu/plenary/2010-12-16_AgendaItem_4> dcterms:hasPart ?speech.
   ?speech lpv:speaker ?speaker.
   ?speaker foaf:givenName ?given.
   ?speaker foaf:familyName ?surname.
   ?speaker lpv:countryOfRepresentation ?country.
   ?country lpv:acronym ?acronym.
   ?speech lpv:translatedText ?text.
   ?speaker lpv:politicalFunction ?func.
   ?func lpv:institution ?institution.
   ?institution rdfs:label ?partyLabel.
   ?institution rdf:type ?type.
   FILTER(langMatches(lang(?text), "en"))
} GROUP BY ?text

请注意,将 ?speech lpv:translatedText ?text. 更改为 ?speech lpv:textt ?text. 可将查询时间缩短至 30 秒。

您的 SPARQL 查询看起来没有什么特别的错误,您也没有犯明显的错误(除了我稍后讨论的一些语法有效性问题)

问题似乎是您使用的 SPARQL 服务使用的三元组存储不能很好地处理具有大量连接的查询。在试验您的查询时,四处移动三重模式在 SPARQL 服务中产生了堆栈溢出!

我建议您自己从 http://linkedpolitics.ops.few.vu.nl/home 下载数据 - 关于数据 部分的第 3 点下有链接,您可以从中自行下载数据。然后,您可以将其加载到您选择的三元组存储中,并 运行 您的查询改为针对它。

例如,我下载了数据并将其放入 Apache Jena Fuseki免责声明 - 我在 Apache Jena 项目上工作)并且能够 运行在我将查询修复为正确有效的 SPARQL 后几乎立即查询。

使查询有效 SPARQL

给定的查询不是严格有效的 SPARQL,因此您需要更正它以便 运行 它在别处。

首先,查询未定义所使用的各种前缀,因为您使用的服务会自动插入它们,对于 运行 针对另一个三元组存储的此查询,您需要将以下内容添加到查询:

PREFIX dcterms: <http://purl.org/dc/terms/> 
PREFIX foaf: <http://xmlns.com/foaf/0.1/> 
PREFIX lpv: <http://purl.org/linkedpolitics/vocabulary/> 
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> 
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> 

在给定的变量名已经在范围内的情况下执行变量赋值也是不合法的,例如(SAMPLE(?speaker) AS ?speaker) 所以那些需要改变:

(SAMPLE(?speaker) AS ?speaker1)

这会导致以下有效且可移植的 SPARQL 查询:

PREFIX dcterms: <http://purl.org/dc/terms/> 
PREFIX foaf: <http://xmlns.com/foaf/0.1/> 
PREFIX lpv: <http://purl.org/linkedpolitics/vocabulary/> 
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> 
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> 

SELECT ?text (SAMPLE(?speaker) AS ?speaker1) (SAMPLE(?given) AS ?given1) (SAMPLE(?surname) AS ?surname1) (SAMPLE(?acronym) AS ?country1) (SAMPLE(?partyLabel) AS ?partyLabel1) (SAMPLE(?type) AS ?type1)
WHERE {
   <http://purl.org/linkedpolitics/eu/plenary/2010-12-16_AgendaItem_4> dcterms:hasPart ?speech.
   ?speech lpv:speaker ?speaker.
   ?speaker foaf:givenName ?given.
   ?speaker foaf:familyName ?surname.
   ?speaker lpv:countryOfRepresentation ?country.
   ?country lpv:acronym ?acronym.
   ?speech lpv:translatedText ?text.
   ?speaker lpv:politicalFunction ?func.
   ?func lpv:institution ?institution.
   ?institution rdfs:label ?partyLabel.
   ?institution rdf:type ?type.
   FILTER(langMatches(lang(?text), "en"))
} GROUP BY ?text