delete
delete 语句用于删除表中的记录。
delete 语句不仅可用于内存表,也可用于 DFS 表(分布式表与维度表)。请注意,对分布式表使用 delete 语句时,系统把要删除记录所在的分区整体删除后更新;对维度表使用 delete 语句时,系统将该表删除后更新。因此对 DFS 表使用 delete 语句仅适用于低频删除任务,例如分钟级删除任务,不适用于高频删除任务,例如毫秒级删除任务。
删除采取了多版本的方式,并且支持事务。系统会创建一个新的版本以存储新的数据。提交事务之前,其他 SQL 语句仍然访问旧版本的数据。若删除涉及多个分区,只要其中某一个分区删除失败,系统会回滚所有分区的修改。
2.00.1版本及以上,delete 支持 map 子句,即支持将 delete 语句在每个分区内分别执行。若 where 子句中使用结果与次序有关的函数,如: isDuplicated, first, firstNot 等,且涉及到多个分区,则必须使用 map 关键字。
语法
delete from table_name
where condition(s);
如果没有使用 where 条件,删除表中所有记录。
例子
例 1:对内存表进行删除操作
t = table(1 1 1 2 2 2 3 3 3 3 as id, 1..10 as x);
t;
id |
x |
---|---|
1 |
1 |
1 |
2 |
1 |
3 |
2 |
4 |
2 |
5 |
2 |
6 |
3 |
7 |
3 |
8 |
3 |
9 |
3 |
10 |
delete from t where id=1;
t;
id |
x |
---|---|
2 |
4 |
2 |
5 |
2 |
6 |
3 |
7 |
3 |
8 |
3 |
9 |
3 |
10 |
delete from t where id=3, x>8;
t;
id |
x |
---|---|
2 |
4 |
2 |
5 |
2 |
6 |
3 |
7 |
3 |
8 |
delete from t;
t;
id |
x |
---|---|
例 2. 对分布式表进行删除操作
$ login(`admin, `123456)
$ n=1000000
$ ID=rand(10, n)
$ x=rand(1.0, n)
$ t=table(ID, x)
$ db=database("dfs://rangedb124", RANGE, 0 5 10)
$ pt=db.createPartitionedTable(t, `pt, `ID)
$ pt.append!(t)
$ select count(*) from pt;
1000000
$ delete from pt where ID=5;
$ select count(*) from pt;
例 3. 删除分布式表数据,且 where 过滤条件与数据行的次序有关,必须使用 map 子句,在分区内分别执行。
$ n=10000
$ ID=take(0..4, n)
$ date=take(2017.08.07..2017.11.11, n)
$ ts=rand(timestamp(1..n),n)
$ int=take(1..50,n)
$ str=rand(string(1..n),n)
$ sym=rand(symbol(string(1..n)),n)
$ x=rand(10.0, n)
$ t=table(ID, date,ts,int,str,sym, x)
$ if(existsDatabase("dfs://compoDB")){
$ dropDatabase("dfs://compoDB")
$ }
$ db = database("dfs://compoDB", VALUE,2017.08M..2017.10M)
$ pt = db.createPartitionedTable(t, `pt, `date)
$ pt.append!(t);
$ delete from pt where isDuplicated([ID,int])=false map;