The 'all cities' part of your query seems strange.  If you replace it with a simple 'instance of city' - ?x wdt:P31 wd:Q515 . it returns 145 results in 341ms.  http://tinyurl.com/m4ffatg
Why would you ask for things that have 'occupation' or 'position' with value city?

Also, I'm not familiar with the use of the '/' in that line of the query.  My experience with that is as a mathematical operator.  What is it supposed to be doing?

On Wed, Apr 5, 2017 at 10:00 AM, Anatoly Zelenin <anatoly@zelenin.de> wrote:
Hi all,
I am building a Question Answering System for Wikidata (I will put the
demo live in May) and would like to be able to ask quesitons like "Give
me the cities in Alaska". To query Wikidata I am using the Wikidata
SPARQL endpoint.

The SPARQL query for this question looks the following way [0]:

SELECT DISTINCT ?x ?xLabel
{
  # All things in Alaska
  ?x (wdt:P131+ | wdt:P276) wd:Q797 .
  #  All cities
  ?x ((wdt:P106 | wdt:P39 | wdt:P31) / wdt:P279*) wd:Q515 .
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en" . }
}

As it needs to join over two very large sets (the set of all cities and
the set of all things in Alaska) the query times out. Do you have an
idea how I could write the query without getting a timeout?
Thank you ;)

[0]
https://query.wikidata.org/#SELECT%20DISTINCT%20%3Fx%20%3FxLabel%0A%7B%0A%3Fx%20%28wdt%3AP131%2B%20%7C%20wdt%3AP276%29%20wd%3AQ797%20.%0A%3Fx%20%28%28wdt%3AP106%20%7C%20wdt%3AP39%20%7C%20wdt%3AP31%29%20%2F%20wdt%3AP279%2a%29%20wd%3AQ515%20.%0ASERVICE%20wikibase%3Alabel%20%7B%0Abd%3AserviceParam%20wikibase%3Alanguage%20%22en%22%20.%0A%7D%0A%7D


--
Best,
Anatoly Zelenin



_______________________________________________
Wikidata mailing list
Wikidata@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/wikidata