1

I have come to this query, which retrieves wikipedia and wikidata info from a wikipedia title:

SELECT ?wikipediaId ?wikipediaTitle ?wikidataId ?desc ?imageUrl
(GROUP_CONCAT(?instanceOfS; SEPARATOR=";") AS ?instancesOf)
(GROUP_CONCAT(?subclassOfS; SEPARATOR=";") AS ?subclassesOf)
(GROUP_CONCAT(?partOfS; SEPARATOR=";") AS ?partsOf)
(GROUP_CONCAT(?instanceOfLabel; SEPARATOR=";") AS ?instancesOfLabel)
(GROUP_CONCAT(?subclassOfLabel; SEPARATOR=";") AS ?subclassesOfLabel)
(GROUP_CONCAT(?partOfLabel; SEPARATOR=";") AS ?partsOfLabel)
WHERE {
  SERVICE wikibase:mwapi {
    bd:serviceParam wikibase:endpoint "en.wikipedia.org".
    bd:serviceParam wikibase:api "Generator".
    bd:serviceParam mwapi:generator "links".
    bd:serviceParam mwapi:titles "Albert Einstein".

    bd:serviceParam mwapi:prop "pageprops|description|pageimages".
    bd:serviceParam mwapi:piprop "thumbnail".
    bd:serviceParam mwapi:pithumbsize "300".
    bd:serviceParam mwapi:pilicense "any".

    ?wikipediaTitle wikibase:apiOutput mwapi:title.
    ?wikipediaId wikibase:apiOutput "@pageid".

    ?item wikibase:apiOutputItem mwapi:item.
    ?desc wikibase:apiOutput "@description".
    ?imageUrl wikibase:apiOutput "thumbnail/@source".
  }
  BIND (COALESCE(?item, "") AS ?vItem)
  OPTIONAL { ?vItem wdt:P31 ?instanceOf }
  OPTIONAL { ?vItem wdt:P279 ?subclassOf }
  OPTIONAL { ?vItem wdt:P361 ?partOf }

  BIND (STRAFTER(STR(?vItem), STR(wd:)) AS ?wikidataId)
  BIND (STRAFTER(STR(?instanceOf), STR(wd:)) AS ?instanceOfS)
  BIND (STRAFTER(STR(?subclassOf), STR(wd:)) AS ?subclassOfS)
  BIND (STRAFTER(STR(?partOf), STR(wd:)) AS ?partOfS)

  MINUS { ?vItem wdt:P31/wdt:P279* wd:Q17442446 }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "en".
    ?instanceOf rdfs:label ?instanceOfLabel.
    ?subclassOf rdfs:label ?subclassOfLabel. 
    ?partOf rdfs:label ?partOfLabel. }
} GROUP BY ?wikipediaId ?wikipediaTitle ?wikidataId ?desc ?imageUrl

It takes 13s for Albert Einstein, and 35s for Brazil.

I don't expect this to work too much fast, but I am sure this can be speeded up. I have read that the labels should be outside from this select, but even without the labels, the code still pretty slow. I have read that some sort of WITH blocks could help, but I haven't found too many examples.

What are the best ways to speed this up?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.