query-f62f6b09cb58e7ec90b528d7e0e2b2fa

rq turtle/ttl

Accessing Fast Range Counts from SPARQL (Blazegraph specific)

Compared to counting, FRCs have O(1) complexity and run in constant amortized time,

but accessing them using the slice service is inflexible and clunky,

since the service doesn't allow many inputs or unused variables.

These can also be obtained from Blazegraph's API, which is preferred, and much nicer.

Also ASTFastRangeCountOptimizer will try to convert some simple cases of counting to use FRC,

without you having to do anything. This is why count(*) on a simple triple pattern tends to be fast.

SELECT ?pred ?count WHERE { { bind("ps:P4342" as ?pred) { select * where { SERVICE bd:slice { [] ps:P4342 [] . bd:serviceParam bd:slice.range ?count . } } } } union { bind("pq:P4342" as ?pred) { select * where { SERVICE bd:slice { [] pq:P4342 [] . bd:serviceParam bd:slice.range ?count . } } } } union { bind("pr:P4342" as ?pred) { select * where { SERVICE bd:slice { [] pr:P4342 [] . bd:serviceParam bd:slice.range ?count . } } } } }

Use at

PREFIX ps: <http://www.wikidata.org/prop/statement/>
PREFIX pr: <http://www.wikidata.org/prop/reference/>
PREFIX pq: <http://www.wikidata.org/prop/qualifier/>
PREFIX bd: <http://www.bigdata.com/rdf#>
# Accessing Fast Range Counts from SPARQL (Blazegraph specific)
# Compared to counting, FRCs have O(1) complexity and run in constant amortized time,
# but accessing them using the slice service is inflexible and clunky,
# since the service doesn't allow many inputs or unused variables.
# These can also be obtained from Blazegraph's API, which is preferred, and much nicer.
# Also ASTFastRangeCountOptimizer will try to convert some simple cases of counting to use FRC,
# without you having to do anything. This is why count(*) on a simple triple pattern tends to be fast.
SELECT ?pred ?count WHERE {
  { bind("ps:P4342" as ?pred) { select * where {
    SERVICE bd:slice { [] ps:P4342 [] . bd:serviceParam bd:slice.range ?count . }
  } } }
  union
  { bind("pq:P4342" as ?pred) { select * where {
    SERVICE bd:slice { [] pq:P4342 [] . bd:serviceParam bd:slice.range ?count . }
  } } }
  union
  { bind("pr:P4342" as ?pred) { select * where {
    SERVICE bd:slice { [] pr:P4342 [] . bd:serviceParam bd:slice.range ?count . }
  } } }
}

Query found at

graph TD classDef projected fill:lightgreen; classDef literal fill:orange; classDef iri fill:yellow; v2("?count"):::projected v3("?pred"):::projected a1((" ")) a2((" ")) a3((" ")) a4((" ")) a5((" ")) a6((" ")) c3(["bd:serviceParam"]):::iri subgraph union0[" Union "] subgraph union0l[" "] style union0l fill:#abf,stroke-dasharray: 3 3; subgraph union1[" Union "] subgraph union1l[" "] style union1l fill:#abf,stroke-dasharray: 3 3; bind0[/"'pr:P4342'"/] bind0 --as--o v3 subgraph s1["http://www.bigdata.com/rdf#slice"] style s1 stroke-width:4px; a5 --"pr:P4342"--> a6 c3 --"bd:slice.range"--> v2 end end subgraph union1r[" "] style union1r fill:#abf,stroke-dasharray: 3 3; bind1[/"'pq:P4342'"/] bind1 --as--o v3 subgraph s1["http://www.bigdata.com/rdf#slice"] style s1 stroke-width:4px; a3 --"pq:P4342"--> a4 c3 --"bd:slice.range"--> v2 end end union1r <== or ==> union1l end end subgraph union0r[" "] style union0r fill:#abf,stroke-dasharray: 3 3; bind2[/"'ps:P4342'"/] bind2 --as--o v3 subgraph s1["http://www.bigdata.com/rdf#slice"] style s1 stroke-width:4px; a1 --"ps:P4342"--> a2 c3 --"bd:slice.range"--> v2 end end union0r <== or ==> union0l end