manual/chinese/Emptying_a_table.md
表可以通过 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 慢。
TRUNCATE TABLE products;
Query OK, 0 rows affected (0.02 sec)
POST /cli -d "TRUNCATE TABLE products"
{
"total":0,
"error":"",
"warning":""
}
$params = [ 'table' => 'products' ];
$response = $client->indices()->truncate($params);
Array(
[total] => 0
[error] =>
[warning] =>
)
utilsApi.sql('TRUNCATE TABLE products')
{u'error': u'', u'total': 0, u'warning': u''}
await utilsApi.sql('TRUNCATE TABLE products')
{u'error': u'', u'total': 0, u'warning': u''}
res = await utilsApi.sql('TRUNCATE TABLE products');
{"total":0,"error":"","warning":""}
utilsApi.sql("TRUNCATE TABLE products", true);
{total=0, error=, warning=}
utilsApi.Sql("TRUNCATE TABLE products", true);
{total=0, error="", warning=""}
utils_api.sql("TRUNCATE TABLE products", Some(true)).await;
{total=0, error="", warning=""}
此命令的一个可能用途是在 附加表 之前。
<!-- example truncate with RECONFIGURE -->当使用 RECONFIGURE 选项时,清空表后,配置中指定的新分词、形态学和其他文本处理设置将生效。如果配置中的 schema 声明 与表的 schema 不同,表清空后将应用配置中的新 schema。
注意:
RECONFIGURE选项仅在 Plain 模式 下有意义,它应用配置文件中的设置。请注意,TRUNCATE仅支持 RT 表,且RECONFIGURE选项仅能在 Manticore 以 Plain 模式运行的 RT 表上使用。
使用此选项时,清空和重新配置表将成为一个原子操作。
<!-- intro -->TRUNCATE TABLE products with reconfigure;
Query OK, 0 rows affected (0.02 sec)
POST /cli -d "TRUNCATE TABLE products with reconfigure"
{
"total":0,
"error":"",
"warning":""
}
$params = [ 'table' => 'products', 'with' => 'reconfigure' ];
$response = $client->indices()->truncate($params);
Array(
[total] => 0
[error] =>
[warning] =>
)
utilsApi.sql('TRUNCATE TABLE products WITH RECONFIGURE')
{u'error': u'', u'total': 0, u'warning': u''}
await utilsApi.sql('TRUNCATE TABLE products WITH RECONFIGURE')
{u'error': u'', u'total': 0, u'warning': u''}
res = await utilsApi.sql('TRUNCATE TABLE products WITH RECONFIGURE');
{"total":0,"error":"","warning":""}
utilsApi.sql("TRUNCATE TABLE products WITH RECONFIGURE", true);
{total=0, error=, warning=}
utilsApi.Sql("TRUNCATE TABLE products WITH RECONFIGURE" ,true);
{total=0, error="", warning=""}
utils_api.sql("TRUNCATE TABLE products WITH RECONFIGURE", Some(true)).await;
{total=0, error="", warning=""}