I’m hoping someone can shed some light on why it does not appear that my indexes are being used by couchbase propertly. I have created the indices using the N1QL command line, I’m running Couchbase 3.0.1 on Mac OSX, and I’m running N1QL DP4.
I have documents in the “default” bucket that look something like this:
{
"_type":"board",
"tId":"team::1"
}
And another document that might look like this
{
"_type":"card",
"bId":"board::1"
}
And another like:
{
"_type":"cxn",
"cId: "card::1",
"tId": "team::1",
"cxns": ["board::1", "board::2"]
}
I have created 3 indexes to account for my 3 different types of documents.
Index 1: Index the _type fields only
cbq > CREATE INDEX typeIndex ON default (_type);
Index 2: Index the “_type” and “tId” fields
cbq > CREATE INDEX typeTeamId ON default (_type, tId);
Index 3: Index the “_type”, “tId” and “cxns” fields
cbq > CREATE INDEX typeTeamIdCxns ON default (_type, tId, cxns);
Now, I would expect that issuing a query that is only referencing the _type field, it would use the “typeIndex” just created, but instead I see the typeTeamIdCxns being referenced, with the only span checking on the _type parameter.
cbq> explain select * from default where _type = 'board';
{
"requestID": "e1a7bc84-86da-4618-b694-5acd7e296f22",
"signature": "json",
"results": [
{
"#operator": "Sequence",
"~children": [
{
"#operator": "IndexScan",
"index": "typeTeamIdCxns",
"keyspace": "default",
"limit": 9.223372036854776e+18,
"namespace": "default",
"spans": [
{
"Range": {
"High": [
"board"
],
"Inclusion": 3,
"Low": [
"board"
]
},
"Seek": null
}
]
},
{
"#operator": "Parallel",
"~child": {
"#operator": "Sequence",
"~children": [
{
"#operator": "Fetch",
"keyspace": "default",
"namespace": "default"
},
{
"#operator": "Filter",
"condition": "((`default`.`_type`) = \"board\")"
},
{
"#operator": "InitialProject",
"result_terms": [
{
"star": true
}
]
},
{
"#operator": "FinalProject"
}
]
}
}
]
}
],
"status": "success",
"metrics": {
"elapsedTime": "1.217282ms",
"executionTime": "1.109197ms",
"resultCount": 1,
"resultSize": 1990
}
}
Making a slightly more complex call, including both the _type and the tId, only the “typeIndex” is invoked, which doesn’t account at all for the “tId” fields used in the query
cbq> explain select * from default where _type = 'board' AND tId = 'team::1';
{
"requestID": "101e1cc8-7c45-4395-8b85-7322e5d6a246",
"signature": "json",
"results": [
{
"#operator": "Sequence",
"~children": [
{
"#operator": "IndexScan",
"index": "typeIndex",
"keyspace": "default",
"limit": 9.223372036854776e+18,
"namespace": "default",
"spans": [
{
"Range": {
"High": [
"board"
],
"Inclusion": 3,
"Low": [
"board"
]
},
"Seek": null
}
]
},
{
"#operator": "Parallel",
"~child": {
"#operator": "Sequence",
"~children": [
{
"#operator": "Fetch",
"keyspace": "default",
"namespace": "default"
},
{
"#operator": "Filter",
"condition": "(((`default`.`_type`) = \"board\") and ((`default`.`tId`) = \"team::1\"))"
},
{
"#operator": "InitialProject",
"result_terms": [
{
"star": true
}
]
},
{
"#operator": "FinalProject"
}
]
}
}
]
}
],
"status": "success",
"metrics": {
"elapsedTime": "1.111ms",
"executionTime": "1.048ms",
"resultCount": 1,
"resultSize": 2025
}
}
Finally, in a 3rd query where I am joining 2 documents together, the 2nd document getting joined based on the existence of a parameter within an array of values. When I run an Explain on it, I see the very simplist index being used again, just the “typeIndex”. This one should be using the typeTeamIdCxns index.
cbq> explain SELECT card.* FROM default cn JOIN default card ON KEYS cn.cId WHERE cn._type = 'cnet' and cn.tID = 'team::1' AND 'board::bkHdkVa5M' IN cn.cxns;
{
"requestID": "355705f0-aebf-4e02-8925-1195d5a3b5c6",
"signature": "json",
"results": [
{
"#operator": "Sequence",
"~children": [
{
"#operator": "IndexScan",
"index": "typeIndex",
"keyspace": "default",
"limit": 9.223372036854776e+18,
"namespace": "default",
"spans": [
{
"Range": {
"High": [
"cnet"
],
"Inclusion": 3,
"Low": [
"cnet"
]
},
"Seek": null
}
]
},
{
"#operator": "Parallel",
"~child": {
"#operator": "Sequence",
"~children": [
{
"#operator": "Fetch",
"as": "cn",
"keyspace": "default",
"namespace": "default"
},
{
"#operator": "Join",
"as": "card",
"keyspace": "default",
"namespace": "default",
"on_keys": "(`cn`.`cId`)"
},
{
"#operator": "Filter",
"condition": "((((`cn`.`_type`) = \"cnet\") and ((`cn`.`tID`) = \"team::1\")) and (\"board::bkHdkVa5M\" in (`cn`.`cxns`)))"
},
{
"#operator": "InitialProject",
"result_terms": [
{
"expr": "`card`",
"star": true
}
]
},
{
"#operator": "FinalProject"
}
]
}
}
]
}
],
"status": "success",
"metrics": {
"elapsedTime": "2.089779ms",
"executionTime": "1.963745ms",
"resultCount": 1,
"resultSize": 2489
}
}
Before I go too far down this path, I’m hoping someone could shed some light as to what I’m missing.
Thanks!
Chris