Nipun
Nipun

Reputation: 4319

Neo4j cypher query takes a lot of time

Can anyone please let me know what is wrong with this query

MATCH (I1:Interface), (I2:Interface) WHERE I1.IfIPAddress = '172.16.42.9' AND I2.IfIPAddress = '172.16.42.10' WITH Count(I1) + Count(I2) AS iCount, I1, I2 WHERE iCount = 2
return iCount;

where there are 5000 interface nodes. Why does it take a lot of time for the query to execute and still it does not executes?

Upvotes: 0

Views: 105

Answers (1)

cybersam
cybersam

Reputation: 66967

If you simply want to test if both Interface instances exist, you can just do this:

MATCH (i1:Interface {IfIPAddress:'172.16.42.9'}), (i2:Interface {IfIPAddress:'172.16.42.10'})
RETURN i1, i2;

If both exist, then a single row (with the i1 and i2 nodes) will be returned, otherwise 0 rows will be returned.

Also, to speed up these queries, you should either create an index, or a uniqueness constraint (if no two Interface nodes can have the same IfIPAddress value).

To create the index:

CREATE INDEX ON :Interface(IfIPAddress);

To create the uniqueness constraint instead (which also automatically creates the above index for you):

CREATE CONSTRAINT ON (i:Interface)
   ASSERT i.IfIPAddress IS UNIQUE;

Upvotes: 1

Related Questions