0

With the following SPARQL query I’m trying to list countries with their national flags in descending order of population. I cannot run it without it reaching a timeout limit. It runs in ~2s when the indicated line is commented out (but this returns the cartesian product of all countries and all national flags, not just the associated pairs).

SELECT ?country ?countryLabel ?flag ?population

WHERE {
    ?country wdt:P31 wd:Q6256;
             wdt:P1082 ?population.

    ?flag wdt:P31 wd:Q186516.
    ?flag wdt:P1001 ?country. # runs without this line
  
    SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}

ORDER BY DESC(?population)
LIMIT 100

Try it here

Does anyone know why this query is to complex to compute, and how to get it to run? Thanks!

Jollywatt
  • 1,382
  • 2
  • 12
  • 31
  • 1
    the problem is often the label `SERVICE` - the same holds for your query, try it without – UninformedUser Oct 09 '21 at 13:59
  • 2
    alternatively, use standard RDF way: `SELECT ?country ?countryLabel ?flag ?population WHERE { ?country wdt:P31 wd:Q6256; wdt:P1082 ?population. ?flag wdt:P31 wd:Q186516; wdt:P1001 ?country. ?country rdfs:label ?countryLabel. FILTER ( lang(?countryLabel) = "en" ) } ORDER BY DESC(?population) LIMIT 100` – UninformedUser Oct 09 '21 at 13:59
  • @UninformedUser Thanks. That’s strange. Any idea why `SERVICE` causes a problem here, or why it often does? – Jollywatt Oct 11 '21 at 08:46
  • not sure, I guess it's basically a query optimizer thing, it's a non-standard SPARQL magic feature, so nice to have but sometimes you have to workaround it – UninformedUser Oct 11 '21 at 08:51

0 Answers0