1

我正在尝试使用以下语法在现有表上添加索引。

创建表contact_in.....(.......域字符串,主题字符串,类别字符串........................)ENGINE = MergeTree PARTITION BY category ORDER BY (topic, domain) 设置 index_granularity = 8192

I want to create an index on the topic column (granularity is 6020)

tried syntax from the documentation but unable to understand since there is no examples explaining the fields in it.

有人可以快速帮助我吗?

4

3 回答 3

2

我会把它留在这里,以防有人会寻找相同的信息。
语法是:

ALTER TABLE  
[db name].[table name]  
ADD INDEX  
[any name for index]([columns to use])  
TYPE [index type: minmax, set, ngrambf_v1, tokenbf_v1 or bloom_filter - more info below]  
GRANULARITY [you can use default value - 8192]

有关索引类型的更多信息在这里

于 2021-08-26T03:55:30.693 回答
1

是的你可以。

可以使用以下操作:

ALTER TABLE [db].name ADD INDEX name expression TYPE type GRANULARITY value AFTER name [AFTER name2] - Adds index description to tables metadata.

ALTER TABLE [db].name DROP INDEX name - Removes index description from tables metadata and deletes index files from disk.

ALTER TABLE [db.]table MATERIALIZE INDEX name IN PARTITION partition_name - The query rebuilds the secondary index name in the partition partition_name. Implemented as a mutation.

https://clickhouse.tech/docs/en/sql-reference/statements/alter/index/

于 2020-07-16T15:31:33.100 回答
1

添加索引

这会将元数据添加到您的表中。旧数据未编入索引!新数据将被索引!操作轻巧。

ALTER TABLE [db].name ADD INDEX name expression TYPE type GRANULARITY value [FIRST|AFTER name]

删除索引

元数据和索引文件被删除。轻量级操作,因为这对数据库来说很容易。

ALTER TABLE [db].name DROP INDEX name

实现索引

这将重新创建提到的索引。表中的所有数据都将被索引。通常,您将在将索引添加到表以将预先存在的数据包含到索引后运行此操作。

ALTER TABLE [db.]table MATERIALIZE INDEX name IN PARTITION partition_name

重建所有索引

您可以通过优化表来强制使用所有表数据重新创建所有索引:

OPTIMIZE TABLE [db].name FINAL;

测试指标

Clickhouse 没有查询提示,只有设置,类似,可以添加到任何查询中。有一些用于控制索引的设置:

use_skip_indexes- 查询执行期间的索引 (>=v21.11)。可能的值:

  • 0 — 禁用
  • 1 — 启用(默认)

force_data_skipping_indices 如果未使用传递的数据跳过索引 (>=v20.6.8.5),则禁用查询执行。

例子:

SELECT * FROM my_table WHERE my_column=1 SETTINGS use_skip_indexes=0;
SELECT * FROM my_table WHERE my_column=1 SETTINGS force_data_skipping_indices='my_index_name';

-- if index is not exiting DB will throw an error:
SELECT * FROM my_table WHERE my_column=1 SETTINGS force_data_skipping_indices='my_non_existing_index_name';
-- > DB::Exception: Index `my_non_existing_index_name` is not used and setting 'force_data_skipping_indices' contains it 

Clickhouse 文档

于 2022-01-19T13:39:12.670 回答