Cosmos DB - 高效查询以查找数组中的项目列表?
Cosmos DB - Efficient query to find list of items in an array?
考虑以下文档:
{
"id": "0001",
"name": "product 1",
"description": "product 1 description",
"categories": ["category1", "category2"]
}
{
"id": "0002",
"name": "product 2",
"description": "product 2 description",
"categories": ["category2", "category3"]
}
{
"id": "0003",
"name": "product 3",
"description": "product 1 description",
"categories": ["category1", "category4"]
}
我想要做的是找到产品类别可以是 category2
或 category3
的产品。
为了获取它,我编写了以下查询:
Select * from Root r where
exists(select value c from c in r.categories where c in ("category2", "category3"))
上面的查询运行良好,给了我想要的结果。
我想知道是否可以改进此查询?
array_contains
查找数组中的单个项目,您要查找的项目是第二个参数。所以你需要:
select *
from c
where array_contains(c.categories, "category2")
or array_contains(c.categories, "category3")
再看看性能
考虑以下文档:
{
"id": "0001",
"name": "product 1",
"description": "product 1 description",
"categories": ["category1", "category2"]
}
{
"id": "0002",
"name": "product 2",
"description": "product 2 description",
"categories": ["category2", "category3"]
}
{
"id": "0003",
"name": "product 3",
"description": "product 1 description",
"categories": ["category1", "category4"]
}
我想要做的是找到产品类别可以是 category2
或 category3
的产品。
为了获取它,我编写了以下查询:
Select * from Root r where
exists(select value c from c in r.categories where c in ("category2", "category3"))
上面的查询运行良好,给了我想要的结果。
我想知道是否可以改进此查询?
array_contains
查找数组中的单个项目,您要查找的项目是第二个参数。所以你需要:
select *
from c
where array_contains(c.categories, "category2")
or array_contains(c.categories, "category3")
再看看性能