删除表在内部执行分两个步骤:
- 清空表(类似于 TRUNCATE)
- 删除表文件夹中的所有表文件。所有表使用的外部表文件(例如词形变化、扩展或停用词)也会被删除。注意,当使用
CREATE TABLE时,这些外部文件会被复制到表文件夹,因此不会删除CREATE TABLE中指定的原始文件。
只有在服务器以 RT 模式运行时才可以删除表。可以删除 RT 表、PQ 表和分布式表。
- SQL
- JSON
- PHP
- Python
- Python-asyncio
- javascript
- Java
- C#
- Rust
DROP TABLE products;POST /cli -d "DROP TABLE products"$params = [ 'table' => 'products' ];
$response = $client->indices()->drop($params);utilsApi.sql('DROP TABLE products')await utilsApi.sql('DROP TABLE products')res = await utilsApi.sql('DROP TABLE products');sqlresult = utilsApi.sql("DROP TABLE products", true);sqlresult = utilsApi.Sql("DROP TABLE products", true);let sqlresult = utils_api.sql("DROP 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=""}以下是 SQL 中 DROP TABLE 语句的语法:
DROP TABLE [IF EXISTS] table_name
通过 SQL 删除表时,添加 IF EXISTS 可以仅在表存在时删除该表。如果尝试删除不存在的表且使用了 IF EXISTS 选项,则不会有任何操作。
通过 PHP 删除表时,可以添加可选的 silent 参数,其作用与 IF EXISTS 相同。
- SQL
- JSON
- PHP
- Python
- Python-asyncio
- javascript
- Java
- C#
- Rust
DROP TABLE IF EXISTS products;POST /cli -d "DROP TABLE IF EXISTS products"$params =
[
'table' => 'products',
'body' => ['silent' => true]
];
$client->indices()->drop($params);utilsApi.sql('DROP TABLE IF EXISTS products')await utilsApi.sql('DROP TABLE IF EXISTS products')res = await utilsApi.sql('DROP TABLE IF EXISTS products');sqlresult = utilsApi.sql("DROP TABLE IF EXISTS products", true);sqlresult = utilsApi.Sql("DROP TABLE IF EXISTS products", true);let sqlresult = utils_api.sql("DROP TABLE IF EXISTS products", Some(true)).await;{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=""}表可以通过 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=""}