3
votes

I am able to use skip, limit (and order by) to fetch the contents of particular page in the UI. E.g. to render nth page of page size m. UI asks for skip n*m and limit m.

But, UI wants to generate links for all the possible pages. For that i have to return it total rows available in neo4j. E.g. for total p rows, the UI will generate hyperlink 1,2,3... (p/m).

What is the best(in terms of performance) way to get the total number of rows while using skip, limit in the the cypher?

1

1 Answers

4
votes

In general it is not advisable as fetching all results requires you to fetch large swaths of the graph into memory.

You have two options:

  1. use a simpler version of your query as separate count query (which might also run asynchronously)
  2. merge the count query and your real query into one, but it will be much more expensive than your skip-limit query, in the worst case totalcount/pageSize times more expensive

    start n=node:User(name={username}) match n-[:KNOWS]->() with n,count(*) as total match n-[:KNOWS]->m return m.name, total skip {offset} limit {pagesize}