Configure Queries

  • Capella Operational
      +
      You can configure the Query service using request-level query parameters.

      Request-Level Parameters

      To set a request-level parameter, do one of the following:

      • Use the Query Options window in the Query tab.

      • Use the cbq shell at the command line.

      • Use an SDK client program.

      Generally, use the cbq shell or the Query tab as a sandbox to test queries in your Capella cluster. Use an SDK client program for your production queries.

      • Capella UI

      • Command Line

      To set request-level preferences in the Query tab:

      1. Go to Data Tools  Query and click Query Options to display the Query Options window.

      2. Specify the preferences — if a preference is not explicitly listed, click + next to Named Parameters and add its name and value.

      3. Click Save.

      The Query Options window.

      For more examples, see the Couchbase Transactions guide.

      To set request-level parameters in cbq, use the \SET command. The parameter name must be prefixed by a hyphen.


      The following example sets the request-level timeout, pretty-print, and max parallelism parameters, and runs a query:

      \SET -timeout "30m";
      \SET -pretty true;
      \SET -max_parallelism 3;
      SELECT * FROM "world" AS hello;

      For more examples, see Parameter Manipulation in the cbq documentation.

      The table below contains details of all request-level parameters, along with examples.

      Table 1. Request-Level Parameters
      Property Schema

      args
      optional

      Supplies the values for positional parameters in the statement. Applicable if the statement or prepared statement contains 1 or more positional parameters.

      The value is an array of JSON values, one for each positional parameter in the statement.

      Refer to Named Parameters and Positional Parameters for details.

      Example: ["LAX",6]

      Any Type array

      atrcollection
      optional

      Specifies the collection where the active transaction record (ATR) is stored. The collection must be present. If not specified, the ATR is stored in the default collection in the default scope in the bucket containing the first mutated document within the transaction.

      The value must be a string in the form "bucket.scope.collection" or "namespace:bucket.scope.collection". If any part of the path contains a special character, that part of the path must be delimited in backticks ``.

      Example: "default:`travel-sample`.transaction.test"

      String

      auto_execute
      optional

      Specifies that prepared statements should be executed automatically as soon as they are created. This saves you from having to make two separate requests in cases where you want to prepare a statement and execute it immediately.

      Refer to Auto-Execute for more information.

      Default: false
      Example: true

      Boolean

      client_context_id
      optional

      A piece of data supplied by the client that is echoed in the response, if present. SQL++ is agnostic about the content of this parameter; it is just echoed in the response.

      • Maximum allowed size is 64 characters; all others will be cut.
      • If it contains an escape character / or quote ", it will be rejected as error code 1110.

      String

      compression
      optional

      Compression format to use for response data on the wire.

      Values are case-insensitive.

      Values: "ZIP", "RLE", "LZMA", "LZO", "NONE"
      Default: "NONE"
      Example: "zip"

      String

      controls
      optional

      Specifies if there should be a controls section returned with the request results.

      When set to true, the query response document includes a controls section with runtime information provided along with the request, such as positional and named parameters or settings.

      If the request qualifies for caching, these values will also be cached in the completed_requests system keyspace.

      Example: true

      Boolean

      durability_level
      optional

      The level of durability for mutations produced by the request.

      If the request contains a BEGIN TRANSACTION statement, or a DML statement with the tximplicit parameter set to true, the durability level is specified for all mutations within that transaction.

      Durability is also supported for non-transactional DML statements. In this case, the kvtimeout parameter is used as the durability timeout.

      If not specified, the default durability level is "majority". Set the durability level to "none" or "" to specify no durability.

      Values: "", "none", "majority", "majorityAndPersistActive", "persistToMajority"
      Default: "majority"
      Example: "none"

      String

      encoded_plan
      optional

      In databases running Couchbase Server 6.5 and later, this parameter is ignored and has no effect. It is included for compatibility with previous versions of Couchbase Server.

      String

      encoding
      optional

      Desired character encoding for the query results.

      Only possible value is UTF-8 and is case-insensitive.

      Default: "UTF-8"

      String

      format
      optional

      Desired format for the query results.

      Values are case-insensitive.

      Values: "JSON", "XML", "CSV", "TSV"
      Default: "JSON"
      Example: "XML"

      String

      kvtimeout
      optional

      The approximate time to wait for a KV get operation before timing out. This applies to statements within a transaction, and to non-transactional statements, whether durability_level is set or not.

      If use_replica is enabled for a query, then this parameter also specifies the approximate time to wait before fetching data from a replica vBucket when the active vBucket is inaccessible.

      The value for this parameter is a string. Its format includes an amount and a mandatory unit, e.g. 10ms (10 milliseconds) or 0.5s (half a second). Valid units are:

      • ns (nanoseconds)
      • us (microseconds)
      • ms (milliseconds)
      • s (seconds)
      • m (minutes)
      • h (hours)

      Specify a duration of 0 or a negative duration to disable. When disabled, no timeout is applied and the KV operation runs for however long it takes.

      Default: "2.5s"
      Example: "10ms"

      String

      max_parallelism
      optional

      Specifies the maximum parallelism for the query.

      The default value is the same as the number of partitions of the index selected for the query.

      Example: 3

      Integer (int32)

      memory_quota
      optional

      Specifies the maximum amount of memory the request may use, in MB.

      Specify 0 (the default value) to disable. When disabled, there is no quota.

      This parameter enforces a ceiling on the memory used for the tracked documents required for processing a request. It does not take into account any other memory that might be used to process a request, such as the stack, the operators, or some intermediate values.

      Within a transaction, this setting enforces the memory quota for the transaction by tracking the delta table and the transaction log (approximately).

      Default: 0
      Example: 4

      Integer (int32)

      metrics
      optional

      Specifies that metrics should be returned with query results.

      Default: true
      Example: false

      Boolean

      namespace
      optional

      Specifies the namespace to use. Currently, only the default namespace is available.

      Example: "default"

      String

      numatrs
      optional

      Specifies the total number of active transaction records. Must be a positive integer.

      Default: 1024
      Example: 512

      Integer (int32)

      pipeline_batch
      optional

      Controls the number of items execution operators can batch for Fetch from the KV.

      Example: 64

      Integer (int32)

      pipeline_cap
      optional

      Maximum number of items each execution operator can buffer between various operators.

      Example: 1024

      Integer (int32)

      prepared
      optional

      Required if statement not provided.

      The name of the prepared SQL++ statement to be executed. Refer to EXECUTE for examples.

      If both prepared and statement are present and non-empty, an error is returned.

      Example: "a1355198-2576-4e3d-af04-5acc77d8a681"

      String

      preserve_expiry
      optional

      Specifies whether documents should keep their current expiration setting when modified by a DML statement.

      If true, documents will keep any existing expiration setting when modified by a DML statement. If the DML statement explicitly specifies the document expiration, the statement overrides this parameter, and the expiration is changed.

      If false, document expiration is set to 0 when modified by a DML statement, unless the DML statement explicitly specifies the document expiration.

      Not supported for statements in a transaction.

      Default: false
      Example: true

      Boolean

      pretty
      optional

      Specifies the query results returned in pretty format.

      Example: false

      Boolean

      profile
      optional

      Specifies if there should be a profile section returned with the request results. The valid values are:

      • off — No profiling information is added to the query response.

      • phases — The query response includes a profile section with stats and details about various phases of the query plan and execution. Three phase times will be included in the system:active_requests and system:completed_requests monitoring keyspaces.

      • timings — Besides the phase times, the profile section of the query response document will include a full query plan with timing and information about the number of processed documents at each phase. This information will be included in the system:active_requests and system:completed_requests keyspaces.

      If profile is not set as one of the above values, then the profile setting does not change.

      Values: "off", "phases", "timings"
      Example: "phases"

      String

      query_context
      optional

      Specifies the namespace, bucket, and scope used to resolve partial keyspace references within the request.

      The query context may be a full path, containing namespace, bucket, and scope; or a relative path, containing just the bucket and scope. Currently, only the default namespace is available. If the namespace name is omitted, the default namespace in the current session is used.

      Default: "default:"
      Example: "default:travel-sample.inventory"

      String

      readonly
      optional

      Controls whether a query can change a resulting recordset.

      If readonly is true, then the following statements are not allowed:

      • CREATE INDEX
      • DROP INDEX
      • INSERT
      • MERGE
      • UPDATE
      • UPSERT

      When using GET requests, it's best to set readonly to true.

      Default: false
      Example: true

      Boolean

      scan_cap
      optional

      Maximum buffered channel size between the indexer client and the query service for index scans. This parameter controls when to use scan backfill.

      Use 0 or a negative number to disable. Smaller values reduce GC, while larger values reduce indexer backfill.

      Example: 1024

      Integer (int32)

      scan_consistency
      optional

      Specifies the consistency guarantee or constraint for index scanning. The valid values are:

      • not_bounded — No timestamp vector is used in the index scan. This is the fastest mode, because it avoids the costs of obtaining the vector and waiting for the index to catch up to the vector.

      • at_plus — This implements bounded consistency. The request includes a scan_vector parameter and value, which is used as a lower bound. This can be used to implement read-your-own-writes (RYOW).

      • request_plus — This implements strong consistency per request. Before processing the request, a current vector is obtained. The vector is used as a lower bound for the statements in the request. If there are DML statements in the request, RYOW is also applied within the request. (If request_plus is specified in a query that runs during a failover of an index node, the query waits until the rebalance operation completes and the index data has rebalanced before returning a result.)

      • statement_plus — This implements strong consistency per statement. Before processing each statement, a current vector is obtained and used as a lower bound for that statement.

      Values are case-insensitive.

      For multi-statement requests, the default behavior is RYOW within each request. If you want to disable RYOW within a request, add a separate request_consistency parameter that can be set to not_bounded.

      If the request contains a BEGIN TRANSACTION statement, or a DML statement with the tximplicit parameter set to true, then this parameter sets the transactional scan consistency. Refer to Transactional Scan Consistency for details.

      Values: "not_bounded", "at_plus", "request_plus", "statement_plus"
      Default: "not_bounded"
      Example: "at_plus"

      String

      scan_vector
      optional

      Required if scan_consistency is at_plus and scan_vectors not provided.

      Specify the lower bound vector timestamp for one keyspace when using at_plus scan consistency.

      Scan vectors are built of two-element [value, guard] entries:

      • value: a vBucket's sequence number (a JSON number)
      • guard: a vBucket's UUID (a string)

      Scan vectors have two forms:

      1. Full: an array of [value, guard] entries, giving an entry for every vBucket in the system.
      2. Sparse: an object providing entries for specific vBuckets, mapping a vBucket number (a string) to each [value, guard] entry.

      Note that scan_vector can only be used if the query uses at most one keyspace; if it is used for a query referencing more than one keyspace, the query will fail with an error.

      For queries referencing multiple keyspaces, use scan_vectors.

      Example: {"5":[5409393,"VB5ID"],"19":[47574574,"VB19ID"]}

      Object

      scan_vectors
      optional

      Required if scan_consistency is at_plus and scan_vector not provided.

      A map from keyspace names to scan vectors. See scan_vector.

      The scan vectors can be Full or Sparse.

      Object

      scan_wait
      optional

      Can be supplied with scan_consistency values of request_plus, statement_plus and at_plus.

      Specifies the maximum time the client is willing to wait for an index to catch up to the vector timestamp in the request.

      Specifies how much time the client is willing to wait for the indexer to satisfy the required scan_consistency and scan_vector criteria. After receiving the scan request, if the indexer is unable to catch up within the scan_wait time and initiate the scan, the indexer aborts with an error and the scan fails.

      Its format includes an amount and a mandatory unit, e.g. 10ms (10 milliseconds) or 0.5s (half a second). Valid units are:

      • ns (nanoseconds)
      • us (microseconds)
      • ms (milliseconds)
      • s (seconds)
      • m (minutes)
      • h (hours)

      Specify 0 or a negative integer to disable.

      Default: ""
      Example: "30m"

      String (duration)

      signature
      optional

      Include a header for the results schema in the response.

      Default: true
      Example: false

      Boolean

      sort_projection
      optional

      If true, causes statement projection terms to be sorted alphabetically.

      If false (the default), statement projection terms are returned in the order specified by the query.

      Default: false
      Example: true

      Boolean

      statement
      optional

      Required if prepared not provided.

      Any valid SQL++ statement for a POST request, or a read-only SQL++ statement (SELECT, EXPLAIN) for a GET request.

      If both prepared and statement are present and non-empty, an error is returned.


      When specifying the request parameters as form data, the statement may not contain an unescaped semicolon (;). If it does, the Query Service responds with error 1040. To avoid this, either URL-encode the semicolon as %3B, or just omit the semicolon if possible.

      This restriction does not apply when specifying the request parameters in JSON format.

      Example: "SELECT * FROM `travel-sample`.inventory.hotel LIMIT 1"

      String

      timeout
      optional

      Maximum time to spend on the request before timing out.

      The value for this parameter is a string. Its format includes an amount and a mandatory unit, e.g. 10ms (10 milliseconds) or 0.5s (half a second). Valid units are:

      • ns (nanoseconds)
      • us (microseconds)
      • ms (milliseconds)
      • s (seconds)
      • m (minutes)
      • h (hours)

      Specify a duration of 0 or a negative duration to disable. When disabled, no timeout is applied and the request runs for however long it takes.

      If tximplicit or txid is set, this parameter is ignored. The request inherits the remaining time of the transaction as timeout.

      Example: "30m"

      String (duration)

      txdata
      optional

      Transaction data. For internal use only.

      Object

      txid
      optional

      Required for statements within a transaction.

      Transaction ID. Specifies the transaction to which a statement belongs. For use with DML statements within a transaction, rollbacks, and commits.

      The transaction ID should be the same as the transaction ID generated by the BEGIN TRANSACTION statement. The transaction must be active and non-expired.

      Example: "d81d9b4a-b758-4f98-b007-87ba262d3a51"

      UUID (UUID)

      tximplicit
      optional

      Specifies that a DML statement is a singleton transaction.

      When this parameter is true, the Query service starts a transaction and executes the statement. If execution is successful, the Query service commits the transaction; otherwise the transaction is rolled back.

      The statement may not be part of an ongoing transaction. If the txid request-level parameter is set, the tximplicit parameter is ignored.

      Default: false
      Example: true

      Boolean

      txstmtnum
      optional

      Transaction statement number. The transaction statement number must be a positive integer, and must be higher than any previous transaction statement numbers in the transaction. If the transaction statement number is lower than the transaction statement number for any previous statement, an error is generated.

      Example: 10

      Integer (int32)

      txtimeout
      optional

      Maximum time to spend on a transaction before timing out. Only applies to BEGIN TRANSACTION statements, or DML statements for which tximplicit is set. For other statements, it is ignored.

      Within a transaction, the request-level timeout parameter is ignored. The transaction timeout clock starts when the BEGIN WORK statement is successful. Once the transaction timeout is reached, no statement is allowed to continue in the transaction.

      The value for this parameter is a string. Its format includes an amount and a mandatory unit, e.g. 10ms (10 milliseconds) or 0.5s (half a second). Valid units are:

      • ns (nanoseconds)
      • us (microseconds)
      • ms (milliseconds)
      • s (seconds)
      • m (minutes)
      • h (hours)

      Specify a duration of 0 to disable. When disabled, the request-level timeout is set to the default.

      The default is "15s" for cbq files or scripts, "2m" for interactive cbq sessions or redirected input.

      Example: "30m"

      String (duration)

      use_cbo
      optional

      Specifies whether the cost-based optimizer is enabled.

      Example: true

      Boolean

      use_fts
      optional

      Specifies that the query should use a full-text index.

      If the query contains a USING FTS hint, that takes priority over this parameter.

      If the query does not contain a USING FTS hint, and this parameter is set to true, all full-text indexes are considered for the query. If a qualified full-text index is available, it is selected for the query. If none of the available full-text indexes are qualified, the available GSI indexes are considered instead.

      Refer to Flex Indexes for more information.

      Default: false
      Example: true

      Boolean

      use_replica
      optional

      Couchbase Server 7.6

      Specifies whether a query can fetch data from a replica vBucket if active vBuckets are inaccessible. The possible values are:

      • off — read from replica is disabled for this request.

      • on — read from replica is enabled for this request, unless it has been disabled for all requests at node level.

      • unset — read from replica is specified by the node-level setting. If the node-level setting is also unset, read from replica is disabled for this request.

      Do not enable read from replica when you require consistent results. Only SELECT queries that are not within a transaction can read from replica.

      Reading from replica is only possible with databases running Couchbase Server 7.6.0 or later.

      Note that KV range scans cannot currently be started on a replica vBucket. If a query uses sequential scan and a data node becomes unavailable, the query might return an error, even if read from replica is enabled for the request.

      Values: "off", "on", "unset"
      Default: "unset"
      Example: "true"

      String

      additional
      property

      Supplies the value for a named parameter in the statement. Applicable if the statement or prepared statement contains 1 or more named parameters.

      The name of this property consists of two parts:

      1. The $ character. In databases running Couchbase Server 7.6.0 and later, this may be the $ character or the @ character.
      2. An identifier that specifies the name of the parameter. This must start with an alpha character, followed by one or more alphanumeric characters.

      The value of the named parameter can be any JSON value.

      Refer to Named Parameters and Positional Parameters for details.

      Any Type

      Transactional Scan Consistency

      If the request contains a BEGIN TRANSACTION statement, or a DML statement with the tximplicit parameter set to true, then the scan_consistency parameter sets the transactional scan consistency. If you specify a transactional scan consistency of request_plus, statement_plus, or at_plus, or if you specify no transactional scan consistency, the transactional scan consistency is set to request_plus; otherwise, the transactional scan consistency is set as specified.

      Table 2. Transactional scan consistency
      Scan consistency at start of transaction Transactional scan consistency

      Not set

      request_plus

      not_bounded

      not_bounded

      request_plus
      statement_plus
      at_plus

      request_plus

      Any DML statements within the transaction that have no scan consistency set will inherit from the transactional scan consistency. Individual DML statements within the transaction may override the transactional scan consistency. If you specify a scan consistency of not_bounded for a statement within the transaction, the scan consistency for the statement is set as specified. When you specify a scan consistency of request_plus, statement_plus, or at_plus for a statement within the transaction, the scan consistency for the statement is set to request_plus.

      However, request_plus consistency is not supported for statements using a full-text index. If any statement within the transaction uses a full-text index, by means of the SEARCH function or the Flex Index feature, the scan consistency is set to not_bounded for the duration of the full-text search.

      Table 3. Overriding the transactional scan consistency
      Scan consistency for statement within transaction Inherited scan consistency

      Not set

      Transactional scan consistency
      (not_bounded for full-text search)

      not_bounded

      not_bounded

      request_plus
      statement_plus
      at_plus

      request_plus
      (not_bounded for full-text search)

      Named Parameters and Positional Parameters

      You can add placeholder parameters to a statement, so that you can safely supply variable values when you run the statement. A placeholder parameter may be a named parameter or a positional parameter.

      • To add a named parameter to a query, enter a dollar sign $ or an at sign @ followed by the parameter name.

      • To add a positional parameter to a query, enter a dollar sign $ or an at sign @ followed by the number of the parameter, or enter a question mark ?.

      To run a query containing placeholder parameters, you must supply values for the parameters.

      • You can use an additional request-level parameter to supply the value for a named parameter. The name of this property is a dollar sign $ or an at sign @ followed by the parameter name.

      • The args request-level parameter enables you to supply a list of values for positional parameters.

      You can supply the values for placeholder parameters using any of the methods used to specify request-level parameters.

      Couchbase Server 7.6

      The at sign @ syntax is only supported in clusters running Couchbase Server 7.6.0 or later. The dollar sign $ syntax is supported in all versions of Couchbase Server.

      Examples

      To try the examples in this section, set the query context to the inventory scope in the travel sample dataset. For more information, see Query Context.

      Example 1. Named Parameters

      • Capella UI

      • Command Line

      The following query uses named parameter placeholders. The parameter values are supplied using the Query Options window.

      Named Parameters

      name

      $country

      value

      "France"

      name

      $altitude

      value

      500

      SELECT COUNT(*) FROM airport
      WHERE country = $country AND geo.alt > @altitude;

      The named parameters and named parameter placeholders in this example use a mixture of @ and $ symbol prefixes. In clusters running Couchbase Server 7.6.0 and later, you can use either of these symbols as preferred. In previous versions of Couchbase Server, you must use the $ symbol prefix.

      The following query uses named parameter placeholders. The parameter values are supplied using the cbq shell.

      \SET -@country "France";
      \SET -$altitude 500;
      
      SELECT COUNT(*) FROM airport
      WHERE country = $country AND geo.alt > @altitude;

      The named parameters and named parameter placeholders in this example use a mixture of @ and $ symbol prefixes. In clusters running Couchbase Server 7.6.0 and later, you can use either of these symbols as preferred. In previous versions of Couchbase Server, you must use the $ symbol prefix.

      Example 2. Numbered Positional Parameters

      • Capella UI

      • Command Line

      The following query uses numbered positional parameter placeholders. The parameter values are supplied using the Query Options window.

      Positional Parameters

      $1

      "France"

      $2

      500

      SELECT COUNT(*) FROM airport
      WHERE country = $1 AND geo.alt > @2;

      In this case, the first positional parameter value is used for the placeholder numbered $1, the second positional parameter value is used for the placeholder numbered @2, and so on.

      The numbered positional parameter placeholders in this example use a mixture of @ and $ symbol prefixes. In clusters running Couchbase Server 7.6.0 and later, you can use either of these symbols as preferred. In previous versions of Couchbase Server, you must use the $ symbol prefix.

      The following query uses numbered positional parameter placeholders. The parameter values are supplied using the cbq shell.

      \SET -args ["France", 500];
      
      SELECT COUNT(*) FROM airport
      WHERE country = $1 AND geo.alt > @2;

      In this case, the first positional parameter value is used for the placeholder numbered $1, the second positional parameter value is used for the placeholder numbered @2, and so on.

      The numbered positional parameter placeholders in this example use a mixture of @ and $ symbol prefixes. In clusters running Couchbase Server 7.6.0 and later, you can use either of these symbols as preferred. In previous versions of Couchbase Server, you must use the $ symbol prefix.

      Example 3. Unnumbered Positional Parameters

      • Capella UI

      • Command Line

      The following query uses unnumbered positional parameter placeholders. The parameter values are supplied using the Query Options window.

      Positional Parameters

      $1

      "France"

      $2

      500

      SELECT COUNT(*) FROM airport
      WHERE country = ? AND geo.alt > ?;

      In this case, the first positional parameter value is used for the first ? placeholder, the second positional parameter value is used for the second ? placeholder, and so on.

      The following query uses unnumbered positional parameter placeholders. The parameter values are supplied using the cbq shell.

      \SET -args ["France", 500];
      
      SELECT COUNT(*) FROM airport
      WHERE country = ? AND geo.alt > ?;

      In this case, the first positional parameter value is used for the first ? placeholder, the second positional parameter value is used for the second ? placeholder, and so on.

      For more details and examples, including examples using SDKs, see the Prepare Statements for Reuse guide.