KingbaseES例程之快速删除表数据
概述
快速删除表中的数据
delete语句删除数据
表中的数据被删除了,但是这个数据在硬盘上的真实存储空间不会被释放。
这种删除缺点是:删除效率比较低。
这种删除优点是:支持删除部分数据,支持回滚。truncate语句删除数据
这种删除效率比较高,表被一次截断,物理删除。
这种删除缺点:不支持删除部分数据。
这种删除优点:快速,支持回滚。
案例:删除大表数据,但保留少量数据
一张表有100万条数据,分为1000组信息,仅保留每组的最后一条数据,如何快速删除其它99万余条数据?
方法一:删除每组非最大值的数据
explain (analyse,buffers )
delete
from test10
where (c1,id) not in (select c1,max(id) from test10 group by c1)
returning *;
Delete on test10 (cost=36508.94..56943.94 rows=500000 width=6) (actual time=221.183..1732.834 rows=998999 loops=1)
Buffers: shared hit=2012980
-> Seq Scan on test10 (cost=36508.94..56943.94 rows=500000 width=6) (actual time=221.128..583.449 rows=998999 loops=1)
Filter: (NOT (hashed SubPlan 1))
Rows Removed by Filter: 1001
Buffers: shared hit=9547
SubPlan 1
-> GroupAggregate (cost=0.42..36506.44 rows=1001 width=8) (actual time=0.067..219.780 rows=1001 loops=1)
Group Key: test10_1.c1
Buffers: shared hit=4112
-> Index Only Scan using idx01 on test10 test10_1 (cost=0.42..31496.42 rows=1000000 width=8) (actual time=0.010..126.628 rows=1000000 loops=1)
Heap Fetches: 0
Buffers: shared hit=4112
Planning Time: 0.120 ms
Execution Time: 1799.063 ms
方法二:CTE获取每组最新行,删除每组非CTE的数据
explain (analyse,buffers )
with recursive cte as (
(select c1, ctid from test10 order by c1, id desc limit 1)
union all
(select test10.c1, test10.CTID
from cte,
lateral ( select CTID, c1
from test10
where cte.c1 < test10.c1
order by test10.c1, test10.id desc
limit 1) test10
))
delete from test10
where not exists (select 1 from cte where cte.ctid = test10.ctid )
returning *
;
Delete on test10 (cost=62.30..28121.41 rows=999899 width=36) (actual time=10.799..1627.548 rows=998999 loops=1)
Buffers: shared hit=2013025
CTE cte
-> Recursive Union (cost=0.42..59.02 rows=101 width=10) (actual time=0.012..9.888 rows=1001 loops=1)
Buffers: shared hit=4157
" -> Subquery Scan on ""*SELECT* 1"" (cost=0.42..0.49 rows=1 width=10) (actual time=0.010..0.013 rows=1 loops=1)"
Buffers: shared hit=4
-> Limit (cost=0.42..0.48 rows=1 width=14) (actual time=0.010..0.011 rows=1 loops=1)
Buffers: shared hit=4
-> Index Scan using idx02 on test10 test10_1 (cost=0.42..54240.28 rows=1000000 width=14) (actual time=0.010..0.010 rows=1 loops=1)
Buffers: shared hit=4
-> Nested Loop (cost=0.42..5.65 rows=10 width=10) (actual time=0.009..0.009 rows=1 loops=1001)
Buffers: shared hit=4153
-> WorkTable Scan on cte cte_1 (cost=0.00..0.20 rows=10 width=4) (actual time=0.000..0.000 rows=1 loops=1001)
-> Limit (cost=0.42..0.53 rows=1 width=14) (actual time=0.009..0.009 rows=1 loops=1001)
Buffers: shared hit=4153
-> Index Scan using idx02 on test10 test10_2 (cost=0.42..33409.58 rows=333333 width=14) (actual time=0.009..0.009 rows=1 loops=1001)
Index Cond: (c1 > cte_1.c1)
Buffers: shared hit=4153
-> Hash Anti Join (cost=3.28..28062.39 rows=999899 width=36) (actual time=10.727..422.146 rows=998999 loops=1)
Hash Cond: (test10.ctid = cte.ctid)
Buffers: shared hit=9592
-> Seq Scan on test10 (cost=0.00..15435.00 rows=1000000 width=6) (actual time=0.005..141.828 rows=1000000 loops=1)
Buffers: shared hit=5435
-> Hash (cost=2.02..2.02 rows=101 width=36) (actual time=10.713..10.714 rows=1001 loops=1)
Buckets: 1024 Batches: 1 Memory Usage: 77kB
Buffers: shared hit=4157
-> CTE Scan on cte (cost=0.00..2.02 rows=101 width=36) (actual time=0.049..10.400 rows=1001 loops=1)
Buffers: shared hit=4157
Planning Time: 0.201 ms
Execution Time: 1691.687 ms
方法三:数组变量与truncate组合,支持事务回滚
do
$$
declare
v_rec test10[];
begin
v_rec := array(
with recursive cte as (
(select id, c1, c2 from test10 order by c1, id desc limit 1)
union all
(select test10.id, test10.c1, test10.c2
from cte,
lateral ( select test10.id, test10.c1, test10.c2
from test10
where cte.c1 < test10.c1
order by test10.c1, test10.id desc
limit 1) test10
))
select (id, c1, c2)
from cte);
truncate test10;
insert into test10
select (t).*
from (select unnest(v_rec) t) t;
commit;
exception
when others then
rollback;
end;
$$
;
ANONYMOUS BLOCK
Time: 99.299 ms
TRUNCATE与DML操作的组合,实现通过少量数据的DML操作,实现DELETE大部分数据操作,可以减少执行时长。由于truncate支持事务回滚,可以在发生异常时回滚事务,或主动回滚事务,保证数据的完整性。
KingbaseES例程之快速删除表数据的更多相关文章
- oracle 快速删除大批量数据方法(全部删除,条件删除,删除大量重复记录)
oracle 快速删除大批量数据方法(全部删除,条件删除,删除大量重复记录) 分类: ORACLE 数据库 2011-05-24 16:39 8427人阅读 评论(2) 收藏 举报 oracledel ...
- oracle 快速备份表数据
oracle 快速备份表数据 CreateTime--2018年2月28日17:04:50 Author:Marydon UpdateTime--2017年1月20日11:45:07 1.1.9. ...
- sql语句中----删除表数据drop、truncate和delete的用法
sql语句中----删除表数据drop.truncate和delete的用法 --drop drop table tb --tb表示数据表的名字,下同 删除内容和定义,释放空间.简单来说就是把整 ...
- sql语句中----删除表数据的"三兄弟"
说到删除表数据的关键字,大家记得最多的可能就是delete了 然而我们做数据库开发,读取数据库数据.对另外的两兄弟用得就比较少了 现在来介绍另外两个兄弟,都是删除表数据的,其实也是很容易理解的 老大- ...
- 删除表数据drop、truncate和delete的用法
说到删除表数据的关键字,大家记得最多的可能就是delete了 然而我们做数据库开发,读取数据库数据.对另外的两兄弟用得就比较少了 现在来介绍另外两个兄弟,都是删除表数据的,其实也是很容易理解的 老大- ...
- SQLite Expert 删除表数据并重置自动增长列
用下面的语句肯定是行不通的,语句不支持 truncate table t_Records 方法:1.删除表数据 2.重置自动增长列 where name='t_Records' /*name :是表名 ...
- sql有几种删除表数据的方式
有几种删除表数据的方式? truncate.delete和drop都可以删除数据. TRUNCATE TABLE删除表中的所有行,而不记录单个行删除操作. TRUNCATE TABLE 与没有 WHE ...
- mysql进阶(二十一)删除表数据
MySQL删除表数据 在MySQL中有两种方法可以删除数据,一种是DELETE语句,另一种是TRUNCATE TABLE语句.DELETE语句可以通过WHERE对要删除的记录进行选择.而使用TRUNC ...
- 数据库之删除表数据drop、truncate和delete的用法
数据库中删除表数据的关键字,最常用的可能就是delete了,另外其实还有drop和truncate两个关键字. 老大:drop 命令格式:drop table tb ---tb表示数据表的名字,下 ...
随机推荐
- RPA应用场景-财务报表统计整合
场景概述 财务报表统计整合 所涉系统名称 邮储银行系统 人工操作(时间/次) 3小时 所涉人工数量 1 操作频率 每月 场景流程 1.登录各个区支行系统 2.机器人按照要求,自动复选多项业务参数,导出 ...
- RPA应用场景-银行回单查询
场景概述银行回单查询 所涉系统名称银行网银 人工操作(时间/次) 5 分钟 所涉人工数量 4 操作频率不定时 场景流程 1.收到外派业务员申请查询收入银行回单的邮件: 2.依据邮件中提供的客户信息进入 ...
- 解决github.com 的响应时间过长以及hosts配置不能保存的问题
github.com 的响应时间过长 1 获取github可以使用的DNS域名 DNS查询 选择TTL值最小的 2 修改hosts配置 打开之后在最后加上如下内容,保存即可 3 出现hosts不能保存 ...
- 扩展新的WCV到标准的WC后,不能在业务角色里面看见视图解决方法
by zyi 感谢群主红枣的分享 1.把你的WCVIEW扩展进WC中 2.使用UI Designer打开你的WCVIEW 3.更改你的WCVIEW名字
- 【UR #2】猪猪侠再战括号序列 题解
题目链接 前言 是的没脑子选手只会做签到题. 思路分析 一开始把题目看成反转括号的状态,直接浪费 \(40\ mins\) . 我们考虑把不确定的"正确括号"转换成一个固定的括号序 ...
- 集合容器和Hash表
集合容器 集合相当于一个容器,在我们使用Arraylist的时候添加参数相当与放了一个容器中.这里面的元素是可以重复的 在HashSet中添加元素是没有重复的,我们来写一个测试看一下 public s ...
- C++基本数据类型范围和区别(详细)
一.基本类型的大小及范围的总结(以下所讲都是默认在32位操作系统下):字节:byte:位:bit.1.短整型short:所占内存大小:2byte=16bit:所能表示范围:-32768~32767:( ...
- atcoder ABC 232 B~E题解
B 模拟,水题 #include<bits/stdc++.h> using namespace std; char s1[100005],s2[100005]; int a1[100005 ...
- 在Centos7.3下使用Siege对Django服务进行压力测试
原文转载自「刘悦的技术博客」https://v3u.cn/a_id_87 Siege是linux下的一个web系统的压力测试工具,支持多链接,支持get和post请求,可以对web系统进行多并发下持续 ...
- 基于Python3(Autosub)以及Ffmpeg配合GoogleTranslation(谷歌)为你的影片实现双语版字幕(逐字稿)
原文转载自「刘悦的技术博客」https://v3u.cn/a_id_169 为影片加字幕其实是一件非常耗费时间的事情,尤其是对于打字慢的朋友来说.当然不光为影片加字幕,在其他领域,类似的逐字稿也是工作 ...