我有一个奇怪的例子,我的couchbase服务器中有一个索引,它是用命令构建的:
CREATE INDEX `idx_metadata` ON `dev`(`email`,`nickname`,`channelUuid`) WHERE (`type` = "user_metadata") WITH { "defer_build":true }`
如果我运行查询:
SELECT channelUuid FROM `dev` WHERE type="user_metadata" AND email="foo" AND nickname="bar"
然后它使用索引并按预期工作。
如果我简单地将AND更改为或类似:
SELECT channelUuid FROM `dev` WHERE type="user_metadata" AND (email="foo" OR nickname="bar")
然后它使用主索引,而不是专用索引。
根据要求,这里是对和查询的解释
{
"plan": {
"#operator": "Sequence",
"~children": [
{
"#operator": "IndexScan2",
"index": "idx_metadata",
"index_id": "d6e2fb94ae221335",
"index_projection": {
"primary_key": true
},
"keyspace": "dev",
"namespace": "default",
"spans": [
{
"exact": true,
"range": [
{
"high": "\"test1@my-email.com\"",
"inclusion": 3,
"low": "\"test1@my-email.com\""
},
{
"high": "\"Badger\"",
"inclusion": 3,
"low": "\"Badger\""
}
]
}
],
"using": "gsi"
},
{
"#operator": "Fetch",
"keyspace": "dev",
"namespace": "default"
},
{
"#operator": "Parallel",
"~child": {
"#operator": "Sequence",
"~children": [
{
"#operator": "Filter",
"condition": "(((`dev`.`type`) = \"user_metadata\") and (((`dev`.`email`) = \"test1@my-email.com\") and ((`dev`.`nickname`) = \"Badger\")))"
},
{
"#operator": "InitialProject",
"result_terms": [
{
"expr": "self",
"star": true
}
]
},
{
"#operator": "FinalProject"
}
]
}
}
]
},
"text": "SELECT * FROM `dev` WHERE type=\"user_metadata\" AND (email=\"test1@my-email.com\" AND nickname=\"Badger\");"
}
对于OR查询:
{
"plan": {
"#operator": "Sequence",
"~children": [
{
"#operator": "PrimaryScan",
"index": "#primary",
"keyspace": "dev",
"namespace": "default",
"using": "gsi"
},
{
"#operator": "Fetch",
"keyspace": "dev",
"namespace": "default"
},
{
"#operator": "Parallel",
"~child": {
"#operator": "Sequence",
"~children": [
{
"#operator": "Filter",
"condition": "(((`dev`.`type`) = \"user_metadata\") and (((`dev`.`email`) = \"test1@my-email.com\") or ((`dev`.`nickname`) = \"Badger\")))"
},
{
"#operator": "InitialProject",
"result_terms": [
{
"expr": "self",
"star": true
}
]
},
{
"#operator": "FinalProject"
}
]
}
}
]
},
"text": "SELECT * FROM `dev` WHERE type=\"user_metadata\" AND (email=\"test1@my-email.com\" OR nickname=\"Badger\");"
}