ClickHouse/docs/en/sql-reference/statements/select/prewhere.md
Ivan Blinkov aa17da6b68
[docs] adjust SELECT ToC meta (#10942)
* [docs] adjust SELECT ToC meta

* Update array-join.md

* Update distinct.md

* Update format.md

* Update from.md

* Update group-by.md

* Update having.md

* Update into-outfile.md

* Update join.md

* Update limit-by.md

* Update limit.md

* Update order-by.md

* Update prewhere.md

* Update sample.md

* Update union-all.md

* Update where.md

* Update with.md
2020-05-15 14:25:18 +03:00

1.6 KiB

toc_title
PREWHERE

PREWHERE Clause

Prewhere is an optimization to apply filtering more efficiently. It is enabled by default even if PREWHERE clause is not specified explicitly. It works by automatically moving part of WHERE condition to prewhere stage. The role of PREWHERE clause is only to control this optimization if you think that you know how to do it better than it happens by default.

With prewhere optimization, at first only the columns necessary for executing prewhere expression are read. Then the other columns are read that are needed for running the rest of the query, but only those blocks where the prewhere expression is "true" at least for some rows. If there are a lot of blocks where prewhere expression is "false" for all rows and prewhere needs less columns than other parts of query, this often allows to read a lot less data from disk for query execution.

Controlling Prewhere Manually

The clause has the same meaning as the WHERE clause. The difference is in which data is read from the table. When manually controlling PREWHERE for filtration conditions that are used by a minority of the columns in the query, but that provide strong data filtration. This reduces the volume of data to read.

A query may simultaneously specify PREWHERE and WHERE. In this case, PREWHERE precedes WHERE.

If the optimize_move_to_prewhere setting is set to 0, heuristics to automatically move parts of expressions from WHERE to PREWHERE are disabled.

Limitations

PREWHERE is only supported by tables from the *MergeTree family.