Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Good SPARQL query to find all triples with a resource as subject or object

I need to find all triples on DBpedia where http://dbpedia.org/resource/Benin is a subject or object. This query gives me the output that I want in a format that works the best for me (just three variables and no blank spaces):

PREFIX : <http://dbpedia.org/resource/>
SELECT * WHERE {
?s ?p ?o
FILTER (?s=:Benin OR ?o=:Benin)
}

I get similar results if I have this query:

PREFIX : <http://dbpedia.org/resource/>
SELECT * WHERE {
{:Benin ?p ?o}
UNION
{?s ?p :Benin}
}

However, the formatting of the latter is off. It first gives me p and o output leaving s blank and then s and p leaving o blank. Also, the first query takes more time to execute. I will be grateful for an explanation of the mechanics of how the two queries work and why there is a difference in the output.

like image 547
kurious Avatar asked Feb 07 '16 08:02

kurious


1 Answers

However, the formatting of the latter is off

That's because both queries have different result sets together with SELECT *. The union joins the tuples, but since some tuples are missing parts, you get skewed output.

You can resolve the problem by explicitly listing and selecting the variables:

PREFIX : <http://dbpedia.org/resource/>
SELECT ?s ?p ?o WHERE {
   {
       ?s ?p ?o
       FILTER (?s=:Benin)
   }
   UNION 
   {
       ?s ?p ?o .
       FILTER (?o=:Benin)
   }
}

Note that this is still much faster on dbpedia than the OR filter.

The union will return duplicates when a tuple matches both filter expressions (i.e. :Benin ?p :Benin). SELECT DISTINCT would remedy that at additional cost and since it looks like the problem is non-existent, I omitted it for improved performance.

Also, the first query takes more time to execute.

That's hard to say without the result of an EXPLAIN(), but my first guess would be that the equality filter is using the index, while the OR filter is using a full table scan. Virtuoso does not seem to generate good query plans for nested filters.

like image 163
dhke Avatar answered Sep 28 '22 14:09

dhke