I am currently using the below query to get all my records that are active. based on rets having 5 mil records and Contacts 5 K the query takes almost 9 sec
SELECT f.Record.PropertyAddress AS farm,
(
SELECT p1.Record.Dates
FROM p AS p1
ORDER BY p1.Record.Dates.ModificationTimestamp DESC
LIMIT 1)[0] AS lisiting
FROM Contacts AS f LEFT NEST rets AS p ON f.Record.apn = REPLACE(p.ParcelNumber, "-", "")
AND p._type = "Residential"
WHERE f._type = "farm"
AND p.Record.StandardStatus = "Active"
LIMIT 5
Blow is the Plan Text
{
"#operator": "Sequence",
"~children": [
{
"#operator": "Sequence",
"~children": [
{
"#operator": "IndexScan3",
"as": "f",
"index": "adv_Record_apn_type",
"index_id": "96e67c7a38d60df1",
"index_projection": {
"primary_key": true
},
"keyspace": "Contacts",
"namespace": "default",
"spans": [
{
"exact": true,
"range": [
{
"inclusion": 0,
"low": "null"
}
]
}
],
"using": "gsi"
},
{
"#operator": "Fetch",
"as": "f",
"keyspace": "Contacts",
"namespace": "default"
},
{
"#operator": "Parallel",
"~child": {
"#operator": "Sequence",
"~children": [
{
"#operator": "Filter",
"condition": "(((`f`.`_type`) = \"farm\") and (((`f`.`Record`).`apn`) is not null))"
},
{
"#operator": "NestedLoopNest",
"alias": "p",
"on_clause": "((((`f`.`Record`).`apn`) = replace((`p`.`ParcelNumber`), \"-\", \"\")) and ((`p`.`_type`) = \"Residential\"))",
"~child": {
"#operator": "Sequence",
"~children": [
{
"#operator": "IndexScan3",
"as": "p",
"index": "adv_Record_StandardStatus_replace_ParcelNumber_sub_type",
"index_id": "c6e89537ed82fb3a",
"index_projection": {
"primary_key": true
},
"keyspace": "rets",
"namespace": "default",
"nested_loop": true,
"spans": [
{
"exact": true,
"range": [
{
"high": "\"Active\"",
"inclusion": 3,
"low": "\"Active\""
},
{
"high": "((`f`.`Record`).`apn`)",
"inclusion": 3,
"low": "((`f`.`Record`).`apn`)"
}
]
}
],
"using": "gsi"
},
{
"#operator": "Fetch",
"as": "p",
"keyspace": "rets",
"namespace": "default",
"nested_loop": true
},
{
"#operator": "Parallel",
"~child": {
"#operator": "Sequence",
"~children": [
{
"#operator": "Filter",
"condition": "((((`p`.`Record`).`StandardStatus`) = \"Active\") and ((`p`.`_type`) = \"Residential\") and ((`p`.`_type`) = \"Residential\"))"
}
]
}
}
]
}
},
{
"#operator": "InitialProject",
"result_terms": [
{
"as": "farm",
"expr": "((`f`.`Record`).`PropertyAddress`)"
},
{
"as": "lisiting",
"expr": "(correlated (select ((`p1`.`Record`).`Dates`) from (`p`) as `p1` ORDER BY (((`p1`.`Record`).`Dates`).`ModificationTimestamp`) DESC limit 1)[0])"
}
]
}
]
}
}
]
},
{
"#operator": "Limit",
"expr": "5"
}
]
}