Pagination
This page covers pagination of TypeQL query results.
Behavior
Pagination in TypeQL queries makes it possible to return a range of results, rather than retrieving all results at once.
By limiting the query to return the first x
results,
processing them, and then sending subsequent queries with an offset, incremented by x
,
you can iterate through the entire result set in manageable batches.
Sorting the results is strongly advised, but is not mandatory. Without sorting, the ordering of results is not guaranteed and can be unpredictable. Pagination relies on sorting to traverse the full set of results.
Offset the results
For this example, use a database with the IAM schema and sample data loaded.
Use the offset
keyword followed by the number of results to skip.
match
$p isa person, has full-name $n;
get $n;
sort $n;
offset 2;
The above example sorts the full-name
attributes of all person
entities in ascending order (as default),
skips the first two results, and returns the rest of results.
Limit the number of results
For this example, use a database with the IAM schema and sample data loaded.
Use the limit
keyword followed by the number of results to return.
match
$p isa person, has full-name $n;
get $n;
limit 2;
The above example has no sort
modifier,
so it returns only two almost random names owned by entities of the person
type.
Usage
For this example, use a database with the IAM schema and sample data loaded.
Combine offset
and limit
to paginate the results of a query.
By knowing the limit amount, we can use offset to get the full set of results in batches.
Make sure to also use sorting, as otherwise you can’t guarantee the continuity of results between queries.
match
$p isa person, has full-name $n;
get $n;
sort $n;
limit 2;
The above query returns the first two results.
match
$p isa person, has full-name $n;
get $n;
sort $n;
offset 2;
limit 2;
The second query returns the next two results (skips two and returns another two).
By continuing to increase the offset
value by the limit
value we can traverse the results.
When we receive fewer results than set by the limit
value, it should be a signal that we got all possible results.