更新表结构

在RT模式下更新表结构

ALTER TABLE table ADD COLUMN column_name [{INTEGER|INT|BIGINT|FLOAT|BOOL|MULTI|MULTI64|JSON [secondary_index='1']|STRING|TEXT [INDEXED [ATTRIBUTE]]|TIMESTAMP|FLOAT_VECTOR [KNN options]}] [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 - 多值bigint属性
  • json - JSON 属性;使用 secondary_index='1' 为 JSON 创建二级索引
  • string / text attribute / string attribute - 字符串属性
  • text / text indexed stored / string indexed stored - 全文索引字段,原始值存储在docstore中
  • text indexed / string indexed - 全文索引字段,仅索引(原始值不存储在docstore中)
  • text indexed attribute / string indexed attribute - 全文索引字段 + 字符串属性(不将原始值存储在docstore中)
  • text stored / string stored - 值仅存储在docstore中,不进行全文索引,也不是字符串属性
  • float_vector - 向量属性。您可以使用与 CREATE TABLE 中相同的 KNN 和 auto-embedding 选项
  • 为任何属性(json除外)添加engine='columnar'将使其存储在列式存储

重要注意事项:

  • ❗建议在ALTER表之前备份表文件,以防突然断电或其他类似问题导致数据损坏。
  • 添加列时无法查询表。
  • 新创建的标量属性默认设置为 0
  • 新增的 float_vector 列如果没有 MODEL_NAME,则初始化为零向量。
  • 如果添加带有 MODEL_NAMEFROMfloat_vector 列,ALTER TABLE ... ADD COLUMN 期间现有行会自动嵌入。
  • 指定 MODEL_NAME 时,FROM 是必需的。使用 FROM='' 从所有 text 字段和 string 属性中嵌入。
  • 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']

您可以使用ALTERRT模式下修改表的全文设置。但是,它只影响新文档,不影响现有文档。 示例:

  • 创建一个具有全文字段和charset_table的表,该表只允许3个可搜索字符:abc
  • 然后我们插入文档'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)

重建嵌入

ALTER TABLE table REBUILD EMBEDDINGS column_name

此命令重新生成一个目标 float_vector 列的嵌入,该列配置了 MODEL_NAMEFROM

在需要为现有嵌入列重新生成向量时使用此功能,例如在使用 ALTER TABLE ... ADD COLUMN 后稍后添加该列并希望重新处理行,或希望强制为所有行重新生成向量时。

重要行为:

  • 列名是必填项。该命令一次仅重建一个嵌入列。
  • 为该列中的所有行重新生成嵌入,而不仅仅是向量为零的行。
  • 它还会覆盖那些手动插入向量的行,以及使用 () 跳过生成并存储零向量的行。
  • 目标列必须是带有嵌入模型配置的索引 float_vector
  • 允许 FROM='',表示“使用所有 text 字段和 string 属性”。

Manticore 不会持久化该列中当前向量是自动生成、由用户显式提供,还是从 () 创建的。如果你运行 REBUILD EMBEDDINGS,存储的值将从配置的 FROM 源为该列中的每一行重新生成,包括当前值为全零向量的行。

‹›
  • Example
Example
📋
ALTER TABLE products ADD COLUMN embedding FLOAT_VECTOR KNN_TYPE='hnsw' HNSW_SIMILARITY='l2' MODEL_NAME='sentence-transformers/all-MiniLM-L6-v2' FROM='title';
ALTER TABLE products REBUILD EMBEDDINGS embedding;
‹›
Response
Query OK, 0 rows affected (0.00 sec)

在 RT 模式下更新嵌入生成的属性 API 参数

当使用远程模型进行自动嵌入时,可以使用 ALTER 修改 API 参数:

ALTER TABLE table_name MODIFY COLUMN column_name API_KEY='key';
ALTER TABLE table_name MODIFY COLUMN column_name API_URL='url';
ALTER TABLE table_name MODIFY COLUMN column_name API_TIMEOUT='seconds';
‹›
  • Example
Example
📋
ALTER TABLE rt MODIFY COLUMN vector API_KEY='new-key';
ALTER TABLE rt MODIFY COLUMN vector API_URL='https://custom-api.example.com/v1/embeddings';
ALTER TABLE rt MODIFY COLUMN vector API_TIMEOUT='30';

注意事项:

  • API_KEY:在 ALTER 操作期间通过实际 API 请求验证新 API 密钥。
  • API_URL:设置为空字符串 ('') 以恢复到默认提供方端点。
  • API_TIMEOUT:设置为 '0' 以使用默认超时时间(10 秒)。必须是非负整数。

更改分布式表

要更改分布式表中的本地或远程节点列表,请遵循与创建表时相同的语法。只需将命令中的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: March 27, 2026