Skip to content

Instantly share code, notes, and snippets.

@filimonov
Created May 14, 2018 12:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save filimonov/dd62b964cf3692ef272f3bc479ef50fe to your computer and use it in GitHub Desktop.
Save filimonov/dd62b964cf3692ef272f3bc479ef50fe to your computer and use it in GitHub Desktop.
Clickhouse prewhere demonstration
SELECT version()
┌─version()─┐
│ 1.1.54380 │
└───────────┘
SELECT *
FROM system.settings
WHERE name LIKE '%prewh%';
┌─name──────────────────────┬─value─┬─changed─┬─description───────────────────────────────────────────────────────────────────────┐
│ optimize_move_to_prewhere │ 1 │ 0 │ Allows disabling WHERE to PREWHERE optimization in SELECT queries from MergeTree. │
└───────────────────────────┴───────┴─────────┴───────────────────────────────────────────────────────────────────────────────────┘
DROP TABLE IF EXISTS prewhere_test;
CREATE TABLE prewhere_test
(
number UInt64,
date Date DEFAULT '2018-05-14',
column001 String DEFAULT concat('abc', toString(rand(1)), 'def'),
column002 String DEFAULT if(number = 500, 'look_for_me_please', ''),
hugecolumn String DEFAULT arrayStringConcat( arrayMap(x -> concat('abc', toString(rand64(x)), 'def'), range(500)) , 'sadfasdfasdf')
)
ENGINE = MergeTree(date, (number,column001,column002), 8192);
insert into prewhere_test(number) select * from numbers(1000);
set max_bytes_to_read = 100000;
-- prewhere works perfect
select * from prewhere_test prewhere column002 = 'look_for_me_please' order by number;
-- where condition should be optimized to prewhere, but it don't if the column is the part of PK.
select * from prewhere_test where column002 = 'look_for_me_please' order by number;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment