怎么理解PostgreSQL全表扫描问题
发表于:2025-11-07 作者:千家信息网编辑
千家信息网最后更新 2025年11月07日,这篇文章主要讲解了"怎么理解PostgreSQL全表扫描问题",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"怎么理解PostgreSQL全表扫描问题"吧
千家信息网最后更新 2025年11月07日怎么理解PostgreSQL全表扫描问题
这篇文章主要讲解了"怎么理解PostgreSQL全表扫描问题",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"怎么理解PostgreSQL全表扫描问题"吧!
本节内容来源于PGer的一个问题:
Q:
由于多版本的存在,那么全表扫描是不是需要更长的时间了呢?
A:
关于全表扫描,不妨考虑2种极端的情况:
1.insert数据(事务已提交,下同),没有执行update/delete,没有dead tuple,全表扫描效率没有影响;
2.insert数据,执行了大量的update/delete,同时禁用了autovacuum也没有手工执行vacuum,那么存在大量的dead tuple,性能上一是需要更多的IO操作,二是需要执行额外的CPU判断(对于所有的tuple都要执行可见性判断).
其判断逻辑如下:
((Xmin == my-transaction && inserted by the current transaction Cmin < my-command && before this command, and (Xmax is null || the row has not been deleted, or (Xmax == my-transaction && it was deleted by the current transaction Cmax >= my-command))) but not before this command,|| or (Xmin is committed && the row was inserted by a committed transaction, and (Xmax is null || the row has not been deleted, or (Xmax == my-transaction && the row is being deleted by this transaction Cmax >= my-command) || but it's not deleted "yet", or (Xmax != my-transaction && the row was deleted by another transaction Xmax is not committed)))) that has not been committed
简单做个实验,创建一张表t_fts,
1.插入数据,大小为s1,执行全表扫描,时间为m秒;
2.update所有行,大小为s2,执行全表扫描,时间为n秒.
理论上来说,n应为m的s2/s1倍左右(相对于IO时间,如果tuple数不多,CPU时间可以忽略不计).
创建数据表,插入数据:
testdb=# drop table if exists t_fts;DROP TABLEtestdb=# create table t_fts(id int,c1 varchar(200),c2 varchar(200));CREATE TABLEtestdb=# testdb=# insert into t_fts select x,lpad('c1'||x,200,'x'),lpad('c1'||x,200,'x') from generate_series(1,2000000) as x;INSERT 0 2000000testdb=# select pg_size_pretty(pg_table_size('t_fts')); pg_size_pretty ---------------- 868 MB(1 row)禁用autovacuum,执行查询:
testdb=# alter system set autovacuum=off;ALTER SYSTEMtestdb=# show autovacuum; autovacuum ------------ off(1 row)testdb=# explain analyze verbose select * from t_fts; QUERY PLAN ---------------------------------------------------------------------------------------------------------------------------- Seq Scan on public.t_fts (cost=0.00..131112.16 rows=2000016 width=412) (actual time=0.048..1086.289 rows=2000000 loops=1) Output: id, c1, c2 Planning Time: 30.762 ms Execution Time: 1181.360 ms(4 rows)
执行update:
testdb=# update t_fts set c1 = lpad('c1'||(id+1),200,id+1||''),c2 = lpad('c1'||(id+1),200,id+1||'');UPDATE 2000000testdb=# select pg_size_pretty(pg_table_size('t_fts')); pg_size_pretty ---------------- 1737 MB(1 row)testdb=# explain analyze verbose select * from t_fts; QUERY PLAN ------------------------------------------------------------------------------------------------------------------------------- Seq Scan on public.t_fts (cost=0.00..262223.14 rows=4000014 width=412) (actual time=3168.414..6117.780 rows=2000000 loops=1) Output: id, c1, c2 Planning Time: 5.493 ms Execution Time: 6205.705 ms(4 rows)testdb=# explain analyze verbose select * from t_fts; QUERY PLAN ------------------------------------------------------------------------------------------------------------------------------ Seq Scan on public.t_fts (cost=0.00..262223.14 rows=4000014 width=412) (actual time=776.660..2311.270 rows=2000000 loops=1) Output: id, c1, c2 Planning Time: 0.426 ms Execution Time: 2391.895 ms(4 rows)testdb=# explain analyze verbose select * from t_fts; QUERY PLAN ------------------------------------------------------------------------------------------------------------------------------ Seq Scan on public.t_fts (cost=0.00..262223.14 rows=4000014 width=412) (actual time=728.758..2293.157 rows=2000000 loops=1) Output: id, c1, c2 Planning Time: 0.481 ms Execution Time: 2373.241 ms(4 rows)感谢各位的阅读,以上就是"怎么理解PostgreSQL全表扫描问题"的内容了,经过本文的学习后,相信大家对怎么理解PostgreSQL全表扫描问题这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是,小编将为大家推送更多相关知识点的文章,欢迎关注!
问题
数据
时间
内容
学习
大小
情况
更多
事务
同时
就是
思路
性能
手工
效率
数据表
文章
来源
极端
版本
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
新沂市软件开发有限公司
社区网络安全宣传日进校园
在服务器上下载文件夹在哪
查看数据库视图
板报网络安全
石嘴山软件开发哪家服务好
企业软件数据库设计
天一魔兽世界服务器端
闵行区信息化软件开发有哪些
网络安全法律法规ppt
网络安全防护运行维护
启用数据库
苏州信息化网络技术怎么样
软件开发职业前景
我的世界防沉迷怎么进服务器
山东正中计算网络技术
软件数据库软件有哪些问题吗
交换机服务器和路由器的区别
psn原神服务器
流媒体服务器流转发耗时
游戏软件开发是学什么软件
各种小软件开发价格
腾讯微信数据库安全不
数据库的脱敏原则不包括
软件开发最小规模团队
中国没有ipv4根服务器
一个淘淘商城软件开发多久
河南采购管理软件开发
互联网 科技英语
db2远程备份数据库