I have a simple SPARQL query that looks for shared nodes within a graph using Filter
, and Union
arguments. The query take an unusually long time to compile. I was hoping to see if it is possible to rearrange this structurally to increase its performance.
The shared nodes are any of the three kinds of
- shared between two objects
- shared between two subjects, and
- object of one triple while subject of another.
I provided the graph of the possible data in another question I asked.
The query looks as below:
"""SELECT DISTINCT ?b
WHERE{
{
?b ?p1 ?a.
?b ?p2 ?c.
filter(?a != ?c).
}
UNION
{
?a ?p1 ?b.
?c ?p2 ?b.
filter(?a != ?c).
}
UNION
{
?a ?p1 ?b.
?b ?p2 ?c.
filter(?a != ?c).
}}
"""
From since I posted this, I did some experiments and learned the most time consuming part is the middle one.
{
?a ?p1 ?b.
?c ?p2 ?b.
filter(?a != ?c).
}