Hi Michael,
I tried adhoc=true. I dont see any difference. I still see the 25 seconds delay in response from CB server.
- I’m using Ruby SDK ( ‘couchbase’, ‘3.4.0’) and Couchbase server(
Enterprise Edition 6.6.3 build 9808)
- I’m using named_parameters not positional_parameters
So I set adhoc=true and just to make sure I printed the options as well:
options = Couchbase::Cluster::QueryOptions.new
options.adhoc = true
And I can see → “adhoc”:true,
options={“timeout”:null,“retry_strategy”:null,“client_context”:null,“parent_span”:null,“adhoc”:true,“client_context_id”:null,“max_parallelism”:null,“readonly”:false,“scan_wait”:null,“scan_cap”:null,“pipeline_cap”:null,“pipeline_batch”:null,“metrics”:true,“profile”:“off”,“flex_index”:null,“preserve_expiry”:null,“scope_qualifier”:null,“scan_consistency”:“not_bounded”,“mutation_state”:null,“transcoder”:{},“positional_parameters”:null,“named_parameters”:{“$lastType”:“”},“raw_parameters”:{}}
I took an additional step forward by capturing the TCPdump on query port 8093.
POST /query/service HTTP/1.1
host: couchbase1.spdev.sightplan-ops.net:8093
authorization: Basic XXXXX== <redacted>
client-context-id: 4848cf-13f4-1e40-33cc-4c024c38f712ab
connection: keep-alive
content-length: 792
content-type: application/json
user-agent: cxx/1.0.0/ec53d75;Linux/x86_64; client/e96632-c201-954d-b5c6-6cc5d76c22a332; session/117666-76b4-8340-3533-a041acb5c3711b; Linux-5.4.0-155-generic; ruby_sdk/85dacb90;ssl/1010114f;ruby_abi/3.0.0
{"$lastType":"","client_context_id":"4848cf-13f4-1e40-33cc-4c024c38f712ab","scan_consistency":"not_bounded","statement":"SELECT meta(sg).`id` FROM `sync_gateway_sw1` AS sg WHERE (sg.`type` IN [\"location\"]) AND (((sg.`type` = \"\") AND (IFMISSINGORNULL(sg.`locationRoot`, sg.`locationId`, sg.`location`, \"...\") = \"\") AND (SUBSTR(META(sg).`id`,0) > \"\")) OR ((sg.`type` = \"\") AND (IFMISSINGORNULL(sg.`locationRoot`, sg.`locationId`, sg.`location`, \"...\") > \"\")) OR (sg.`type` > $lastType)) AND (NOT IFMISSINGORNULL(sg.`softDelete`, FALSE)) AND (NOT IFMISSINGORNULL(sg.`_deleted`, FALSE)) AND (META(sg).`id` NOT LIKE \"_sync:%\") ORDER BY sg.`type`, IFMISSINGORNULL(sg.`locationRoot`, sg.`locationId`, sg.`location`, \"...\"), SUBSTR(META(sg).`id`,0) LIMIT 10;","timeout":"74500ms"}
^^^ You mentioned “adhoc=true effectively un-parameterizes the query” - But I still parameters. $lastType is the one parameter here in the query request (check HTTP request). Did you mean when I pass adhoc=true the client SDK will unparameterize the query before it send the request to the server?
Here is the comparison of PARAMETERIZED EXPLAIN (25 seconds delay) v/s UN-PARAMETERIZED EXPLAIN (<100milliseconds delay):
PARAMETERIZED EXPLAIN (has 25 seconds delay) :
{
"plan": {
"#operator": "Sequence",
"~children": [
{
"#operator": "Sequence",
"~children": [
{
"#operator": "IndexScan3",
"as": "sg",
"covers": [
"cover ((`sg`.`type`))",
"cover (ifmissingornull((`sg`.`locationRoot`), (`sg`.`locationId`), (`sg`.`location`), \"\"))",
"cover (substr0((meta(`sg`).`id`), 0))",
"cover (ifmissingornull((`sg`.`subType`), (`sg`.`changeType`)))",
"cover (encoded_size(object_remove(`sg`, \"_sync\")))",
"cover (encoded_size(`sg`))",
"cover ((meta(`sg`).`id`))"
],
"filter_covers": {
"cover (((`sg`.`type`) is valued))": true,
"cover ((not ((meta(`sg`).`id`) like \"_sync:%\")))": true,
"cover ((not ifmissingornull((`sg`.`_deleted`), false)))": true,
"cover ((not ifmissingornull((`sg`.`softDelete`), false)))": true
},
"index": "by_type_location_size_sg_idx_v2",
"index_id": "f6ee0e217e16f1a5",
"index_order": [
{
"keypos": 0
},
{
"keypos": 1
},
{
"keypos": 2
}
],
"index_projection": {
"entry_keys": [
0,
1,
2
],
"primary_key": true
},
"keyspace": "sync_gateway_sw1",
"namespace": "default",
"spans": [
{
"range": [
{
"high": "\"location\"",
"inclusion": 2,
"low": "$lastType"
}
]
}
],
"using": "gsi"
},
{
"#operator": "Parallel",
"maxParallelism": 1,
"~child": {
"#operator": "Sequence",
"~children": [
{
"#operator": "Filter",
"condition": "(((((cover ((`sg`.`type`)) in [\"location\"]) and (((((cover ((`sg`.`type`)) = \"\") and (cover (ifmissingornull((`sg`.`locationRoot`), (`sg`.`locationId`), (`sg`.`location`), \"\")) = \"\")) and (\"\" < cover (substr0((meta(`sg`).`id`), 0)))) or ((cover ((`sg`.`type`)) = \"\") and (\"\" < cover (ifmissingornull((`sg`.`locationRoot`), (`sg`.`locationId`), (`sg`.`location`), \"\"))))) or ($lastType < cover ((`sg`.`type`))))) and cover ((not ifmissingornull((`sg`.`softDelete`), false)))) and cover ((not ifmissingornull((`sg`.`_deleted`), false)))) and cover ((not ((meta(`sg`).`id`) like \"_sync:%\"))))"
},
{
"#operator": "InitialProject",
"result_terms": [
{
"expr": "cover ((meta(`sg`).`id`))"
}
]
},
{
"#operator": "FinalProject"
}
]
}
}
]
},
{
"#operator": "Limit",
"expr": "10"
}
]
},
"text": "SELECT meta(sg).`id` FROM `sync_gateway_sw1` AS sg WHERE (sg.`type` IN [\"location\"]) AND (((sg.`type` = \"\") AND (IFMISSINGORNULL(sg.`locationRoot`, sg.`locationId`, sg.`location`, \"\") = \"\") AND (SUBSTR(META(sg).`id`,0) > \"\")) OR ((sg.`type` = \"\") AND (IFMISSINGORNULL(sg.`locationRoot`, sg.`locationId`, sg.`location`, \"\") > \"\")) OR (sg.`type` > $lastType)) AND (NOT IFMISSINGORNULL(sg.`softDelete`, FALSE)) AND (NOT IFMISSINGORNULL(sg.`_deleted`, FALSE)) AND (META(sg).`id` NOT LIKE \"_sync:%\") ORDER BY sg.`type`, IFMISSINGORNULL(sg.`locationRoot`, sg.`locationId`, sg.`location`, \"\"), SUBSTR(META(sg).`id`,0) LIMIT 10;"
}
UNPARAMETERIZED PLAN (< 100 milliseconds delay):
{
"plan": {
"#operator": "Sequence",
"~children": [
{
"#operator": "Sequence",
"~children": [
{
"#operator": "IndexScan3",
"as": "sg",
"covers": [
"cover ((`sg`.`type`))",
"cover (ifmissingornull((`sg`.`locationRoot`), (`sg`.`locationId`), (`sg`.`location`), \"\"))",
"cover (substr0((meta(`sg`).`id`), 0))",
"cover (ifmissingornull((`sg`.`subType`), (`sg`.`changeType`)))",
"cover (encoded_size(object_remove(`sg`, \"_sync\")))",
"cover (encoded_size(`sg`))",
"cover ((meta(`sg`).`id`))"
],
"filter_covers": {
"cover (((`sg`.`type`) is valued))": true,
"cover ((not ((meta(`sg`).`id`) like \"_sync:%\")))": true,
"cover ((not ifmissingornull((`sg`.`_deleted`), false)))": true,
"cover ((not ifmissingornull((`sg`.`softDelete`), false)))": true
},
"index": "by_type_location_size_sg_idx_v2",
"index_id": "f6ee0e217e16f1a5",
"index_order": [
{
"keypos": 0
},
{
"keypos": 1
},
{
"keypos": 2
}
],
"index_projection": {
"entry_keys": [
0,
1,
2
],
"primary_key": true
},
"keyspace": "sync_gateway_sw1",
"limit": "10",
"namespace": "default",
"spans": [
{
"exact": true,
"range": [
{
"high": "\"location\"",
"inclusion": 3,
"low": "\"location\""
}
]
}
],
"using": "gsi"
},
{
"#operator": "Parallel",
"maxParallelism": 1,
"~child": {
"#operator": "Sequence",
"~children": [
{
"#operator": "Filter",
"condition": "(((((cover ((`sg`.`type`)) in [\"location\"]) and (((((cover ((`sg`.`type`)) = \"\") and (cover (ifmissingornull((`sg`.`locationRoot`), (`sg`.`locationId`), (`sg`.`location`), \"\")) = \"\")) and (\"\" < cover (substr0((meta(`sg`).`id`), 0)))) or ((cover ((`sg`.`type`)) = \"\") and (\"\" < cover (ifmissingornull((`sg`.`locationRoot`), (`sg`.`locationId`), (`sg`.`location`), \"\"))))) or (\"\" < cover ((`sg`.`type`))))) and cover ((not ifmissingornull((`sg`.`softDelete`), false)))) and cover ((not ifmissingornull((`sg`.`_deleted`), false)))) and cover ((not ((meta(`sg`).`id`) like \"_sync:%\"))))"
},
{
"#operator": "InitialProject",
"result_terms": [
{
"expr": "cover ((meta(`sg`).`id`))"
}
]
},
{
"#operator": "FinalProject"
}
]
}
}
]
},
{
"#operator": "Limit",
"expr": "10"
}
]
},
"text": "SELECT meta(sg).`id` FROM `sync_gateway_sw1` AS sg WHERE (sg.`type` IN [\"location\"]) AND (((sg.`type` = \"\") AND (IFMISSINGORNULL(sg.`locationRoot`, sg.`locationId`, sg.`location`, \"\") = \"\") AND (SUBSTR(META(sg).`id`,0) > \"\")) OR ((sg.`type` = \"\") AND (IFMISSINGORNULL(sg.`locationRoot`, sg.`locationId`, sg.`location`, \"\") > \"\")) OR (sg.`type` > \"\")) AND (NOT IFMISSINGORNULL(sg.`softDelete`, FALSE)) AND (NOT IFMISSINGORNULL(sg.`_deleted`, FALSE)) AND (META(sg).`id` NOT LIKE \"_sync:%\") ORDER BY sg.`type`, IFMISSINGORNULL(sg.`locationRoot`, sg.`locationId`, sg.`location`, \"\"), SUBSTR(META(sg).`id`,0) LIMIT 10;"
}
Thanks,
Vishnu