mirror of
https://github.com/ClickHouse/ClickHouse.git
synced 2024-09-29 13:10:48 +00:00
40 lines
2.6 KiB
Markdown
40 lines
2.6 KiB
Markdown
|
# FROM Clause {#select-from}
|
|||
|
|
|||
|
The `FROM` clause specifies the source to read data from:
|
|||
|
|
|||
|
- [Table](../../../engines/table-engines/index.md)
|
|||
|
- [Subquery](index.md) {## TODO: better link ##}
|
|||
|
- [Table function](../../table-functions/index.md#table-functions)
|
|||
|
|
|||
|
[JOIN](join.md) and [ARRAY JOIN](array-join.md) clauses may also be used to extend the functionality of the `FROM` clause.
|
|||
|
|
|||
|
Subquery is another `SELECT` query that may be specified in parenthesis inside `FROM` clause.
|
|||
|
|
|||
|
`FROM` clause can contain multiple data sources, separated by commas, which is equivalent of performing [CROSS JOIN](join.md) on them.
|
|||
|
|
|||
|
## FINAL Modifier {#select-from-final}
|
|||
|
|
|||
|
When `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](../../../engines/table-engines/mergetree-family/mergetree.md)-engine family (except `GraphiteMergeTree`). Also supported for:
|
|||
|
|
|||
|
- [Replicated](../../../engines/table-engines/mergetree-family/replication.md) versions of `MergeTree` engines.
|
|||
|
- [View](../../../engines/table-engines/special/view.md), [Buffer](../../../engines/table-engines/special/buffer.md), [Distributed](../../../engines/table-engines/special/distributed.md), and [MaterializedView](../../../engines/table-engines/special/materializedview.md) engines that operate over other engines, provided they were created over `MergeTree`-engine tables.
|
|||
|
|
|||
|
### Drawbacks
|
|||
|
|
|||
|
Queries that use `FINAL` are executed not as fast as similar queries that don’t, because:
|
|||
|
|
|||
|
- Query is executed in a single thread and data is merged during query execution.
|
|||
|
- Queries with `FINAL` read 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). {## TODO: examples ##}
|
|||
|
|
|||
|
## Implementation Details
|
|||
|
|
|||
|
If the `FROM` clause is omitted, data will be read from the `system.one` table.
|
|||
|
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.
|