I have a list of nodes and I want to retrieve the triples containing all the relations between those nodes.
This is what I've done so far:
PREFIX myPrefix: <http://example.org/myPrefix/>
SELECT ?a ?relation ?b
WHERE{
values ?a { myPrefix:id_00083184 myPrefix:id_00083187
myPrefix:id_00083189 myPrefix:id_00083182
myPrefix:id_09463313 myPrefix:id_00000790
myPrefix:id_00073965 myPrefix:id_00073966
myPrefix:id_00016990 myPrefix:id_08380001
myPrefix:id_00019131 myPrefix:id_03739215
myPrefix:id_00019129 myPrefix:id_00034482
myPrefix:id_00098617
}
values ?b { myPrefix:id_00083184 myPrefix:id_00083187
myPrefix:id_00083189 myPrefix:id_00083182
myPrefix:id_09463313 myPrefix:id_00000790
myPrefix:id_00073965 myPrefix:id_00073966
myPrefix:id_00016990 myPrefix:id_08380001
myPrefix:id_00019131 myPrefix:id_03739215
myPrefix:id_00019129 myPrefix:id_00034482
myPrefix:id_00098617
}
?a ?relation ?b .
}
With this I get what I want, that is, all the relations between the nodes on my (external) list; using values
and repeating the nodes for ?a
and ?b
. But the problem is, my nodes list can be very large; sometimes it can have up to 1000 nodes and that's just a small case; and that query takes too long to execute.
Is there a better way to do what I need? This is the only approach I've found but I'm not very fluent in SPARQL so, any way of doing it correctly?
PS: I get the nodes list externally from a query from another database and using Java I construct the query appending every node on the list.
This all sounds very caveman-ish for me but as I said, I'm starting to use SPARQL.
Thanks a lot.
What you're doing now is probably the most general way to do this.
Individual APIs, e.g., Jena, may support other ways to specify a set of initial bindings that are functionally equivalent to a values block, but could let the actual query be much shorter. For example, see my answer to Saving and reusing the result of a SPARQL query. The answer to Add text search where clause to SPARQL query also has an example of initial bindings in Jena.