Reputation: 155
I am running different kinds of cypher query in neo4j browser to inspect my data in neo4j graph database. It's an offline analytic work so the database is read only (I can write it but no new data will come)
I want to have a "neo4j browser console session" to save result of a query for later use like:
var Persons = match (j:Person)-[..........complex query running for a long time] return j
so later I can run multiple queries ( which runs faster than previous one) like
start with Persons as p match (p)-[]->q...
How can I do this? I know I can use "with" to put them together but the first slow query will be run multiple times and I don't want that.
Currently I set a property on my node to mark the result
match (j:Person)-[...complex] set j.mark=1
match (j:Person {mark:1})........
But I think it is ugly and it will cause problem in when multiple users are using the database. And there are also extra cost (scan all nodes with label Person or maintaning :Person(mark) index )
How can I do this in a more elegant way?
Upvotes: 0
Views: 2546
Reputation: 29172
While in the Cypher
is not implemented CACHE MATCH...
statement you can do something like this:
CREATE (CACHE:Cache {name: 'cachequery'}) WITH CACHE
match (j:Person)-[..........complex query running for a long time]
MERGE (CACHE)-[:cacheContains]->(j)
return j
Get data from the cache:
MATCH (CACHE:Cache {name: 'cachequery'})-[:cacheContains]->(j)
WITH collect(j) as CACHE
...and do something with the data
Or you can do it through the label:
match (j:Person)-[..........complex query running for a long time]
SET j :CacheQuery
return j
Get data from the cache:
MATCH (j:Person:CacheQuery)
WITH collect(j) as CACHE
...and do something with the data
Upvotes: 2