DISCARD
Calculates SELECT without returning the result neither to the client or table.
It's good to combine it with Ensure to check the final calculation result against the user's criteria.
A query with DISCARD is executed in full — with all its filters, aggregations, and Ensure checks — but the result set is not returned to the client. The data is not sent over the network either: the client receives only the query execution status. For large outputs, this saves a noticeable amount of traffic and memory.
In a query consisting of multiple statements, DISCARD applies to an individual statement:
SELECT 1; -- returned
DISCARD SELECT 2; -- executed, but not included in the response
SELECT 3; -- returned
The client will receive two result sets — from the first and third statements.
DISCARD applies only to a statement as a whole and cannot be used inside expressions. In particular, it is not allowed:
- in subqueries —
SELECT * FROM (DISCARD SELECT 1); - in
WHERE ... IN (...)—SELECT * FROM my_table WHERE Key IN (DISCARD SELECT 1); - in
UNION ALLoperands —SELECT 1 UNION ALL DISCARD SELECT 2.
Note
DISCARD is supported for queries executed via the Query Service starting from YDB version 26.2. When a query is executed via the legacy interfaces (Table Service, scan queries), DISCARD is ignored and the result is returned to the client — this behavior is preserved for backward compatibility.
Examples
DISCARD SELECT 1;
DISCARD SELECT Ensure(
Data,
Data < 1000000,
"Value too big"
) FROM `result_table`;
If the Ensure condition is violated, the query fails with an error. Otherwise, the query completes successfully, and the client does not receive the result set that would otherwise have to be downloaded in full.