HI,
I was trying to select data using an UNNEST query but it was taking far too long to return so I resorted to using ARRAY operations in an attempt to limit how much data was being joined. Now I have a query that returns an array (from a union of selects) in acceptable time, and my array has two kinds of data objects in it, prediction objects and observation objects. I want to combine a prediction with an observation based on a matching avtime field. Predictions and observations that have no peer, based on avtime, should be excluded. It seems that this should be simple but it eludes me. I include my query here, and a sample of data that has a few matches, 1614805200, 1614812400 and 1614816000. In production I’ll be querying many thousands or hundreds of thousands of records.
This is my query…
SELECT observation, CEIL(3600*FLOOR(((mdata.fcstValidEpoch)+3600/2)/3600)) avtime
FROM mdata
LET observation = (ARRAY {"station":station.Ceiling,"name":station.name} FOR station IN mdata.data WHEN station.name IN ["KBYG","KGEY","KSHR"] END),
observationLength = ARRAY_LENGTH(observation)
WHERE
type='DD'
AND docType='obs'
AND subset='METAR'
AND version='V01'
AND fcstValidEpoch >= 1614805400 - 900
AND fcstValidEpoch <= 1615401000 + 900
AND observationLength = 3
UNION
SELECT prediction, CEIL(3600*FLOOR(((mdata.fcstValidEpoch)+3600/2)/3600)) avtime
FROM mdata
LET prediction = (ARRAY {"station":station.Ceiling,"name":station.name} FOR station IN OBJECT_VALUES(mdata.data) WHEN station.name IN ["KBYG","KGEY","KSHR"] END),
predictionLength = ARRAY_LENGTH(prediction)
WHERE
type='DD'
AND docType='model'
AND model="HRRR"
AND fcstLen=6
AND subset='METAR'
AND version='V01'
AND fcstValidEpoch >= 1614805400 - 900
AND fcstValidEpoch <= 1615401000 + 900
AND predictionLength = 3
LIMIT 10
How can I rewrite this query, or add to it, such that I still retain performance and I would get back data like this…
[
{
"avtime": 1614805200,
"prediction": [
{
"name": "KBYG",
"station": 6084
},
{
"name": "KGEY",
"station": 6162
},
{
"name": "KSHR",
"station": 6175
}
],
"observation": [
{
"name": "KBYG",
"station": 6000
},
{
"name": "KSHR",
"station": 6000
},
{
"name": "KGEY",
"station": 6000
}
]
},
{
"avtime": 1614812400,
"observation": [
{
"name": "KBYG",
"station": 6000
},
{
"name": "KSHR",
"station": 6000
},
{
"name": "KGEY",
"station": 6000
}
],
"prediction": [
{
"name": "KBYG",
"station": 6084
},
{
"name": "KGEY",
"station": 6162
},
{
"name": "KSHR",
"station": 6176
}
]
},
{
"avtime": 1614816000,
"observation": [
{
"name": "KBYG",
"station": 6000
},
{
"name": "KSHR",
"station": 6000
},
{
"name": "KGEY",
"station": 6000
}
],
"prediction": [
{
"name": "KBYG",
"station": 6084
},
{
"name": "KGEY",
"station": 6162
},
{
"name": "KSHR",
"station": 6176
}
]
}
]
From this data which is returned currently…
[
{
"avtime": 1614805200,
"prediction": [
{
"name": "KBYG",
"station": 6084
},
{
"name": "KGEY",
"station": 6162
},
{
"name": "KSHR",
"station": 6175
}
]
},
{
"avtime": 1614805200,
"observation": [
{
"name": "KBYG",
"station": 6000
},
{
"name": "KSHR",
"station": 6000
},
{
"name": "KGEY",
"station": 6000
}
]
},
{
"avtime": 1614808800,
"prediction": [
{
"name": "KBYG",
"station": 6084
},
{
"name": "KGEY",
"station": 6162
},
{
"name": "KSHR",
"station": 6175
}
]
},
{
"avtime": 1614812400,
"observation": [
{
"name": "KBYG",
"station": 6000
},
{
"name": "KSHR",
"station": 6000
},
{
"name": "KGEY",
"station": 6000
}
]
},
{
"avtime": 1614812400,
"prediction": [
{
"name": "KBYG",
"station": 6084
},
{
"name": "KGEY",
"station": 6162
},
{
"name": "KSHR",
"station": 6176
}
]
},
{
"avtime": 1614816000,
"observation": [
{
"name": "KBYG",
"station": 6000
},
{
"name": "KSHR",
"station": 6000
},
{
"name": "KGEY",
"station": 6000
}
]
},
{
"avtime": 1614816000,
"prediction": [
{
"name": "KBYG",
"station": 6084
},
{
"name": "KGEY",
"station": 6162
},
{
"name": "KSHR",
"station": 6176
}
]
},
{
"avtime": 1614819600,
"observation": [
{
"name": "KBYG",
"station": 6000
},
{
"name": "KSHR",
"station": 6000
},
{
"name": "KGEY",
"station": 6000
}
]
},
{
"avtime": 1614996000,
"prediction": [
{
"name": "KBYG",
"station": 6084
},
{
"name": "KGEY",
"station": 6162
},
{
"name": "KSHR",
"station": 6175
}
]
},
{
"avtime": 1614823200,
"observation": [
{
"name": "KBYG",
"station": 6000
},
{
"name": "KSHR",
"station": 6000
},
{
"name": "KGEY",
"station": 6000
}
]
}
]
Any help is greatly appreciated.
thanks