Neo4j Graph Database Fundamentals · บทเรียน

การปรับโครงสร้างและพัฒนาโมเดลกราฟ

เรียนรู้การพัฒนาโมเดลกราฟ Neo4j อย่างปลอดภัยในระยะยาวด้วยรูปแบบการปรับโครงสร้างและคำค้นที่จัดรูปใหม่

บทเรียน 4 จาก 413 ขั้นตอน

การปรับโครงสร้างและพัฒนาโมเดลกราฟ เป็นบทเรียน Neo4j Graph Database Fundamentals ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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
เริ่มต้นได้ฟรี

เรียนรู้ Neo4j Graph Database Fundamentals ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
12
บทเรียน
48

คำถามที่พบบ่อย

บทเรียน “การปรับโครงสร้างและพัฒนาโมเดลกราฟ” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การปรับโครงสร้างและพัฒนาโมเดลกราฟ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Neo4j Graph Database Fundamentals ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Neo4j Graph Database Fundamentals มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การปรับโครงสร้างและพัฒนาโมเดลกราฟ”

เรียนรู้การพัฒนาโมเดลกราฟ Neo4j อย่างปลอดภัยในระยะยาวด้วยรูปแบบการปรับโครงสร้างและคำค้นที่จัดรูปใหม่ คุณปฏิบัติ Neo4j Graph Database Fundamentals ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Neo4j Graph Database Fundamentals หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Neo4j Graph Database Fundamentals บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “การปรับโครงสร้างและพัฒนาโมเดลกราฟ” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Neo4j Graph Database Fundamentals นี้ได้ไหม

ได้ บทเรียน Neo4j Graph Database Fundamentals ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. หลักการสร้างแบบจำลองข้อมูลกราฟ
  2. การออกแบบแบบจำลองกราฟแรกของคุณ
  3. ข้อจำกัดสคีมาและดัชนี
  4. การปรับโครงสร้างและพัฒนาโมเดลกราฟ
← กลับไปที่ Neo4j Graph Database Fundamentals