Mostafa Hassan
Mostafa Hassan

Reputation: 1

Neo4j Relationship Existence Query Have too Many DB Hits

I have a query that retrieves 1,500 customers and a promotion with a specific ID. The query then checks if those customers do not have this promotion.

My problem is that the query results in too many database hits at this step, around 120,000 hits:

WHERE NOT (customers)-[:HAS_PROMOTION]->(promo)

Here's the query

PROFILE
MATCH(promo:Promotion{id: "XXXX"})
WHERE promo.end_time > timestamp()
MATCH (c:Customer)
WITH promo, c AS customers SKIP 9000 LIMIT 1500
WHERE NOT (customers)-[:HAS_PROMOTION]->(promo)
RETURN customers.id

First part of profile Second part of profile

I tried to use exists function and it didn't make any difference and I also inverted the relation direction and it got faster by around 10%

Upvotes: 0

Views: 56

Answers (1)

cybersam
cybersam

Reputation: 67019

Depending on your data, it may be more efficient to first get all promoted customers, and then just return all other customers. For example:

MATCH(promo:Promotion{id: "XXXX"})<-[:HAS_PROMOTION]-(pc:Customer)
WHERE promo.end_time > timestamp()
WITH COLLECT(ID(pc)) AS promotedCustomers
MATCH (c:Customer)
WHERE NOT ID(c) IN promotedCustomers
RETURN c.id

Upvotes: 0

Related Questions