0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Azure SDK for python (cosmos DB): partition_key を指定して、パフォーマンスへの悪影響を避ける

Posted at

背景

Cosmos DB に対して SQL 調査していた際に出たエラーの対処記録

具体的なエラーは以下

method:

  • container_proxy.query_items

SQL:

  • aggregation を利用 (AVG)
実際のエラー
Query contained NonValueAggregate, which the calling client does not support.
ActivityId: {ActivityId}, Microsoft.Azure.Documents.Common/2.14.0
Code: BadRequest
Message: Query contains 1 or more unsupported features. Upgrade your SDK to a version that does support the requested features:
Query contained NonValueAggregate, which the calling client does not support.
ActivityId: {ActivityId}, Microsoft.Azure.Documents.Common/2.14.0

結論

partition_key をきちんと指定してやる

そもそも、指定してないとパフォーマンスに影響がある

今回は、query_items() だったが、他のメソッドでも同じ

詳細

エラーメッセージ的には、SDK update?

最新にしても特に変わらず。

Azure Portal では正常に動くのも確認。

ってことで、以下が疑われる

  1. SDK の問題
  2. 使い方の問題
    で、こっちを調べてみたら解決

partition_key を省略するとどうなるのか?

ドキュメントを見てみると、aio の場合、省略すると、cross-partition query になるって書いてある。

  • aio の partition_key
    2024-10-04_11h35_24.png

  • sync の partition_key
    2024-10-04_11h35_33.png

しかしながら enable_cross_partition_query は、aio には存在しない :thinking:

  • aio では、エラーになる。以下で、エラーについて記述してた

  • sync にはあって、sample 等では、True に設定されている
    2024-10-04_11h46_42.png

では、そもそも、cross_partition_query ってなに?って話だが・・ 以下を見ると、

  • パーティションを横断してクエリを実行するってこと

2024-10-04_11h55_31.png

  • で、パーティションは以下の図に詳しい
    そう、きちんと、container 内で partition_key を使っていれば高速になるよねって話

何故、enable_cross_partition_query が、aio にないのか?

指定してないなら、cross にするよーって話。ドキュメントに記載の話
2024-10-04_12h15_07.png

何故、aio で、cross_partition_query がダメなのか?

省略してても、普通に動く query はあることから、

Aggregation Query だから unsupported ってことだと思われる。

それについて根拠は見つけられなかったので、とりあえず今回は諦め


公式ドキュメント

SDK reference of query_items()

  • aio 用

参考

エラー内容に関連した github issues

それっぽかったけど、2019 なんで、片付いている印象

2024-10-04_10h48_53.png

genspark に対処法を聞いてみた結果

非常にいい感じなんだけど、答えまでは行きつけてない

2024-10-04_10h48_07.png

Cross Partition Query について


あとがき

サンプルを元にやってたから、partition key 指定してなかったことが多かったけど、
ちゃんと設定していこうと改めて思いましたとさ :sweat:

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?