# 模型关系 (Relationship)

## 概述

**关系（Relationship）** 用于定义不同语义模型（Cube）之间的 join 方式。通过建立 Cube 之间的关系，可以实现数据的无缝集成，方便用户创建高效、准确的数据视图（View）。

## 关系的类型

1. **一对一（One-to-One，1-1）：**&#x6BCF;个 Cube A 中的记录与 Cube B 中的记录一一对应。场景示例：一个用户档案表和用户详细信息表，每个用户仅对应一条详细信息。
2. **一对多（One-to-Many，1-n）或 多对一（Many-to-One，n-1）：**&#x43;ube A 中的一条记录可以关联 Cube B 中的多条记录。场景示例：一个客户对应多张订单，客户表与订单表之间是一对多关系。

## 为什么要建立关系？

**1. 用于视图（View）创建**

* 支持在视图中聚合并展示来自多个 Cube 的数据。
* 提前定义好数据关联，提高 BI 工具展示和查询的性能。

**2. 保障数据一致性和完整性**

* 确保不同数据集之间的正确关联，避免数据孤岛。
* 降低数据冗余，便于模型维护和更新。

**3. 优化查询性能**

* 通过预定义的关系减少运行时复杂 Join 的计算压力。
* 依赖已建立的索引和优化逻辑，提高数据检索效率。

## 举例说明

如果你有一个 **客户 Cube** 和一个 **订单 Cube**，你可以通过客户 ID 建立“一对多”关系：

* 一个客户对应多个订单。
* 你可以在视图里直接分析每个客户的订单总额、下单频次等指标。
* 查询性能更好，不需要临时去写复杂的 Join。

## 定义关系

在 Recurve 中定义 Cube 之间的关系，可以按照以下步骤操作：

{% hint style="warning" %}
在创建关系之前，须为 Cube 设置好主键（Primary Key）。参考文档 [语义层模型 (Cube)](/cn-reorc-help-center/semantics_modeling/yu-yi-ceng-mo-xing-cube.md#chuang-jian-cube)
{% endhint %}

1. **点击「编辑关系」按钮**
   1. 在 Cube 页面中，点击 **「编辑关系（Edit Relationship）」** 按钮，系统将弹出「关系配置窗口（Relationship Modal）」。

2. **新增关系**

* 点击 **「添加关系（Add Relationship）」** 按钮，开始配置新的关系行。
* 在新建的行中，选择以下信息：
  * **关系类型（Relationship Type）**\
    例如：一对一（1-1）、一对多（1-n）
  * **来源字段（From）**\
    选择当前 Cube 中作为关联字段的列
  * **目标字段（To）**\
    选择要关联的目标 Cube 以及其字段
* 配置完成后，点击 **「添加（Add）」** 按钮。

3. **保存关系**\
   所有关系配置完成后，点击 **「保存（Save）」** 按钮，保存已定义的所有关系。

***

#### 小提示

* 建议先理清 Cube 之间的逻辑关系，确认好主键、外键设置，避免数据连接错误。
* 配置完成后，可以在 View 或 BI 工具中直接使用已建立的关系，快速分析跨表数据。


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.reorc.com/cn-reorc-help-center/semantics_modeling/mo-xing-guan-xi-relationship.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
