By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
category_1_operations.date_-1
[Function: insert]
VACUUM
[
{
_id: 341,
operations: [
{ date: ISODate('2026-09-26T11:43:10.219Z'), amount: 777 },
{ date: ISODate('2026-09-26T11:43:10.224Z'), amount: 848 }
]
}
]
{
stage: 'LIMIT',
startupCost: 0,
totalCost: 0.21,
estimatedTotalKeysExamined: 1,
inputStage: {
stage: 'PROJECT',
startupCost: 0,
totalCost: 0.21,
estimatedTotalKeysExamined: 1,
inputStage: {
stage: 'FETCH',
ns: 'test.accounts',
startupCost: 0,
totalCost: 32.11,
estimatedTotalKeysExamined: 158,
inputStage: {
stage: 'IXSCAN',
ns: 'test.accounts',
indexName: 'category_1_operations.date_-1',
direction: 'Forward',
indexUsage: {
indexKeyString: '{"category": 1,"operations.date": -1}',
isMultiKey: true,
bounds: [
'["category": [1, 1], "operations.date": DESC(MinKey, MaxKey)]'
]
},
startupCost: 0,
totalCost: 32.11,
hasOrderBy: true,
indexFilterSet: [ { category: { '$eq': 1 } } ],
estimatedTotalKeysExamined: 158
}
}
}
}
SET
| document |
|---|
| { "_id" : { "$numberInt" : "341" }, "operations" : [ { "date" : { "$date" : { "$numberLong" : "1790422990219" } }, "amount" : { "$numberInt" : "777" } }, { "date" : { "$date" : { "$numberLong" : "1790422990224" } }, "amount" : { "$numberInt" : "848" } } ] } |
SELECT 1
| QUERY PLAN |
|---|
| Subquery Scan on agg_stage_3 (actual time=0.076..0.077 rows=1.00 loops=1) |
| Output: documentdb_api_internal.bson_dollar_project(agg_stage_3.document, '{ "operations.amount" : { "$numberInt" : "1" }, "operations.date" : { "$numberInt" : "1" } }'::documentdb_core.bson, '{ "now" : { "$date" : { "$numberLong" : "1790422994043" } } }'::documentdb_core.bson) |
| Buffers: shared hit=4 |
| -> Limit (actual time=0.070..0.070 rows=1.00 loops=1) |
| Output: collection.document, (documentdb_api_catalog.bson_orderby(collection.document, '{ "operations.date" : { "$numberInt" : "-1" } }'::documentdb_core.bson)) |
| Buffers: shared hit=4 |
| -> Custom Scan (DocumentDBApiExplainQueryScan) (actual time=0.069..0.070 rows=1.00 loops=1) |
| Output: collection.document, documentdb_api_catalog.bson_orderby(collection.document, '{ "operations.date" : { "$numberInt" : "-1" } }'::documentdb_core.bson) |
| namespaceName: test.accounts |
| indexName: category_1_operations.date_-1 |
| indexKey: {"category": 1,"operations.date": -1} |
| isMultiKey: true |
| indexBounds: ["category": [1, 1], "operations.date": DESC(MinKey, MaxKey)] |
| innerScanLoops: 1 loops |
| scanType: ordered |
| scanKeyDetails: key 1: [(isInequality: true, estimatedEntryCount: 40)] |
| _id_: (startup cost=0.275, total cost=43.377, selectivity=1, correlation=0.750, estimated index pages loaded=100.00%, estimated total index entries=947, boundary selectivity=1, num boundaries=0, estimated data pages loaded=0.00%) |
| Buffers: shared hit=4 |
| -> Index Scan using "category_1_operations.date_-1" on documentdb_data.documents_2 collection (actual time=0.026..0.027 rows=1.00 loops=1) |
| Output: collection.document |
| Index Cond: (collection.document OPERATOR(documentdb_api_catalog.@=) '{ "category" : { "$numberInt" : "1" } }'::documentdb_core.bson) |
| Order By: (collection.document OPERATOR(documentdb_api_catalog.|-<>) '{ "operations.date" : { "$numberInt" : "-1" } }'::documentdb_core.bson) |
| Index Searches: 0 |
| Buffers: shared hit=4 |
| Planning: |
| Buffers: shared hit=2 |
| Planning Time: 0.126 ms |
| Execution Time: 0.104 ms |
EXPLAIN
SET
SELECT 947
SELECT 1000
CREATE INDEX
CREATE INDEX
VACUUM
| account_id | category | date | amount |
|---|---|---|---|
| 341 | 1 | 2026-09-26 11:43:10.219+00 | 777 |
| 341 | 1 | 2026-09-26 11:43:10.224+00 | 848 |
SELECT 2
| QUERY PLAN |
|---|
| Sort (actual time=0.322..0.324 rows=2.00 loops=1) |
| Output: a.account_id, a.category, o.date, o.amount |
| Sort Key: o.date |
| Sort Method: quicksort Memory: 25kB |
| Buffers: shared hit=18 |
| -> Nested Loop (actual time=0.319..0.321 rows=2.00 loops=1) |
| Output: a.account_id, a.category, o.date, o.amount |
| Buffers: shared hit=18 |
| -> Nested Loop (actual time=0.315..0.316 rows=1.00 loops=1) |
| Output: a_1.account_id, a.account_id, a.category |
| Buffers: shared hit=15 |
| -> Limit (actual time=0.309..0.310 rows=1.00 loops=1) |
| Output: a_1.account_id, o_1.date |
| Buffers: shared hit=12 |
| -> Sort (actual time=0.309..0.309 rows=1.00 loops=1) |
| Output: a_1.account_id, o_1.date |
| Sort Key: o_1.date DESC |
| Sort Method: top-N heapsort Memory: 25kB |
| Buffers: shared hit=12 |
| -> Hash Join (actual time=0.108..0.268 rows=340.00 loops=1) |
| Output: a_1.account_id, o_1.date |
| Hash Cond: (o_1.account_id = a_1.account_id) |
| Buffers: shared hit=12 |
| -> Seq Scan on documentdb_api.account_operations o_1 (actual time=0.005..0.055 rows=1000.00 loops=1) |
| Output: o_1.account_id, o_1.date, o_1.amount |
| Buffers: shared hit=7 |
| -> Hash (actual time=0.099..0.099 rows=319.00 loops=1) |
| Output: a_1.account_id |
| Buckets: 1024 Batches: 1 Memory Usage: 20kB |
| Buffers: shared hit=5 |
| -> Seq Scan on documentdb_api.accounts a_1 (actual time=0.004..0.064 rows=319.00 loops=1) |
| Output: a_1.account_id |
| Filter: (a_1.category = 1) |
| Rows Removed by Filter: 628 |
| Buffers: shared hit=5 |
| -> Index Only Scan using accounts_account_id_category_idx on documentdb_api.accounts a (actual time=0.005..0.005 rows=1.00 loops=1) |
| Output: a.account_id, a.category |
| Index Cond: (a.account_id = a_1.account_id) |
| Heap Fetches: 0 |
| Index Searches: 1 |
| Buffers: shared hit=3 |
| -> Index Scan using account_operations_account_id_date_idx on documentdb_api.account_operations o (actual time=0.003..0.003 rows=2.00 loops=1) |
| Output: o.account_id, o.date, o.amount |
| Index Cond: (o.account_id = a.account_id) |
| Index Searches: 1 |
| Buffers: shared hit=3 |
| Planning: |
| Buffers: shared hit=36 |
| Planning Time: 0.362 ms |
| Execution Time: 0.346 ms |
EXPLAIN