Query plan cache does not auto‑invalidate after schema changes
0 reputation · 16 May 2022, 07:27 UTC
0 reputation · 16 May 2022, 07:27 UTC
Ensure that Cypher queries reflect the latest schema state without manual intervention.
Neo4j’s plan cache stores compiled execution plans to avoid re‑planning. The cache is not automatically invalidated when indexes, constraints, or labels are added or removed. Consequently, a query that previously used an index may continue to use a stale plan, potentially omitting newly indexed properties or applying an outdated index strategy. Administrators must explicitly clear the cache with CALL dbms.clearQueryCache() or restart the instance, which can impact throughput.
34775 reputation · 16 May 2022, 13:35 UTC
In current stable versions of Neo4j (including 5.x), the query plan cache does not automatically invalidate when indexes, constraints, or labels are modified. To ensure queries utilize the latest schema state, you must manually clear the cache using the following command:
CALL dbms.clearQueryCache();
Alternatively, a full restart of the Neo4j instance will clear the cache, though this is generally less desirable than the targeted procedure call.
Neo4j compiles Cypher queries into execution plans to avoid the overhead of repeated planning. These plans are stored in a cache. Because the cache is not linked to a schema-change trigger, a query that was planned before a new index was created will continue to use the old plan (e.g., performing a NodeByLabelScan instead of a NodeIndexSeek).
To verify if a stale plan is affecting your performance, follow this sequence:
PROFILE on your target query to see if it is ignoring a newly created index.CALL dbms.queryCacheStats() to observe the current cache state.CALL dbms.clearQueryCache() and run the PROFILE command again. If the execution plan changes to use the new index, the cache was indeed the bottleneck.To provide a more specific recommendation regarding throughput impact, please specify your current Neo4j version and average queries-per-second (QPS). Clearing the cache in extremely high-traffic environments can cause a temporary CPU spike as the system re-plans all active queries simultaneously.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.