Neo4j : Using ORDER BY with SKIP and LIMIT, is it the better way to get a good performance ( execution time)?

Hello,

I am currently using neo4j, i want to know is there a way to retreive the resutls of the (SKIP, LIMIT) query in a way that the retreived data exists only once in the results, here an example for a better comprehension :

We have 80k persons (the key is id) in our data, i want to get back these persons in three different pages so that our serveur will not take a long time ( its called the pagination of the results of cypher query). So here is three queries to understande how it will happen :

Match (p: Person)
Return p
ORDER BY p.id
Skip 0 LIMIT 30 000

Match (p: Person)
Return p
ORDER BY p.id
Skip 30 000 LIMIT 30 000

Match (p: Person)
Return p
ORDER BY p.id
Skip 60 000 LIMIT 20 000

(Note : i know there is a way to use paramaters to avoid repeating the query, its just so you can understand me)

So, here i am using ORDER BY to assure that i am getting back the data only once, but its not the best way to do it because it takes long time and we must order all the persons in each iterate witch is not the best practice.

My question is, is there any way to remplace the work of ORDER BY.

Please, is there anyone knows any clause or function so we can get these persons correctly and effectively.

Thank you in advance.

I assume you have an index or constraint on id.
If you use Neo4j 3.5 it will be using the index to return the users in a sorted fashion.

the only thing you have to change is to tell the planner that it's a supported type, in your case e.g. an number with an operation that hints at it.

explain
Match (p: Person)
WHERE p.id > 0
Return p
ORDER BY p.id
Skip 0 LIMIT 30 000

Here is the relevant documentations link:

https://neo4j.com/docs/cypher-manual/current/query-tuning/cypher-index-values-order/

1 Like

Hi @michael.hunger,
Do you think "LIMIT 30000" is a large data set for return data ? I also have an issue like this and It take around above 4 5min for a query.
Thanks for your help.