CouchBase 索引嵌套元素
CouchBase Index nested elements
在 CouchBase(4.1.1) 中,以这两个示例为例,N1QL 可以使用整个元素创建索引。
假设我们有这样的文档结构:
{
"name": "blah",
"credentials": [
{
"level": 5,
"sector": "XP"
}
],
}
现在假设我们想根据名称和整个凭证元素创建索引 1,这可能吗?
类似于
create index indexName on `bucketName` (name, credentials) USING GSI;
或 index2 基于名称和嵌套字段之一,如级别;这怎么可能呢?像
create index indexName on `bucketName`(name, credential.levels) USING GSI;
当 运行 说明我的二级索引没有被使用并且 couchbase 默认为这个桶的主索引。
这是我正在使用的select。
select s.name, s.credentials
from `security` unnest s.credentials
where credentials is not missing and name = 'tom';
这是生成的说明:
{
"requestID": "f8d46eeb-3898-4ace-a24f-1582e0504eb7",
"signature": "json",
"results": [
{
"#operator": "Sequence",
"~children": [
{
"#operator": "PrimaryScan",
"index": "#primary",
"keyspace": "read",
"namespace": "default",
"using": "gsi"
},
{
"#operator": "Parallel",
"~child": {
"#operator": "Sequence",
"~children": [
{
"#operator": "Fetch",
"as": "s",
"keyspace": "bucketName",
"namespace": "default"
},
{
"#operator": "Unnest",
"as": "beacons",
"expr": "(`s`.`credentials`)"
},
{
"#operator": "Filter",
"condition": "((`s`.`name`) = \"tom\")"
},
{
"#operator": "InitialProject",
"result_terms": [
{
"expr": "(`s`.`id`)"
},
{
"expr": "`credentials`"
}
]
},
{
"#operator": "FinalProject"
}
]
}
}
]
}
],
"status": "success",
"metrics": {
"elapsedTime": "2.82063ms",
"executionTime": "2.765439ms",
"resultCount": 1,
"resultSize": 1917
}
}
以下查询应使用您的索引。
select s.name, s.credentials
from `security` s
where s.credentials is not missing and s.name = 'tom';
select s.name, credentials
from `security` s unnest s.credentials
where s.credentials is not missing and s.name = 'tom';
根据我的发现,如果您尝试创建索引,则必须排除不包含您正在建立索引的字段的文档。因此,对于我上面的示例,一旦我添加: name is not missing
在 where
语句中,我的查询开始使用我的二级索引。
下面来自 Couchbase site 的简介让我得出了这个发现
Attention: MISSING items are not indexed by indexers. To take advantage of covering indexes and for the index to qualify, a query needs to exclude documents where the index key expression evaluates to MISSING.
在 CouchBase(4.1.1) 中,以这两个示例为例,N1QL 可以使用整个元素创建索引。
假设我们有这样的文档结构:
{
"name": "blah",
"credentials": [
{
"level": 5,
"sector": "XP"
}
],
}
现在假设我们想根据名称和整个凭证元素创建索引 1,这可能吗?
类似于
create index indexName on `bucketName` (name, credentials) USING GSI;
或 index2 基于名称和嵌套字段之一,如级别;这怎么可能呢?像
create index indexName on `bucketName`(name, credential.levels) USING GSI;
当 运行 说明我的二级索引没有被使用并且 couchbase 默认为这个桶的主索引。
这是我正在使用的select。
select s.name, s.credentials
from `security` unnest s.credentials
where credentials is not missing and name = 'tom';
这是生成的说明:
{
"requestID": "f8d46eeb-3898-4ace-a24f-1582e0504eb7",
"signature": "json",
"results": [
{
"#operator": "Sequence",
"~children": [
{
"#operator": "PrimaryScan",
"index": "#primary",
"keyspace": "read",
"namespace": "default",
"using": "gsi"
},
{
"#operator": "Parallel",
"~child": {
"#operator": "Sequence",
"~children": [
{
"#operator": "Fetch",
"as": "s",
"keyspace": "bucketName",
"namespace": "default"
},
{
"#operator": "Unnest",
"as": "beacons",
"expr": "(`s`.`credentials`)"
},
{
"#operator": "Filter",
"condition": "((`s`.`name`) = \"tom\")"
},
{
"#operator": "InitialProject",
"result_terms": [
{
"expr": "(`s`.`id`)"
},
{
"expr": "`credentials`"
}
]
},
{
"#operator": "FinalProject"
}
]
}
}
]
}
],
"status": "success",
"metrics": {
"elapsedTime": "2.82063ms",
"executionTime": "2.765439ms",
"resultCount": 1,
"resultSize": 1917
}
}
以下查询应使用您的索引。
select s.name, s.credentials
from `security` s
where s.credentials is not missing and s.name = 'tom';
select s.name, credentials
from `security` s unnest s.credentials
where s.credentials is not missing and s.name = 'tom';
根据我的发现,如果您尝试创建索引,则必须排除不包含您正在建立索引的字段的文档。因此,对于我上面的示例,一旦我添加: name is not missing
在 where
语句中,我的查询开始使用我的二级索引。
下面来自 Couchbase site 的简介让我得出了这个发现
Attention: MISSING items are not indexed by indexers. To take advantage of covering indexes and for the index to qualify, a query needs to exclude documents where the index key expression evaluates to MISSING.