Avoid timeout when selecting more properties with Sparql from Wikidata

37 Views Asked by At

I'm trying to write a query where I find all musicians and their relatives, children, spouses, fathers and mothers from Wikidata, that are still alive:

Here's the query:

SELECT DISTINCT ?item ?itemLabel ?itemDescription ?birthDate ?positionHeld WHERE {
  {
    SELECT ?item WHERE {
      {
        ?item wdt:P106 wd:Q639669. 
        ?item wdt:P27 wd:Q43.
      }
      FILTER(NOT EXISTS { ?item (wdt:P570|wdt:P509|wdt:P20) ?o. })
    }
  }
  UNION
  {
    SELECT ?item WHERE {
      {
        ?parent wdt:P106 wd:Q639669.
        ?parent wdt:P27 wd:Q43.
      }
      OPTIONAL { ?parent wdt:P40 ?item. }
    }
  }
  UNION
  {
    SELECT ?item WHERE {
      {
        ?parent wdt:P106 wd:Q639669.
        ?parent wdt:P27 wd:Q43.
      }
      OPTIONAL { ?parent wdt:P22 ?item. }
    }
  }
  UNION
  {
    SELECT ?item WHERE {
      {
        ?parent wdt:P106 wd:Q639669.
        ?parent wdt:P27 wd:Q43.
      }
      OPTIONAL { ?parent wdt:P26 ?item. }
    }
  }
  UNION
  {
    SELECT ?item WHERE {
      {
        ?parent wdt:P106 wd:Q639669.
        ?parent wdt:P27 wd:Q43.
      }
      OPTIONAL { ?parent wdt:P25 ?item. }
    }
  }
  UNION
  {
    SELECT ?item WHERE {
      {
        ?parent wdt:P106 wd:Q82955.
        ?parent wdt:P27 wd:Q43.
      }
      OPTIONAL { ?parent wdt:P1038 ?item. }
    }
  }
  OPTIONAL { ?item wdt:P569 ?birthDate. }
  OPTIONAL { ?item wdt:P39 ?positionHeld. }
  SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE]". }
}

This query times out. When I remove

OPTIONAL { ?item wdt:P569 ?birthDate. }
OPTIONAL { ?item wdt:P569 ?positionHeld. }

and stop selecting ?birthDate and ?positionHeld it works. Adding FILTER(NOT EXISTS { ?parent (wdt:P570|wdt:P509|wdt:P20) ?o. }) to filter only relatives that are still alive to subqueries also generates a timeout. So:

  1. How can I select ?birthDate and ?positionHeld without timing out?
  2. How do I filter out relatives that have passed out?
0

There are 0 best solutions below