可以使用 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 选项时,配置中指定的新分词、形态学和其他文本处理设置将在表被清空后生效。如果配置中的模式声明与表模式不同,则清空表后将应用配置中的新模式。
注意:
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
}