[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-09-03。"],[],[],null,["# Best practices for tuning Spanner Graph queries\n\n| **Note:** This feature is available with the Spanner Enterprise edition and Enterprise Plus edition. For more information, see the [Spanner editions overview](/spanner/docs/editions-overview).\n\n\u003cbr /\u003e\n\nThis document describes best practices for tuning Spanner Graph query\nperformance, which include the following optimizations:\n\n- Avoid a full scan of the input table for nodes and edges.\n- Reduce the amount of data the query needs to read from storage.\n- Reduce the size of intermediate data.\n\nStart from lower cardinality nodes\n----------------------------------\n\nWrite the path traversal so that it starts with the lower cardinality nodes.\nThis approach keeps the intermediate result set small, and speeds up query\nexecution.\n\nFor example, the following queries have the same semantics:\n\n- Forward edge traversal:\n\n GRAPH FinGraph\n MATCH (p:Person {name:\"Alex\"})-[:Owns]-\u003e(a:Account {is_blocked: true})\n RETURN p.id AS person_id, a.id AS account_id;\n\n- Reverse edge traversal:\n\n GRAPH FinGraph\n MATCH (a:Account {is_blocked:true})\u003c-[:Owns]-(p:Person {name: \"Alex\"})\n RETURN p.id AS person_id, a.id AS account_id;\n\nAssuming that there are fewer people with the name `Alex` than there are\nblocked accounts, we recommend that you write this query in the forward\nedge traversal.\n\nStarting from lower cardinality nodes is especially important for\nvariable-length path traversal. The following example shows the recommended way\nto find accounts that are within three transfers of a given account. \n\n GRAPH FinGraph\n MATCH (:Account {id: 7})-[:Transfers]-\u003e{1,3}(a:Account)\n RETURN a.id;\n\nSpecify all labels by default\n-----------------------------\n\nSpanner Graph infers the qualifying nodes and edge labels if\nlabels are omitted. We recommend that you specify labels for all nodes and edges\nwhere possible, because this inference might not always be possible and it might\ncause more labels than necessary to be scanned.\n\n### Single MATCH statement\n\nThe following example finds accounts linked by at most 3 transfers from the\ngiven account: \n\n GRAPH FinGraph\n MATCH (src:Account {id: 7})-[:Transfers]-\u003e{1,3}(dst:Account)\n RETURN dst.id;\n\n### Across MATCH statements\n\nSpecify labels on nodes and edges when they refer to the same element but are\nacross `MATCH` statements.\n\nThe following example shows this recommended approach: \n\n GRAPH FinGraph\n MATCH (acct:Account {id: 7})-[:Transfers]-\u003e{1,3}(other_acct:Account)\n RETURN acct, COUNT(DISTINCT other_acct) AS related_accts\n GROUP BY acct\n\n NEXT\n\n MATCH (acct:Account)\u003c-[:Owns]-(p:Person)\n RETURN p.id AS person, acct.id AS acct, related_accts;\n\nWhat's next\n-----------\n\n- Learn how to [query property graphs in Spanner Graph](/spanner/docs/graph/queries-overview).\n- [Migrate to Spanner Graph](/spanner/docs/graph/migrate)."]]