jjg
jjg

Reputation: 1024

Neo4j WHERE IN queries

I'm a graph DB relative n00b, have been playing with Neo4j (via Ruby ActiveRecord, but I don't think that's relevant), very much enjoying it but have found some counter-intuitive behaviour.

My problem involves getting some data from the DB, pulling it back to my Ruby code, doing some processing, then making a second query with the set of ids identified by this external processing (it is too complicated to do in the DB). The result is an ActiveRecord query like

Foo.where(foo_id: foo_ids)...

which translates to a Cypher query like

MATCH (foo:Foo) WHERE foo.foo_id IN [1, 5, ...] ...

The set of ids is largish, a few tens of thousands, and I'm finding this query really rather slow. The foo_id has a unique constraint, and my experience in RDBMSs shouts that it shouldn't be. Is this just a the way things are or am I ignorant of the proper "graphy" way of doing things?

Upvotes: 0

Views: 80

Answers (1)

TheTeacher
TheTeacher

Reputation: 510

you need to create index on foo_id . After that , instead of checking in array you can unwind that array and match like following

UNWIND [1,4,5,.....] as id 
MATCH (foo : Foo{foo_id:id}) 
RETURN foo 

Upvotes: 2

Related Questions