Peter Krauss
Peter Krauss

Reputation: 13982

Is possible to do faster filter-IN queries?

This query seems very lazy for a "WHERE IN"... Because not need to check "the universe", only the little IN () set.

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".
  }
}

There are another way to do faster the same thing?


NOTE

The problem grows with similar thing, but checking if the item has "any dependency" — an instance, sub-instance, class or sub-class of something (eg. 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".
  }
}

Upvotes: 2

Views: 91

Answers (1)

Stanislav Kralin
Stanislav Kralin

Reputation: 11479

As @AKSW has pointed out, you could use VALUES. Your first query will be ~150 times faster:

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!

In your second query, you should also add 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!

Upvotes: 3

Related Questions