0Pricing
Neo4j Graph Database Fundamentals · 课时

重构与演进图模型

学习如何随着时间推移,使用重构模式和查询重塑安全地演进 Neo4j 图模型。

重构与演进图模型 是 CoddyKit 上的免费 Neo4j Graph Database Fundamentals 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Neo4j Graph Database Fundamentals 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Neo4j Graph Database Fundamentals 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Models Are Never Final

As applications grow, your graph model will change. New questions demand new structure. Refactoring lets you reshape the graph without losing data.

Property to Node

A common refactor: a property shared by many nodes (like a city name) becomes its own node so it can hold relationships.

MATCH (p:Person)
WHERE p.city IS NOT NULL
MERGE (c:City {name: p.city})
MERGE (p)-[:LIVES_IN]->(c)
REMOVE p.city;

Node to Property

The reverse refactor: collapse a rarely-traversed node back into a property when its relationships add no value.

MATCH (p:Person)-[r:LIVES_IN]->(c:City)
SET p.city = c.name
DELETE r;

Renaming Relationship Types

To rename a relationship type, create the new one and delete the old. Cypher cannot rename a type in place.

MATCH (a)-[old:KNOWS]->(b)
MERGE (a)-[:FRIEND]->(b)
DELETE old;

Adding a New Label

You can introduce a new label to a subset of nodes to support new queries or constraints.

MATCH (p:Person)
WHERE p.age >= 18
SET p:Adult;

Splitting a Node

When one node mixes two concerns (e.g. a user and their address), split it into linked nodes for clarity and reuse.

MATCH (u:User)
WHERE u.street IS NOT NULL
CREATE (a:Address {street: u.street, zip: u.zip})
MERGE (u)-[:HAS_ADDRESS]->(a)
REMOVE u.street, u.zip;

Merging Duplicate Nodes

Data imports can create duplicates. Use APOC or careful Cypher to merge them, reattaching all relationships to a single surviving node.

MATCH (a:Person {email: 'x@y.com'}), (b:Person {email: 'x@y.com'})
WHERE id(a) < id(b)
WITH a, b
CALL apoc.refactor.mergeNodes([a, b]) YIELD node
RETURN node;

Batching Large Refactors

Refactors over millions of nodes should run in batches to avoid huge transactions. APOC's periodic iterate helps.

CALL apoc.periodic.iterate(
  'MATCH (p:Person) WHERE p.city IS NOT NULL RETURN p',
  'MERGE (c:City {name: p.city}) MERGE (p)-[:LIVES_IN]->(c) REMOVE p.city',
  {batchSize: 1000}
);

Versioning Your Model

Keep refactor scripts in version control and apply them in order, like database migrations. This makes changes reproducible across environments.

Testing After Refactor

Always validate counts before and after. Compare relationship totals to ensure nothing was orphaned or lost.

MATCH ()-[r:LIVES_IN]->() RETURN count(r) AS livesInCount;

Refactor with Confidence

Back up first, refactor in batches, verify counts, and keep scripts versioned. A graph model is a living thing you improve over time.

Quick Check

Test your refactoring knowledge.

Recap

You learned to evolve graph models safely:

  • Convert properties to nodes and back
  • Rename relationship types by recreating them
  • Split nodes and merge duplicates
  • Batch large refactors with APOC
  • Version scripts and verify counts

常见问题解答

「重构与演进图模型」课时是免费的吗?

是的 — 「重构与演进图模型」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Neo4j Graph Database Fundamentals 课程的其余内容,请升级到 CoddyKit PRO。 Neo4j Graph Database Fundamentals 课程共包含 4 节课。

「重构与演进图模型」这节课中我会学到什么?

学习如何随着时间推移,使用重构模式和查询重塑安全地演进 Neo4j 图模型。 你通过在浏览器中直接运行的动手代码来练习 Neo4j Graph Database Fundamentals,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Neo4j Graph Database Fundamentals 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Neo4j Graph Database Fundamentals 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「重构与演进图模型」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Neo4j Graph Database Fundamentals 课中编写并运行代码吗?

能。每节 Neo4j Graph Database Fundamentals 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 图数据建模原则
  2. 设计第一个图模型
  3. 模式约束与索引
  4. 重构与演进图模型
← 返回 Neo4j Graph Database Fundamentals