更新表结构

在 RT 模式下更新表结构

ALTER TABLE table ADD COLUMN column_name [{INTEGER|INT|BIGINT|FLOAT|BOOL|MULTI|MULTI64|JSON|STRING|TIMESTAMP|TEXT [INDEXED [ATTRIBUTE]]}] [engine='columnar']
ALTER TABLE table DROP COLUMN column_name
ALTER TABLE table MODIFY COLUMN column_name bigint

此功能仅支持一次为 RT 表添加一个字段,或将 int 列扩展为 bigint。支持的数据类型有:

  • int - 整数属性
  • timestamp - 时间戳属性
  • bigint - 大整数属性
  • float - 浮点属性
  • bool - 布尔属性
  • multi - 多值整数属性
  • multi64 - 多值大整数属性
  • json - json 属性
  • string / text attribute / string attribute - 字符串属性
  • text / text indexed stored / string indexed stored - 全文索引字段,原始值存储在文档存储中
  • text indexed / string indexed - 仅全文索引字段(原始值不存储在文档存储中)
  • text indexed attribute / string indexed attribute - 全文索引字段 + 字符串属性(不在文档存储中存储原始值)
  • text stored / string stored - 仅存储在文档存储中,不做全文索引,也不是字符串属性
  • 给任何属性(除 json 外)添加 engine='columnar' 会使其存储在列存储

重要说明:

  • ❗建议在执行 ALTER 之前备份表文件,以防突然断电或其他类似问题导致数据损坏。
  • 添加列时无法查询表。
  • 新创建的属性值默认为 0。
  • ALTER 不适用于分布式表和没有任何属性的表。
  • 不能删除 id 列。
  • 删除既是全文字段又是字符串属性的字段时,第一次 ALTER DROP 删除属性,第二次删除全文字段。
  • 添加/删除全文字段仅支持在RT 模式下。
‹›
  • Example
Example
📋
mysql> desc rt;
+------------+-----------+
| Field      | Type      |
+------------+-----------+
| id         | bigint    |
| text       | field     |
| group_id   | uint      |
| date_added | timestamp |
+------------+-----------+
mysql> alter table rt add column test integer;
mysql> desc rt;
+------------+-----------+
| Field      | Type      |
+------------+-----------+
| id         | bigint    |
| text       | field     |
| group_id   | uint      |
| date_added | timestamp |
| test       | uint      |
+------------+-----------+
mysql> alter table rt drop column group_id;
mysql> desc rt;
+------------+-----------+
| Field      | Type      |
+------------+-----------+
| id         | bigint    |
| text       | field     |
| date_added | timestamp |
| test       | uint      |
+------------+-----------+
mysql> alter table rt add column title text indexed;
mysql> desc rt;
+------------+-----------+------------+
| Field      | Type      | Properties |
+------------+-----------+------------+
| id         | bigint    |            |
| text       | text      | indexed    |
| title      | text      | indexed    |
| date_added | timestamp |            |
| test       | uint      |            |
+------------+-----------+------------+
mysql> alter table rt add column title text attribute;
mysql> desc rt;
+------------+-----------+------------+
| Field      | Type      | Properties |
+------------+-----------+------------+
| id         | bigint    |            |
| text       | text      | indexed    |
| title      | text      | indexed    |
| date_added | timestamp |            |
| test       | uint      |            |
| title      | string    |            |
+------------+-----------+------------+
mysql> alter table rt drop column title;
mysql> desc rt;
+------------+-----------+------------+
| Field      | Type      | Properties |
+------------+-----------+------------+
| id         | bigint    |            |
| text       | text      | indexed    |
| title      | text      | indexed    |
| date_added | timestamp |            |
| test       | uint      |            |
+------------+-----------+------------+
mysql> alter table rt drop column title;
mysql> desc rt;
+------------+-----------+------------+
| Field      | Type      | Properties |
+------------+-----------+------------+
| id         | bigint    |            |
| text       | text      | indexed    |
| date_added | timestamp |            |
| test       | uint      |            |
+------------+-----------+------------+

在 RT 模式下更新表全文设置

ALTER TABLE table ft_setting='value'[, ft_setting2='value']

你可以使用 ALTER 修改表在RT 模式下的全文设置。但这只影响新文档,已有文档不受影响。 示例:

  • 创建一个带有全文字段和只允许 3 个可搜索字符(abc)的 charset_table 的表。
  • 然后插入文档 'abcd' 并用查询 abcd 查找,d 被忽略,因为它不在 charset_table 数组中。
  • 之后我们想让 d 也可搜索,于是用 ALTER 添加它。
  • 但同样的查询 where match('abcd') 仍然只搜索了 abc,因为已有文档记住了之前的 charset_table 内容。
  • 然后我们添加另一个文档 abcd 并再次用 abcd 查询。
  • 现在它找到了两个文档,show meta 显示使用了两个关键词:abc(用于旧文档)和 abcd(用于新文档)。
