Joe124
Joe124

Reputation: 199

Limit records returned by range

Hi I have a particular sparql query which returns all my triples in a graph. This takes some time to run so I was wondering if I can limit the amount of triples and rerun the query everytime the user presses next on the interface ex: first he sees the first 10, then next 10 etc... Right now I am getting all the results and saving them and then traverse the results. Can I just get the first 10, next 10 etc..

Upvotes: 2

Views: 366

Answers (1)

cygri
cygri

Reputation: 9472

For a SELECT query, to get only the first ten rows:

SELECT ...
WHERE {
  ...
}
LIMIT 10

To get the next ten:

SELECT ...
WHERE {
  ...
}
OFFSET 10 LIMIT 10

For the next ten, increase the OFFSET to 20, and so on.

You say that your query is returning triples, so is it a CONSTRUCT query? LIMIT and OFFSET also work with CONSTRUCT, but the number of triples returned will depend on the number of triple patterns in the construct template.

Edit: When using LIMIT and OFFSET, on some SPARQL stores you will need to also use ORDER BY ?x, where ?x is one of the query variables. This ensures a predictable order of the results.

Upvotes: 1

Related Questions