

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# Neptune 如何使用语句索引处理 Gremlin 查询
<a name="gremlin-explain-background-indexing-examples"></a>

在 Amazon Neptune 中，可以通过三种语句索引访问语句，如[如何在 Neptune 中为语句编制索引](feature-overview-storage-indexing.md)中所述。Neptune 从 Gremlin 查询中提取语句*模式*，其中一些位置是已知的，其余的则留待索引搜索发现。

Neptune 假定属性图架构的大小不大。这意味着不同边缘标签和属性名称的数量相当少，导致不同谓词的总数较少。Neptune 在单独的索引中跟踪不同的谓词。它使用该谓词缓存来进行 `{ all P x POGS }` 的联合扫描，而不是使用 OSGP 索引。避免使用反向遍历 OSGP 索引可节省存储空间和加载吞吐量。

Neptune Gremlin Explain/Profile API 允许你在图表中获取谓词计数。然后，您可以确定您的应用程序是否使 Neptune 的“属性图架构较小”的假设失效。

以下示例有助于说明 Neptune 如何使用索引来处理 Gremlin 查询。

**问题：顶点 `v1` 有哪些标签？**

```
  Gremlin code:      g.V('v1').label()
  Pattern:           (<v1>, <~label>, ?, ?)
  Known positions:   SP
  Lookup positions:  OG
  Index:             SPOG
  Key range:         <v1>:<~label>:*
```

**问题：顶点 `v1` 有哪些“已知”的出边？**

```
  Gremlin code:      g.V('v1').out('knows')
  Pattern:           (<v1>, <knows>, ?, ?)
  Known positions:   SP
  Lookup positions:  OG
  Index:             SPOG
  Key range:         <v1>:<knows>:*
```

**问题：哪些顶点具有 `Person` 顶点标签？**

```
  Gremlin code:      g.V().hasLabel('Person')
  Pattern:           (?, <~label>, <Person>, <~>)
  Known positions:   POG
  Lookup positions:  S
  Index:             POGS
  Key range:         <~label>:<Person>:<~>:*
```

**问题：给定边`e1`的 from/to 顶点是什么？**

```
  Gremlin code:      g.E('e1').bothV()
  Pattern:           (?, ?, ?, <e1>)
  Known positions:   G
  Lookup positions:  SPO
  Index:             GPSO
  Key range:         <e1>:*
```

Neptune 所**没有** 的一个声明索引是反向遍历 OSGP 索引。该索引可用于收集所有边缘标签的所有入边，如以下示例所示。

**问题：什么是传入的相邻顶点 `v1`？**

```
  Gremlin code:      g.V('v1').in()
  Pattern:           (?, ?, <v1>, ?)
  Known positions:   O
  Lookup positions:  SPG
  Index:             OSGP  // <-- Index does not exist
```