可以进行更快的过滤查询吗?

时间:2018-08-08 00:50:23

标签: query-optimization sparql wikidata

对于“ WHERE IN”来说,此查询似乎非常懒...由于不需要检查“ Universe”,因此只设置了少量IN ()

SELECT ?item ?itemLabel ?of ?ofLabel
WHERE 
{
  ?item wdt:P31 ?of.
  FILTER ( ?item IN (
    wd:Q28114532, wd:Q27745011,wd:Q3415363,wd:Q3415324,wd:Q2877432,wd:Q2877444,
    wd:Q2396644,wd:Q3444776,wd:Q2877428,wd:Q578757,wd:Q2877445,wd:Q2333617
  ) )
  SERVICE wikibase:label { 
     bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en".
  }
}

还有另一种方法可以更快地完成同一件事?


注意

问题随着类似的事情而增长,但要检查该项目是否具有“任何依赖项”-某物的实例,子实例,类或子类(例如Q7860962)。

SELECT ?item ?itemLabel ?x ?xLabel
WHERE 
{
  ?x (wdt:P31|wdt:P279)* wd:Q7860962 .
  ?item wdt:P31 ?x .
  FILTER ( ?item IN (
    wd:Q28114532, wd:Q27745011,wd:Q3415363,wd:Q3415324,wd:Q2877432,wd:Q2877444,
    wd:Q2396644,wd:Q3444776,wd:Q2877428,wd:Q578757,wd:Q2877445,wd:Q2333617
  ) )
  SERVICE wikibase:label { 
     bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en".
  }
}

1 个答案:

答案 0 :(得分:3)

正如@AKSW指出的那样,您可以使用VALUES。您的第一个查询将快大约150倍:

SELECT ?item ?itemLabel ?of ?ofLabel
WHERE 
{ VALUES (?item) {
      (wd:Q28114532) (wd:Q27745011) (wd:Q3415363) (wd:Q3415324) (wd:Q2877432) (wd:Q2877444)
      (wd:Q2396644)  (wd:Q3444776)  (wd:Q2877428) (wd:Q578757)  (wd:Q2877445) (wd:Q2333617)
  }
  ?item wdt:P31 ?of.
  SERVICE wikibase:label  { 
     bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en".
  }
}

Try it!

在第二个查询中,您还应该添加hint:Prior hint:gearing "forward"

SELECT ?item ?itemLabel ?x ?xLabel
WHERE 
{
  VALUES (?item) {
      (wd:Q28114532) (wd:Q27745011) (wd:Q3415363) (wd:Q3415324) (wd:Q2877432) (wd:Q2877444)
      (wd:Q2396644)  (wd:Q3444776)  (wd:Q2877428) (wd:Q578757)  (wd:Q2877445) (wd:Q2333617)
  }
  ?item wdt:P31 ?x .
  ?x (wdt:P31|wdt:P279)* wd:Q7860962 .
  hint:Prior hint:gearing "forward" .
  SERVICE wikibase:label { 
     bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en".
  }
}

Try it!