FROM clause specifies the source to read data from:
Subquery is another
SELECT query that may be specified in parenthesis inside
FROM clause can contain multiple data sources, separated by commas, which is equivalent of performing CROSS JOIN on them.
FINAL is specified, ClickHouse fully merges the data before returning the result and thus performs all data transformations that happen during merges for the given table engine.
It is applicable when selecting data from tables that use the MergeTree-engine family (except
GraphiteMergeTree). Also supported for:
- Replicated versions of
- View, Buffer, Distributed, and MaterializedView engines that operate over other engines, provided they were created over
SELECT queries with
FINAL are executed in parallel and slightly faster. But there are drawbacks (see below). The max_final_threads setting limits the number of threads used.
Queries that use
FINAL are executed slightly slower than similar queries that don’t, because:
- Data is merged during query execution.
- Queries with
FINALread primary key columns in addition to the columns specified in the query.
In most cases, avoid using
FINAL. The common approach is to use different queries that assume the background processes of the
MergeTree engine have’t happened yet and deal with it by applying aggregation (for example, to discard duplicates).
FROM clause is omitted, data will be read from the
system.one table contains exactly one row (this table fulfills the same purpose as the DUAL table found in other DBMSs).
To execute a query, all the columns listed in the query are extracted from the appropriate table. Any columns not needed for the external query are thrown out of the subqueries.
If a query does not list any columns (for example,
SELECT count() FROM t), some column is extracted from the table anyway (the smallest one is preferred), in order to calculate the number of rows.