次の方法で共有


Azure Cosmos DB for Apache Gremlin でグラフ データに対してクエリを実行する

Important

99.999% 可用性サービス レベル アグリーメント (SLA)、インスタント 自動スケール、および複数のリージョン間の自動フェールオーバーを使用した 大規模 なシナリオ向けのデータベース ソリューションをお探しですか? Azure Cosmos DB for NoSQL について考えてみましょう。

オンライン分析処理 (OLAP) グラフを実装するか、既存の Apache Gremlin アプリケーションを移行しますか? Microsoft Fabric で Graph を検討してください。

Azure Cosmos DB for Apache Gremlin では、クエリの Gremlin TinkerPop 構文がサポートされています。 このガイドでは、このサービスを使用して実行できる一般的なクエリについて説明します。 このガイドでは、 Gremlin コンソールまたはお気に入りの Gremlin ドライバーを使用して、次のクエリを実行できます。

[前提条件]

  • Azure サブスクリプション

    • Azure サブスクリプションをお持ちでない場合は、開始する前に 無料アカウント を作成してください。
  • Azure Cosmos DB for Apache Gremlin アカウント
  • テスト用のサンプル データへのアクセス

グラフ内の頂点の数をカウントする

グラフ内の積の頂点の合計数をカウントします。 この操作は、製品カタログのサイズを理解したり、データの読み込みを検証したりするのに役立ちます。

g.V().hasLabel('product').count()

グラフ内の特定のラベルを持つ頂点の数をカウントする

特定のラベルを含むグラフ内の製品頂点の合計数をカウントします。 この例では、ラベルは product

g.V().hasLabel('product').count()

ラベルとプロパティで製品をフィルター処理する

特定のラベルとプロパティ値に一致する製品を取得します。 このクエリは、価格が 800 ドルを超える製品など、関心のあるサブセットに結果を絞り込む場合に役立ちます。

g.V().hasLabel('product').has('price', gt(800))

製品からのプロジェクト固有のプロパティ

一致した製品から選択したプロパティのみを返します。 このクエリは、返されるデータの量を減らし、製品名などの関連フィールドに重点を置いています。

g.V().hasLabel('product').values('name')

グラフを走査して関連製品を検索します。 たとえば、特定の製品に置き換えられたすべての製品を検索するには、発信 "置換" エッジを走査し、接続された積の頂点に移動します。

g.V(['gear-surf-surfboards', 'bbbbbbbb-1111-2222-3333-cccccccccccc']).outE('replaces').inV().hasLabel('product')

このクエリを使用して、交換チェーン内の 2 ホップ離れた製品を検索します。

g.V(['gear-surf-surfboards', 'bbbbbbbb-1111-2222-3333-cccccccccccc']).outE('replaces').inV().hasLabel('product').outE('replaces').inV().hasLabel('product')

実行プロファイルを使用してクエリ実行を分析する

executionProfile()ステップを使用して、Gremlin クエリのパフォーマンスと実行の詳細を分析します。 この手順では、クエリ内の各ステップのメトリックを含む JSON オブジェクトが返されます。これは、トラブルシューティングと最適化に役立ちます。

g.V(['gear-surf-surfboards', 'bbbbbbbb-1111-2222-3333-cccccccccccc']).out().executionProfile()
[
  {
    "gremlin": "g.V('mary').out().executionProfile()",
    "totalTime": 28,
    "metrics": [
      {
        "name": "GetVertices",
        "time": 24,
        "annotations": { "percentTime": 85.71 },
        "counts": { "resultCount": 2 },
        "storeOps": [ { "fanoutFactor": 1, "count": 2, "size": 696, "time": 0.4 } ]
      },
      {
        "name": "GetEdges",
        "time": 4,
        "annotations": { "percentTime": 14.29 },
        "counts": { "resultCount": 1 },
        "storeOps": [ { "fanoutFactor": 1, "count": 1, "size": 419, "time": 0.67 } ]
      },
      {
        "name": "GetNeighborVertices",
        "time": 0,
        "annotations": { "percentTime": 0 },
        "counts": { "resultCount": 1 }
      },
      {
        "name": "ProjectOperator",
        "time": 0,
        "annotations": { "percentTime": 0 },
        "counts": { "resultCount": 1 }
      }
    ]
  }
]

executionProfile()ステップの詳細については、実行プロファイルリファレンスを参照してください

ヒント

