I have a query that produces a different query plan when I run it with inlined values vs positional parameters, I would assume they should produce the same plan.
SELECT RAW meta().id
FROM `lep`
WHERE `_type` = 'App\\Lead\\Lead'
AND `createdAt` >= '2019-08-28'
AND ANY `item` IN `responses` SATISFIES `item`.`bid` = 'BidRegistration::1234' END
ORDER BY RANDOM()
LIMIT 1
VS
SELECT RAW meta().id
FROM `lep`
WHERE `_type` = $1
AND `createdAt` >= $2
AND ANY `item` IN `responses` SATISFIES `item`.`bid` = $3 END
ORDER BY RANDOM()
LIMIT 1
These queries produce the following query plans.
For the query with inlined values.
{
"plan": {
"#operator": "Sequence",
"~children": [
{
"#operator": "Sequence",
"~children": [
{
"#operator": "DistinctScan",
"scan": {
"#operator": "IndexScan3",
"covers": [
"cover ((distinct (array (`response`.`bid`) for `response` in (`lep`.`responses`) end)))",
"cover ((`lep`.`_type`))",
"cover ((`lep`.`createdAt`))",
"cover ((meta(`lep`).`id`))"
],
"filter_covers": {
"cover ((`lep`.`_type`))": "App\\Lead\\Lead",
"cover (any `item` in (`lep`.`responses`) satisfies ((`item`.`bid`) = \"BidRegistration::1234\") end)": true
},
"index": "idx_lead_responses_bid",
"index_id": "c1577ff034097ebc",
"index_projection": {
"entry_keys": [
1,
2
],
"primary_key": true
},
"keyspace": "lep",
"namespace": "default",
"spans": [
{
"exact": true,
"range": [
{
"high": "\"BidRegistration::1234\"",
"inclusion": 3,
"low": "\"BidRegistration::1234\""
},
{
"high": "\"App\\\\Lead\\\\Lead\"",
"inclusion": 3,
"low": "\"App\\\\Lead\\\\Lead\""
},
{
"inclusion": 1,
"low": "\"2019-08-28\""
}
]
}
],
"using": "gsi"
}
},
{
"#operator": "Parallel",
"~child": {
"#operator": "Sequence",
"~children": [
{
"#operator": "Filter",
"condition": "(((cover ((`lep`.`_type`)) = \"App\\\\Lead\\\\Lead\") and (\"2019-08-28\" <= cover ((`lep`.`createdAt`)))) and cover (any `item` in (`lep`.`responses`) satisfies ((`item`.`bid`) = \"BidRegistration::1234\") end))"
},
{
"#operator": "InitialProject",
"raw": true,
"result_terms": [
{
"expr": "cover ((meta(`lep`).`id`))"
}
]
}
]
}
}
]
},
{
"#operator": "Order",
"limit": "1",
"sort_terms": [
{
"expr": "random()"
}
]
},
{
"#operator": "Limit",
"expr": "1"
},
{
"#operator": "FinalProject"
}
]
},
"text": "SELECT RAW meta().id FROM `lep` WHERE `_type` = 'App\\\\Lead\\\\Lead' AND `createdAt` >= '2019-08-28' AND ANY `item` IN\r\n`responses` SATISFIES `item`.`bid` = 'BidRegistration::1234' END ORDER BY RANDOM() LIMIT 1"
}
For the query with positional parameters.
{
"plan": {
"#operator": "Sequence",
"~children": [
{
"#operator": "Sequence",
"~children": [
{
"#operator": "DistinctScan",
"scan": {
"#operator": "IndexScan3",
"index": "idx_lead_responses_bid",
"index_id": "c1577ff034097ebc",
"index_projection": {
"primary_key": true
},
"keyspace": "lep",
"namespace": "default",
"spans": [
{
"exact": true,
"range": [
{
"high": "\"BidRegistration::1234\"",
"inclusion": 3,
"low": "\"BidRegistration::1234\""
},
{
"high": "\"App\\\\Lead\\\\Lead\"",
"inclusion": 3,
"low": "\"App\\\\Lead\\\\Lead\""
},
{
"inclusion": 1,
"low": "\"2019-08-28\""
}
]
}
],
"using": "gsi"
}
},
{
"#operator": "Fetch",
"keyspace": "lep",
"namespace": "default"
},
{
"#operator": "Parallel",
"~child": {
"#operator": "Sequence",
"~children": [
{
"#operator": "Filter",
"condition": "((((`lep`.`_type`) = $1) and ($2 <= (`lep`.`createdAt`))) and any `item` in (`lep`.`responses`) satisfies ((`item`.`bid`) = $3) end)"
},
{
"#operator": "InitialProject",
"raw": true,
"result_terms": [
{
"expr": "(meta(`lep`).`id`)"
}
]
}
]
}
}
]
},
{
"#operator": "Order",
"limit": "1",
"sort_terms": [
{
"expr": "random()"
}
]
},
{
"#operator": "Limit",
"expr": "1"
},
{
"#operator": "FinalProject"
}
]
},
"text": "SELECT RAW meta().id FROM `lep` WHERE `_type` = $1 AND `createdAt` >= $2 AND ANY `item` IN `responses` SATISFIES `item`.`bid` = $3 END ORDER BY RANDOM() LIMIT 1"
}
Is there something I’m missing that would cause these 2 queries to produce different plans even though they seem to be virtually identical?