‹›
  • Example
Example
📋
mysql> create table rt(title text) charset_table='a,b,c';
mysql> insert into rt(title) values('abcd');
mysql> select * from rt where match('abcd');
+---------------------+-------+
| id                  | title |
+---------------------+-------+
| 1514630637682688054 | abcd  |
+---------------------+-------+
mysql> show meta;
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| total         | 1     |
| total_found   | 1     |
| time          | 0.000 |
| keyword[0]    | abc   |
| docs[0]       | 1     |
| hits[0]       | 1     |
+---------------+-------+
mysql> alter table rt charset_table='a,b,c,d';
mysql> select * from rt where match('abcd');
+---------------------+-------+
| id                  | title |
+---------------------+-------+
| 1514630637682688054 | abcd  |
+---------------------+-------+
mysql> show meta
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| total         | 1     |
| total_found   | 1     |
| time          | 0.000 |
| keyword[0]    | abc   |
| docs[0]       | 1     |
| hits[0]       | 1     |
+---------------+-------+
mysql> insert into rt(title) values('abcd');
mysql> select * from rt where match('abcd');
+---------------------+-------+
| id                  | title |
+---------------------+-------+
| 1514630637682688055 | abcd  |
| 1514630637682688054 | abcd  |
+---------------------+-------+
mysql> show meta;
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| total         | 2     |
| total_found   | 2     |
| time          | 0.000 |
| keyword[0]    | abc   |
| docs[0]       | 1     |
| hits[0]       | 1     |
| keyword[1]    | abcd  |
| docs[1]       | 1     |
| hits[1]       | 1     |
+---------------+-------+

重命名实时表

你可以在 RT 模式下更改实时表的名称。

ALTER TABLE table_name RENAME new_table_name;

注意:重命名实时表需要 Manticore Buddy。如果不生效,请确保 Buddy 已安装。

‹›
  • Example
Example
📋
ALTER TABLE table_name RENAME new_table_name;
‹›
Response
Query OK, 0 rows affected (0.00 sec)

在普通模式下更新表全文设置

ALTER TABLE table RECONFIGURE

ALTER 也可以重新配置普通模式下的 RT 表,使配置文件中的新分词、形态学和其他文本处理设置对新文档生效。注意,已有文档保持不变。内部实现是强制将当前 RAM 块保存为新的磁盘块,并调整表头,使新文档使用更新后的全文设置进行分词。

‹›
  • Example
Example
📋
mysql> show table rt settings;
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| settings      |       |
+---------------+-------+
1 row in set (0.00 sec)
mysql> alter table rt reconfigure;
Query OK, 0 rows affected (0.00 sec)
mysql> show table rt settings;
+---------------+----------------------+
| Variable_name | Value                |
+---------------+----------------------+
| settings      | morphology = stem_en |
+---------------+----------------------+
1 row in set (0.00 sec)

重建二级索引

ALTER TABLE table REBUILD SECONDARY

你也可以使用 ALTER 重建指定表的二级索引。有时,二级索引可能被禁用,针对整个表或表内一个或多个属性:

  • 当属性被更新时,其二级索引会被禁用。
  • 如果 Manticore 加载了不再支持的旧版本二级索引,整个表的二级索引会被禁用。

ALTER TABLE table REBUILD SECONDARY 会从属性数据重建二级索引并重新启用它们。

此外,旧版本的二级索引可能仍被支持,但缺少某些功能。REBUILD SECONDARY 可用于更新二级索引。

‹›
  • Example
Example
📋
ALTER TABLE rt REBUILD SECONDARY;
‹›
Response
Query OK, 0 rows affected (0.00 sec)

重建 KNN 索引

ALTER TABLE table REBUILD KNN

该命令会重新处理表中所有向量数据,并从头重建 KNN 索引。

‹›
  • Example
Example
📋
ALTER TABLE rt REBUILD KNN;
‹›
Response
Query OK, 0 rows affected (0.00 sec)

在 RT 模式下更新属性 API 密钥(用于生成嵌入)

当使用远程模型自动生成嵌入时,可以用 ALTER 修改 API 密钥:

ALTER TABLE table_name MODIFY COLUMN column_name API_KEY='key';
‹›
  • Example
Example
📋
ALTER TABLE rt MODIFY COLUMN vector API_KEY='key';

更改分布式表

要更改分布式表中的本地或远程节点列表,使用与创建表时相同的语法。只需将命令中的 CREATE 替换为 ALTER,并移除 type='distributed'

ALTER TABLE `distr_table_name` [[local='local_table_name'], [agent='host:port:remote_table'] ... ]

注意:在线更改分布式表的结构需要 Manticore Buddy。如果不生效,请确保 Buddy 已安装。

‹›
  • Example
Example
📋
ALTER TABLE local_dist local='index1' local='index2' agent='127.0.0.1:9312:remote_table';
Last modified: August 28, 2025