executionProfile()ステップでは、Gremlin クエリが実行されます。 このクエリには、 addV または addE の手順が含まれています。これにより、クエリで指定された変更が作成およびコミットされます。 Gremlin クエリによって生成された要求ユニットも課金されます。

ブラインド ファンアウト クエリ パターンを識別する

ブラインド ファンアウトは、多くの場合、パーティション キー述語がないために、クエリが必要以上に多くのパーティションにアクセスするときに発生します。 このアンチパターンにより、待機時間とコストが増加する可能性があります。 実行プロファイルは、高い fanoutFactorを示すことによって、このようなパターンを識別するのに役立ちます。

g.V(['gear-surf-surfboards', 'aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb']).executionProfile()
[
  {
    "gremlin": "g.V('tt0093640').executionProfile()",
    "totalTime": 46,
    "metrics": [
      {
        "name": "GetVertices",
        "time": 46,
        "annotations": { "percentTime": 100 },
        "counts": { "resultCount": 1 },
        "storeOps": [ { "fanoutFactor": 5, "count": 1, "size": 589, "time": 75.61 } ]
      },
      {
        "name": "ProjectOperator",
        "time": 0,
        "annotations": { "percentTime": 0 },
        "counts": { "resultCount": 1 }
      }
    ]
  }
]

ファンアウト クエリの最適化

高い fanoutFactor (5 など) は、クエリが複数のパーティションにアクセスしたことを示します。 最適化するには、クエリ述語にパーティション キーを含めます。

g.V(['gear-surf-surfboards', 'aaaaaaaa-0000-1111-2222-bbbbbbbbbbbb'])

フィルター処理されていないクエリ パターン

フィルター処理されていないクエリでは、大規模な初期データセットが処理され、コストと待機時間が増加する可能性があります。

g.V().hasLabel('product').out().executionProfile()
[
  {
    "gremlin": "g.V().hasLabel('tweet').out().executionProfile()",
    "totalTime": 42,
    "metrics": [
      {
        "name": "GetVertices",
        "time": 31,
        "annotations": { "percentTime": 73.81 },
        "counts": { "resultCount": 30 },
        "storeOps": [ { "fanoutFactor": 1, "count": 13, "size": 6819, "time": 1.02 } ]
      },
      {
        "name": "GetEdges",
        "time": 6,
        "annotations": { "percentTime": 14.29 },
        "counts": { "resultCount": 18 },
        "storeOps": [ { "fanoutFactor": 1, "count": 20, "size": 7950, "time": 1.98 } ]
      },
      {
        "name": "GetNeighborVertices",
        "time": 5,
        "annotations": { "percentTime": 11.9 },
        "counts": { "resultCount": 20 },
        "storeOps": [ { "fanoutFactor": 1, "count": 4, "size": 1070, "time": 1.19 } ]
      },
      {
        "name": "ProjectOperator",
        "time": 0,
        "annotations": { "percentTime": 0 },
        "counts": { "resultCount": 20 }
      }
    ]
  }
]

フィルター処理されたクエリ パターン

トラバーサルの前にフィルターを追加すると、ワーキング セットを減らし、パフォーマンスを向上させることができます。 実行プロファイルには、フィルター処理の効果が表示されます。 フィルター処理されたクエリでは処理される頂点が少なくなり、待機時間とコストが削減されます。

g.V().hasLabel('product').has('clearance', true).out().executionProfile()
[
  {
    "gremlin": "g.V().hasLabel('tweet').has('lang', 'en').out().executionProfile()",
    "totalTime": 14,
    "metrics": [
      {
        "name": "GetVertices",
        "time": 14,
        "annotations": { "percentTime": 58.33 },
        "counts": { "resultCount": 11 },
        "storeOps": [ { "fanoutFactor": 1, "count": 11, "size": 4807, "time": 1.27 } ]
      },
      {
        "name": "GetEdges",
        "time": 5,
        "annotations": { "percentTime": 20.83 },
        "counts": { "resultCount": 18 },
        "storeOps": [ { "fanoutFactor": 1, "count": 18, "size": 7159, "time": 1.7 } ]
      },
      {
        "name": "GetNeighborVertices",
        "time": 5,
        "annotations": { "percentTime": 20.83 },
        "counts": { "resultCount": 18 },
        "storeOps": [ { "fanoutFactor": 1, "count": 4, "size": 1070, "time": 1.01 } ]
      },
      {
        "name": "ProjectOperator",
        "time": 0,
        "annotations": { "percentTime": 0 },
        "counts": { "resultCount": 18 }
      }
    ]
  }
]