You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何高效获取百万邻居节点的所有Cypher关系类型?

Optimizing Your Cypher Query for Distinct Relationship Types

Nice question—dealing with nodes that have hundreds of thousands of neighbors can definitely lead to slow queries if you're not leveraging Neo4j's optimizations. Let's break down why your current approach is taking 18 seconds and how to fix it.

Why Your Current Query Is Slow

Your existing query:

MATCH (n:Label {indexedProperty:"value"}) MATCH (n)-[r]-() RETURN distinct type(r)

has to traverse all 1 million relationships attached to your node, then deduplicate their types. That's a ton of unnecessary work when you only care about the unique relationship types, not the individual relationships themselves.

The Fastest Fix: Use Neo4j's Built-in Metadata Procedure

Neo4j has a built-in procedure that directly fetches all distinct relationship types linked to a node without iterating through every single relationship. This is way more efficient because it pulls the data straight from the database's internal metadata about the node's connections.

Try this query instead:

MATCH (n:Label {indexedProperty:"value"})
WITH n
CALL db.node.relationshipTypes(n) YIELD relationshipType
RETURN relationshipType

This should drop your runtime from 18 seconds to milliseconds—since it only needs to return the ~6 unique types, not process 1M rows.

Additional Optimizations to Verify

  • Confirm Index Usage
    Make sure your initial lookup of node n is using the indexed property correctly. Run EXPLAIN on your original query:

    EXPLAIN MATCH (n:Label {indexedProperty:"value"}) MATCH (n)-[r]-() RETURN distinct type(r)
    

    Look for an Index Seek in the execution plan. If you see an AllNodesScan instead, create or verify your index:

    CREATE INDEX IF NOT EXISTS FOR (n:Label) ON (n.indexedProperty);
    
  • Optimize Without the Built-in Procedure (Older Neo4j Versions)
    If you're stuck on a version that doesn't support db.node.relationshipTypes(), you can still speed up the original query by aggregating early to reduce data processing:

    MATCH (n:Label {indexedProperty:"value"})
    OPTIONAL MATCH (n)-[r]-()
    WITH DISTINCT type(r) AS relType
    WHERE relType IS NOT NULL
    RETURN relType
    

    This still traverses all relationships, but the early DISTINCT cuts down on data size earlier in the pipeline.

  • Filter by Relationship Direction (If Needed)
    If you only need incoming or outgoing relationships, use the direction-specific variants of the procedure:

    // For outgoing relationships only
    MATCH (n:Label {indexedProperty:"value"})
    CALL db.node.outgoingRelationshipTypes(n) YIELD relationshipType
    RETURN relationshipType
    
    // For incoming relationships only
    MATCH (n:Label {indexedProperty:"value"})
    CALL db.node.incomingRelationshipTypes(n) YIELD relationshipType
    RETURN relationshipType
    

Always use PROFILE to compare execution plans between queries—it's the best way to confirm exactly where the time is being spent and validate improvements.

内容的提问来源于stack exchange,提问作者HenrikRüping

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:37:56