-1

this seems to be relatively simple, to query the date of birth of Barack Obama, why does it take so long that it timeout at https://query.wikidata.org/ ? There are only about 3 entity that has label Barack Obama and also about 2 for "date of birth"

SELECT DISTINCT ?ent ?wdtProperty ?val ?valLabel WHERE { 
  
  ?ent rdfs:label|skos:altLabel "Barack Obama"@en. 
  ?wdProperty1 rdfs:label|skos:altLabel "date of birth"@en; 
  wikibase:directClaim ?wdtProperty1. 
  ?ent ?wdtProperty1 ?val . 
                                                         
} LIMIT 10
logi-kal
  • 7,107
  • 6
  • 31
  • 43
William
  • 5,526
  • 6
  • 20
  • 42
  • 1
    Blazegraph can't use POS indexes with these alternative property paths, I guess. Try https://w.wiki/mED . – Stanislav Kralin Nov 12 '20 at 16:36
  • @StanislavKralin Hey, thanks so much. It does become faster. Actually previously my query was using your way, but it was slow also then I changed to alternative paths. In order to get all result, I have to use different ?label for each triple, otherwise the result will set will be reduced, I am not sure why. But by using different label for each triple, it become very slow. I will attach the query on next comment, the query is to find out people whose religion is Buddhism. – William Nov 12 '20 at 17:14
  • @StanislavKralin Any idea how to improve the efficiency of this query ? It's taking 17 seconds currently `SELECT DISTINCT ?ent ?wdtProperty ?val ?valLabel WHERE { VALUES ?label1 { rdfs:label skos:altLabel } ?val wdt:P31|wdt:P106 [ ?label1 'people'@en ]. ?val ?wdtProperty2 ?ent2 . VALUES ?labelB2 { rdfs:label skos:altLabel } ?ent2 ?labelB2 "Buddhism"@en . ?wdProperty2 wikibase:directClaim ?wdtProperty2 . VALUES ?label2 { rdfs:label skos:altLabel } ?wdProperty2 ?label2 "religion"@en . OPTIONAL { ?val rdfs:label ?valLabel FILTER(lang(?valLabel) = "en") } } LIMIT 10` – William Nov 12 '20 at 17:15
  • @William what is the purpose of this query? It looks like mixed up some variable names? `?wdProperty2` vs `?wdProperty`? `?wdProperty` is selected but never bound? And `?wdProperty2 wikibase:directClaim ?wdtProperty2 .` is supposed to do what? – UninformedUser Nov 13 '20 at 04:53
  • Also `ent2` bvs `?ent` - if your goal is to find people with religion Buddhism, then it'S the worst query you could do? Why not using the URIs directly? – UninformedUser Nov 13 '20 at 07:09
  • 1
    anyways: `select ?item ?itemLabel ?a ?ent { hint:Query hint:optimizer "None". VALUES ?label { rdfs:label skos:altLabel } ?prop wikibase:directClaim ?a . ?prop ?label "religion"@en . ?item ?a ?ent . VALUES ?labelB2 { rdfs:label skos:altLabel } ?ent ?labelB2 "Buddhism"@en . VALUES ?label1 { rdfs:label skos:altLabel } ?item wdt:P31 [ ?label1 'people'@en ]. OPTIONAL { ?item rdfs:label ?itemLabel FILTER(lang(?valLabel) = "en") } } limit 10` – UninformedUser Nov 13 '20 at 07:34
  • @UninformedUser Thanks, sorry, just ignore those select variable, because the sparql was dynamically generated with parts combine together, so it's still buggy. yes, it's to find people whose religion is Buddhism. URI as you meant the Q ID in wikidata directly ? The Q ID is unknown before hand in my application, such as to convert natural language to SPARQL, the Q ID is unknown, so we have to lookup the Q ID or Property ID by label. ?wdProperty2 wikibase:directClaim ?wdtProperty2 this is to lookup the property ID by label. – William Nov 13 '20 at 09:08
  • @UninformedUser Your query is very optimized, I am trying to understand why when this triple is on top it makes the query run faster ? ?item ?a ?ent . Isn't at that position only the predicate ?a is known, so there will be a huge data to be scanned ? It seems counterintuitive to me, still trying to understand how the query is executed. I have moved the triple around for testing. – William Nov 13 '20 at 09:17

1 Answers1

-1

The correct answer to this is

SELECT DISTINCT ?ent ?wdtProperty1 ?val
WITH
{
  SELECT ?wdtProperty1
  WHERE
  {
    [] rdfs:label|skos:altLabel "date of birth"@en; 
       wikibase:directClaim ?wdtProperty1. 
  }
} AS %get_predicate
WITH
{
  SELECT ?ent
  WHERE
  {
    ?ent rdfs:label|skos:altLabel "Barack Obama"@en. 
  }
} AS %get_subject
WHERE
{ 
  INCLUDE %get_predicate
  INCLUDE %get_subject
  ?ent ?wdtProperty1 ?val .
}

Thanks to https://www.wikidata.org/wiki/Wikidata:Request_a_query#Slow_query_on_label

William
  • 5,526
  • 6
  • 20
  • 42