I just recently started with graph databases and Neo4j. I have created nodes with the following labels: Person, Company, City, Skill which have one or more relationships in between them.
I am trying to do a cypher query which finds one Person node (by node id) and then I want to find all the skills, Companies etc related to this Person. This is my query so far:
MATCH (person:Person)
WHERE ID (person) = 123
WITH person LIMIT 1
OPTIONAL MATCH (p:Person) - [:HAS_SKILL] -> (skill:Skill)
WHERE ID (p) = 123
WITH person, skill ORDER BY skill.name ASC
OPTIONAL MATCH (p:Person) - [:WORKED_AT] -> (company:Company)
WHERE ID (p) = 123
WITH person, skill, company ORDER BY company.name ASC
OPTIONAL MATCH (p:Person) - [:LIVES_IN] -> (city:City)
WHERE ID (p) = 123
WITH person
, collect(DISTINCT skill) as skills
, collect(DISTINCT company) as companies
, city LIMIT 1
RETURN person, skills, companies, city
I'm concerned that this is not really an optimal query and it looks like it will be really slow if/when we get lets say a couple of million nodes. Can someone tell me a better way of structuring this kind of query? Should I divide it up to several smaller queries?