Running a query
The table query execute
subcommand is designed for reliable execution of YQL queries. With this sub-command, you can successfully execute your query when certain table partitions are unavailable for a short time (for example, due to being split or merged) by using built-in retry policies.
General format of the command:
ydb [global options...] table query execute [options...]
global options
: Global parameters.options
: Parameters of the subcommand.
View the description of the YQL query command:
ydb table query execute --help
Parameters of the subcommand
Name |
Description |
|
The time within which the operation should be completed on the server. |
|
Query type.
|
|
Statistics mode.
|
|
Enable statistics collection in the |
|
Transaction mode (for serializable-rw : The result of parallel transactions is equivalent to their serial execution.online-ro : Each of the reads in the transaction reads data that is most recent at the time of its execution.stale-ro : Data reads in a transaction return results with a possible delay (fractions of a second).Default value: serializable-rw .
|
|
Text of the YQL query to be executed. |
|
Path to the text of the YQL query to be executed. |
|
Result format.
|
Working with parameterized queries
A brief help is provided below. For a detailed description with examples, see Running parametrized YQL queries and scripts.
Name | Description |
---|---|
-p, --param |
The value of a single parameter of a YQL query, in the format: $name=value , where $name is the parameter name and value is its value (a valid JSON value). |
--param-file |
Name of the file in JSON format and in UTF-8 encoding that specifies values of the parameters matched against the YQL query parameters by key names. |
--input-format |
Format of parameter values. Applies to all the methods of parameter transmission (among command parameters, in a file or using stdin ).Acceptable values: |
--stdin-format |
The parameter format and framing for stdin . To set both values, specify the parameter twice.Format of parameter encoding for stdin Acceptable values:
stdin isn't specified, the format set in --input-format is used.Classification of parameter sets for stdin (framing)Acceptable values:
|
--stdin-par |
The name of the parameter whose value will be sent over stdin is specified without a $ . |
--batch |
The batch mode of transmitting parameter sets received via stdin .Acceptable values:
|
--batch-limit |
A maximum number of sets of parameters per batch in the adaptive batch mode. The setting of 0 removes the limit.The default value is 1000 . |
--batch-max-delay |
The maximum delay related to processing the resulting parameter set in the adaptive batch mode. It's set as a number of s , ms , m .Default value: 1s (1 second). |
Examples
Note
The examples use the quickstart
profile. To learn more, see Creating a profile to connect to a test database.
Creating tables
ydb -p quickstart table query execute \
--type scheme \
-q '
CREATE TABLE series (series_id Uint64 NOT NULL, title Utf8, series_info Utf8, release_date Date, PRIMARY KEY (series_id));
CREATE TABLE seasons (series_id Uint64, season_id Uint64, title Utf8, first_aired Date, last_aired Date, PRIMARY KEY (series_id, season_id));
CREATE TABLE episodes (series_id Uint64, season_id Uint64, episode_id Uint64, title Utf8, air_date Date, PRIMARY KEY (series_id, season_id, episode_id));
'
Populating the table with data
ydb -p quickstart table query execute \
-q '
UPSERT INTO series (series_id, title, release_date, series_info) VALUES
(1, "IT Crowd", Date("2006-02-03"), "The IT Crowd is a British sitcom produced by Channel 4, written by Graham Linehan, produced by Ash Atalla and starring Chris O'"'"'Dowd, Richard Ayoade, Katherine Parkinson, and Matt Berry."),
(2, "Silicon Valley", Date("2014-04-06"), "Silicon Valley is an American comedy television series created by Mike Judge, John Altschuler and Dave Krinsky. The series focuses on five young men who founded a startup company in Silicon Valley.");
UPSERT INTO seasons (series_id, season_id, title, first_aired, last_aired) VALUES
(1, 1, "Season 1", Date("2006-02-03"), Date("2006-03-03")),
(1, 2, "Season 2", Date("2007-08-24"), Date("2007-09-28")),
(2, 1, "Season 1", Date("2014-04-06"), Date("2014-06-01")),
(2, 2, "Season 2", Date("2015-04-12"), Date("2015-06-14"));
UPSERT INTO episodes (series_id, season_id, episode_id, title, air_date) VALUES
(1, 1, 1, "Yesterday'"'"'s Jam", Date("2006-02-03")),
(1, 1, 2, "Calamity Jen", Date("2006-02-03")),
(2, 1, 1, "Minimum Viable Product", Date("2014-04-06")),
(2, 1, 2, "The Cap Table", Date("2014-04-13"));
'
Simple data selection
ydb -p quickstart table query execute -q '
SELECT season_id, episode_id, title
FROM episodes
WHERE series_id = 1
'
Result:
┌───────────┬────────────┬───────────────────┐
| season_id | episode_id | title |
├───────────┼────────────┼───────────────────┤
| 1 | 1 | "Yesterday's Jam" |
├───────────┼────────────┼───────────────────┤
| 1 | 2 | "Calamity Jen" |
└───────────┴────────────┴───────────────────┘
Unlimited selection for automated processing
Selecting data by a query whose text is saved to a file, without a limit on the number of rows in the selection and data output in the format: Newline-delimited JSON stream.
Let's write the query text to the request1.yql
file.
echo 'SELECT season_id, episode_id, title FROM episodes' > request1.yql
Now, run the query:
ydb -p quickstart table query execute -f request1.yql --type scan --format json-unicode
Result:
{"season_id":1,"episode_id":1,"title":"Yesterday's Jam"}
{"season_id":1,"episode_id":2,"title":"Calamity Jen"}
{"season_id":1,"episode_id":1,"title":"Minimum Viable Product"}
{"season_id":1,"episode_id":2,"title":"The Cap Table"}
Passing parameters
You can find examples of executing parameterized queries, including streamed processing, in the Passing parameters to YQL execution commands article.