I have this query (created with help from people in here!):
SELECT du.modified,du.modifiedby,d.revisioninfo.modifiedcount FROM data AS d UNNEST TO_ARRAY(IFMISSINGORNULL(d.revisioninfo.updates,{'modified':d.revisioninfo.created,'modifiedby':d.revisioninfo.createdby})) AS du WHERE d.type='Catch'
ORDER BY du.modified DESC
It works functionally as expected
…but it is too slow (1 sec.) which obviously is due to lack of an appropriate index. The data structure looks like this:
{
"bait": "5",
"count": 12,
"date": "2015-08-24T12:00:00+0200",
"finclipcount": 3,
"fishingtripkey": "141A1CBE90BD7AFDC1257EAB0048501D",
"ispublic": false,
"key": "0D4F548C4DDC5CBDC1257F7000282F6B",
"length": 43,
"localssociation": "AF67A22726B129A9C1257E600051BE7C",
"markednow": false,
"place": "045B152BDB745864C1257E600051BE56",
"refno": "1234321",
"revisioninfo": {
"created": "2016-03-08T08:18:56+0100",
"createdby": "John Dalsgaard/BA171123846CEBF1C1257CB2002DA330/Fangst",
"modifiedcount": 2,
"updates": [
{
"modified": "2016-03-08T08:20:02+0100",
"modifiedby": "John Dalsgaard/BA171123846CEBF1C1257CB2002DA330/Fangst"
},
{
"modified": "2016-03-08T08:21:08+0100",
"modifiedby": "John Dalsgaard/BA171123846CEBF1C1257CB2002DA330/Fangst"
}
]
},
"sexcountfemale": 7,
"sexcountmale": 5,
"species": "25",
"takenhome": 1,
"type": "Catch",
"undersized": 9,
"userkey": "BA171123846CEBF1C1257CB2002DA330",
"weight": 9
}
and the updates
node may or may not be there…
I have tried to create an index like this:
CREATE INDEX `def_type_modified` ON `data`(`type`,`revisioninfo.modifiedcount`,`revisioninfo.created`,`revisioninfo.createdby`,`revisioninfo.updates.modified`,`revisioninfo.updates.modifiedby`)
But “Explain” indicates that it doesn’t use it…:
{
"plan": {
"#operator": "Sequence",
"~children": [
{
"#operator": "Sequence",
"~children": [
{
"#operator": "IndexScan2",
"index": "def_type",
"index_id": "941b5fdba8f0e7f8",
"index_projection": {
"primary_key": true
},
"keyspace": "data",
"namespace": "default",
"spans": [
{
"exact": true,
"range": [
{
"high": "\"FishingTrip\"",
"inclusion": 3,
"low": "\"FishingTrip\""
}
]
}
],
"using": "gsi"
},
{
"#operator": "Fetch",
"as": "d",
"keyspace": "data",
"namespace": "default"
},
{
"#operator": "Parallel",
"~child": {
"#operator": "Sequence",
"~children": [
{
"#operator": "Unnest",
"as": "du",
"expr": "to_array(ifmissingornull(((`d`.`revisioninfo`).`updates`), {\"modified\": ((`d`.`revisioninfo`).`created`), \"modifiedby\": ((`d`.`revisioninfo`).`createdby`)}))"
}
]
}
},
{
"#operator": "Parallel",
"~child": {
"#operator": "Sequence",
"~children": [
{
"#operator": "Filter",
"condition": "((`d`.`type`) = \"FishingTrip\")"
},
{
"#operator": "InitialProject",
"result_terms": [
{
"expr": "(`du`.`modified`)"
},
{
"expr": "(`du`.`modifiedby`)"
},
{
"expr": "((`d`.`revisioninfo`).`modifiedcount`)"
}
]
}
]
}
}
]
},
{
"#operator": "Order",
"sort_terms": [
{
"desc": true,
"expr": "(`du`.`modified`)"
}
]
},
{
"#operator": "FinalProject"
}
]
},
"text": "SELECT du.modified,du.modifiedby,d.revisioninfo.modifiedcount FROM data AS d UNNEST TO_ARRAY(IFMISSINGORNULL(d.revisioninfo.updates,{'modified':d.revisioninfo.created,'modifiedby':d.revisioninfo.createdby})) AS du WHERE d.type='FishingTrip'\nORDER BY du.modified DESC"
}
Any ideas appreciated