ByteHouse 优化器为业界目前唯一的 ClickHouse 优化器方案。ByteHouse 优化器的能力简单总结如下:
开启 ClickHouse SQL Mode:
set enable_optimizer =1
收集表中数据的直方图信息。目前暂未支持自动收集,因此需要手动触发。
-- Collect statistics for all tables in current database
create stats all;
-- Collect statistics for all tables and start sampling
create stats all settings statistics_enable_sample = 1
-- Collect statistics of a table
create stats [IF NOT EXISTS] <table_name>;
-- Collect statistics of a table and start sampling
create stats <table_name> settings statistics_enable_sample = 1
-- Display all statistics
show stats all;
-- Display Statistics of a table
show stats [<db_name>.]<table_name>;
-- Display column level statistics of all tables
show column_stats all;
-- Display specified column level statistics of specified table
show column_stats [<db_name>.]<table_name> [at column <column_name>];
列的介绍:
-- Delete all statistics in current database
drop stats all;
-- Delete statistics of a table
drop stats <table_name>;
set graphviz_path='/path/to/folder'
use <database_name>
show stats __save; # save statistics into /path/to/folder/<database_name>.bin
show stats __load; # load statistics from /path/to/folder/<database_name>.bin
文件是PB格式的,请参考dbms/src/Protos/cbo_statistics.proto
.
支持 EXPLAIN
(返回执行计划,不执行)。
EXPALIN SELECT sum(l_extendedprice * l_discount) AS revenue
FROM lineitem
WHERE l_shipdate >= toDate('1994-01-01')
AND l_shipdate < toDate('1994-01-01') + INTERVAL '1' YEAR
AND l_discount BETWEEN toFloat64('0.06') - toFloat64('0.01') AND toFloat64('0.06') + toFloat64('0.01')
AND l_quantity < 24
set print_graphviz=1
set graphviz_path='/.../...' (A path you can access on the server)
如果性能回退,可以在 query_level 手动指定 SETTINGS enable_optimizer=0
--Add "SETTINGS enable_optimizer=0" after your query to turn off optimizer
select distinct d_year from date_dim SETTINGS enable_optimizer=0;