跳至主要内容

如何确认查询是否使用了投影?

了解如何通过使用示例数据和使用 EXPLAIN 来验证投影使用情况,来检查投影是否在 ClickHouse 查询中使用。

问题

如何判断是否使用了投影?

答案

  1. 创建一个示例数据库
CREATE database db1;
  1. 创建一个示例表,该表将使用 column1 作为主键
CREATE table db1.table1_projections
(
 column1 Int32,
 column2 Int32
)
engine = MergeTree()
order by column1;
  1. 添加一个投影 for_column2,使用 column2 作为主键
ALTER table db1.table1_projections add projection for_column2
(
  select * 
  order by column2
);
  1. 插入测试数据

*这会插入 100000 行,column1 和 column2 中包含随机数

INSERT INTO db1.table1_projections
select 
 floor(randNormal(50, 5)) as column1,
 floor(randUniform(1, 100)) as column2
from numbers(100000);
  1. 检查示例数据集
clickhouse-cloud :) SELECT * from db1.table1_projections limit 5;

SELECT *
FROM db1.table1_projections
LIMIT 5

Query id: d6940799-b507-4a5e-9843-df55ebe818ab

┌─column1─┬─column2─┐
│      28 │      41 │
│      29 │      12 │
│      30 │      73 │
│      30 │      75 │
│      30 │      70 │
└─────────┴─────────┘
  1. 测试是否正在使用带有 column1 的原始表
clickhouse-cloud :) explain indexes = 1 
                    SELECT count() from db1.table1_projections where column1 > 50;

EXPLAIN indexes = 1
SELECT count()
FROM db1.table1_projections
WHERE column1 > 50

Query id: e04d5236-1a05-4f1f-9502-7e41986beb44

┌─explain────────────────────────────────────────────┐
│ Expression ((Projection + Before ORDER BY))        │
│   Aggregating                                      │
│     Expression (Before GROUP BY)                   │
│       Filter (WHERE)                               │
│         ReadFromMergeTree (db1.table1_projections) │
│         Indexes:                                   │
│           PrimaryKey                               │
│             Condition: true                        │
│             Parts: 1/1                             │
│             Granules: 12/12                        │
└────────────────────────────────────────────────────┘

*请注意,它正在从 db1.table1_projections 读取

  1. 通过在 where 子句中使用 column2 来测试从投影读取
clickhouse-cloud :) explain indexes = 1 
                    SELECT * from db1.table1_projections where column2 > 50;

EXPLAIN indexes = 1
SELECT *
FROM db1.table1_projections
WHERE column2 > 50

Query id: d2b20e01-93bf-4b60-a370-4aac7b454267

┌─explain─────────────────────────────────────┐
│ Expression ((Projection + Before ORDER BY)) │
│   Filter                                    │
│     ReadFromMergeTree (for_column2)         │
│     Indexes:                                │
│       PrimaryKey                            │
│         Keys:                               │
│           column2                           │
│         Condition: (column2 in [51, +Inf))  │
│         Parts: 1/1                          │
│         Granules: 6/12                      │
└─────────────────────────────────────────────┘

*请注意,现在使用了 for_column2 投影。

更多信息

投影:https://clickhouse.ac.cn/docs/sql-reference/statements/alter/projection

numbers 表函数:https://clickhouse.ac.cn/docs/sql-reference/table-functions/numbers

生成随机数据的博客:https://clickhouse.ac.cn/blog/generating-random-test-distribution-data-for-clickhouse

·2 分钟阅读
    © . This site is unofficial and not affiliated with ClickHouse, Inc.