Couchbase中Object_Pairs索引的覆盖查询性能及存储问题问询
object_pairs(): Performance & Storage Questions Question 1: Index Storage & Overhead for Covering Queries
First, let's break down your initial question:
若基于Object_pair(values).val.data创建索引,该索引是否会将values字段存储为经object_pair转换后的、包含name(作为ID)和val(作为数据)元素的数组?若情况属实,当N1QL查询为仅获取Object_pair(values).val.data的覆盖查询时,是否仍存在性能开销?
Answer:
Yes, the index stores the pre-computed transformed array: When you build an index using
object_pairs(values).val.data, Couchbase runs theobject_pairs()function during index creation. It converts your original nestedvaluesobject (like the example below) into the array structure you described, then stores that computed result directly in the index.Example original
valuesfield:"values": { "item_1": { "data": [{ "name": "data_1", "value": "A" }, { "name": "data_2", "value": "XYZ" } ] }, "item_2": { "data": [{ "name": "data_1", "value": "123" }, { "name": "data_2", "value": "A23" } ] } }The index will store the output of
object_pairs(values)—an array of entries like{name: "item_1", val: {...}}and{name: "item_2", val: {...}}—along with the extractedval.dataportion targeted by your index.Minimal overhead for covering queries: If your query is a covering query (meaning it only requests data that exists in the index), Couchbase pulls the pre-computed values directly from the index. It won't need to fetch the original document or re-run the
object_pairs()transformation. The only overhead here is the index lookup itself, which is drastically faster than scanning documents + executing the function.- Non-covering queries, on the other hand, will have to fetch the full document and run
object_pairs()on the originalvaluesfield, adding the expected transformation overhead.
- Non-covering queries, on the other hand, will have to fetch the full document and run
Question 2: Performance of Indexed Array Projections
Your updated question focuses on an index that projects a combined array of name and val.data, paired with a matching SELECT query:
索引语句为:
CREATE INDEX idx01 ON ent_comms_tracking(ARRAY { value.name, value.val.data} FOR value IN object_pairs(values) END),对应的查询语句为:SELECT ARRAY { value.name, value.val.data} FOR value IN object_pairs(values) END as values_array FROM bucket,此时是否存在性能开销?
Answer:
The performance overhead depends entirely on whether the query uses your idx01 index as a covering index:
If the query uses the covering index:
- No significant transformation overhead. The index already stores the pre-built array of
{value.name, value.val.data}entries. Couchbase will pull this ready-made array directly from the index and return it asvalues_array—no need to runobject_pairs()on the original document at query time. - To ensure the query leverages the index, you can use an
INDEX HINT(e.g.,USE INDEX (idx01)) or check the query plan to confirm index usage.
- No significant transformation overhead. The index already stores the pre-built array of
If the query does NOT use the index:
- You'll see the full overhead of scanning documents and running
object_pairs()for each document to build the array on the fly. This is identical to not having the index at all.
- You'll see the full overhead of scanning documents and running
Key Note:
For this to work as a covering query, your SELECT clause must exactly match (or be a subset of) the projection in the index. Since your query's ARRAY expression is identical to the index's projection, it's a perfect fit for covering index usage.
内容的提问来源于stack exchange,提问作者anshul.gairola

