Skip to main content

SET_VAR

SET_VAR is used to specify optimizer hints within a single SQL statement, allowing for finer control over the execution plan of that specific statement. This includes:

  • Configure settings temporarily, affecting only the duration of the SQL statement execution. It's important to note that the settings specified with SET_VAR will solely impact the result of the current statement being executed and will not have any lasting effects on the overall database configuration. For a list of available settings that can be configured using SET_VAR, see SHOW SETTINGS. To understand how it works, see Example 1. Temporarily Set Timezone.

  • Control the deduplication behavior on INSERT, UPDATE, or REPLACE operations with the label deduplicate_label. For those operations with a deduplicate_label in the SQL statements, Databend executes only the first statement, and subsequent statements with the same deduplicate_label value are ignored, regardless of their intended data modifications. Please note that once you set a deduplicate_label, it will remain in effect for a period of 24 hours. To understand how the deduplicate_label assists in deduplication, see Example 2: Set Deduplicate Label.

See also: SET

Syntax

/*+ SET_VAR(key=value) SET_VAR(key=value) ... */
  • The hint must immediately follow an SELECT, INSERT, UPDATE, REPLACE, DELETE, or COPY (INTO) keyword that begins the SQL statement.
  • A SET_VAR can include only one Key=Value pair, which means you can configure only one setting with one SET_VAR. However, you can use multiple SET_VAR hints to configure multiple settings.
    • If multiple SET_VAR hints containing a same key, the first Key=Value pair will be applied.
    • If a key fails to parse or bind, all hints will be ignored.

Examples

Example 1: Temporarily Set Timezone

root@localhost> SELECT TIMEZONE();

SELECT
TIMEZONE();

┌────────────┐
│ timezone()
│ String │
├────────────┤
│ UTC │
└────────────┘

1 row in 0.011 sec. Processed 1 rows, 1B (91.23 rows/s, 91B/s)

root@localhost> SELECT /*+SET_VAR(timezone='America/Toronto') */ TIMEZONE();

SELECT
/*+SET_VAR(timezone='America/Toronto') */
TIMEZONE();

┌─────────────────┐
│ timezone()
│ String │
├─────────────────┤
│ America/Toronto │
└─────────────────┘

1 row in 0.023 sec. Processed 1 rows, 1B (43.99 rows/s, 43B/s)

root@localhost> SELECT TIMEZONE();

SELECT
TIMEZONE();

┌────────────┐
│ timezone()
│ String │
├────────────┤
│ UTC │
└────────────┘

1 row in 0.010 sec. Processed 1 rows, 1B (104.34 rows/s, 104B/s)

Example 2: Set Deduplicate Label

CREATE TABLE t1(a Int, b bool);
INSERT /*+ SET_VAR(deduplicate_label='databend') */ INTO t1 (a, b) VALUES(1, false);
SELECT * FROM t1;

a|b|
-+-+
1|0|

UPDATE /*+ SET_VAR(deduplicate_label='databend') */ t1 SET a = 20 WHERE b = false;
SELECT * FROM t1;

a|b|
-+-+
1|0|

REPLACE /*+ SET_VAR(deduplicate_label='databend') */ INTO t1 on(a,b) VALUES(40, false);
SELECT * FROM t1;

a|b|
-+-+
1|0|