表可以通过 TRUNCATE TABLE SQL 语句或 truncate() PHP 客户端函数来清空。
以下是 SQL 语句的语法:
TRUNCATE TABLE table_name [WITH RECONFIGURE]
执行此语句时,会完全清除 RT 或分布式表。它会处理内存中的数据,解除所有表数据文件的链接,并释放相关的二进制日志。
对于清空分布式表,请使用不带 with reconfigure 选项的语法。只需对您的分布式表执行标准的 TRUNCATE 语句即可。
TRUNCATE TABLE distributed_table
注意:清空分布式表需要 Manticore Buddy。如果无法使用,请确保 Buddy 已安装。
也可以使用 DELETE FROM index WHERE id>0 来清空表,但不推荐这样做,因为它比 TRUNCATE 慢。
- SQL
- JSON
- PHP
- Python
- Python-asyncio
- javascript
- Java
- C#
- Rust
TRUNCATE TABLE products;POST /cli -d "TRUNCATE TABLE products"$params = [ 'table' => 'products' ];
$response = $client->indices()->truncate($params);utilsApi.sql('TRUNCATE TABLE products')await utilsApi.sql('TRUNCATE TABLE products')res = await utilsApi.sql('TRUNCATE TABLE products');utilsApi.sql("TRUNCATE TABLE products", true);utilsApi.Sql("TRUNCATE TABLE products", true);utils_api.sql("TRUNCATE TABLE products", Some(true)).await;Query OK, 0 rows affected (0.02 sec){
"total":0,
"error":"",
"warning":""
}Array(
[total] => 0
[error] =>
[warning] =>
){u'error': u'', u'total': 0, u'warning': u''}{u'error': u'', u'total': 0, u'warning': u''}{"total":0,"error":"","warning":""}{total=0, error=, warning=}{total=0, error="", warning=""}{total=0, error="", warning=""}此命令的一个可能用途是在 附加表 之前。
当使用 RECONFIGURE 选项时,清空表后,配置中指定的新分词、形态学和其他文本处理设置将生效。如果配置中的 schema 声明 与表的 schema 不同,表清空后将应用配置中的新 schema。
注意:
RECONFIGURE选项仅在 Plain 模式 下有意义,它应用配置文件中的设置。请注意,TRUNCATE仅支持 RT 表,且RECONFIGURE选项仅能在 Manticore 以 Plain 模式运行的 RT 表上使用。
使用此选项时,清空和重新配置表将成为一个原子操作。
- SQL
- HTTP
- PHP
- Python
- Python-asyncio
- javascript
- Java
- C#
- Rust
TRUNCATE TABLE products with reconfigure;POST /cli -d "TRUNCATE TABLE products with reconfigure"$params = [ 'table' => 'products', 'with' => 'reconfigure' ];
$response = $client->indices()->truncate($params);utilsApi.sql('TRUNCATE TABLE products WITH RECONFIGURE')await utilsApi.sql('TRUNCATE TABLE products WITH RECONFIGURE')res = await utilsApi.sql('TRUNCATE TABLE products WITH RECONFIGURE');utilsApi.sql("TRUNCATE TABLE products WITH RECONFIGURE", true);utilsApi.Sql("TRUNCATE TABLE products WITH RECONFIGURE" ,true);utils_api.sql("TRUNCATE TABLE products WITH RECONFIGURE", Some(true)).await;Query OK, 0 rows affected (0.02 sec){
"total":0,
"error":"",
"warning":""
}Array(
[total] => 0
[error] =>
[warning] =>
){u'error': u'', u'total': 0, u'warning': u''}{u'error': u'', u'total': 0, u'warning': u''}{"total":0,"error":"","warning":""}{total=0, error=, warning=}{total=0, error="", warning=""}要理解如何添加带有远程代理的分布式表,首先需要基本了解 分布式表。本文将重点介绍如何使用分布式表作为创建 Manticore 实例集群的基础。
以下是一个将数据拆分到 4 台服务器上的示例,每台服务器负责一个分片:
- ini
table mydist {
type = distributed
agent = box1:9312:shard1
agent = box2:9312:shard2
agent = box3:9312:shard3
agent = box4:9312:shard4
}如果发生服务器故障,分布式表仍能工作,但来自故障分片的结果将会缺失。
现在我们已经添加了镜像,每个分片都位于 2 台服务器上。默认情况下,主节点(拥有分布式表的 searchd 实例)会随机选择一个镜像。
用于选择镜像的模式可以通过 ha_strategy 设置来配置。除了默认的 random 模式外,还有 ha_strategy = roundrobin。
基于延迟加权概率的更高级策略包括 noerrors 和 nodeads。这些策略不仅会排除有问题的镜像,还会监控响应时间并进行负载均衡。如果某个镜像响应较慢(例如,由于其上正在运行某些操作),它将收到更少的请求。当镜像恢复并提供更好的响应时间时,它将收到更多的请求。
- ini
table mydist {
type = distributed
agent = box1:9312|box5:9312:shard1
agent = box2:9312:|box6:9312:shard2
agent = box3:9312:|box7:9312:shard3
agent = box4:9312:|box8:9312:shard4
}