Search code examples
timeoutsparqlwikidata

SPARQL get all the data before it reaches timeout


I am trying to get all the city names of all countries in the world using this below query. Whenever I execute this below query it returns this message "Query timeout limit reached".

Is there any other way to get all the data before it reaches timeout limit?

SELECT ?country ?countryLabel ?city ?cityLabel
WHERE
{
  ?city wdt:P31/wdt:P279* wd:Q515;
        wdt:P17 ?country .

  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
ORDER BY ?country

Solution

  • I am not at all sure why, but, this query works for me:

    SELECT ?country ?countryLabel ?city ?cityLabel
    WHERE
    {
      ?city wdt:P31/wdt:P279* wd:Q515;
            wdt:P17 ?country .
    
      SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
    }
    ORDER BY ?countryLabel
    LIMIT 100000
    

    The two differences from your original query are:

    1. Ordering by countryLabel is, I'm guessing, what you actually wanted instead of ordering by country. In my experience ordering by label is sometimes faster too.
    2. I set a limit number. The query appears to return results of the same length as it would without a limit, since the limit is higher than the proper number of results.