111 发表于 2016-11-22 00:17:02

对PostgreSQL的 ctid 的初步认识

  开始
  ctid 和 物理存储有关,指的是 一条记录位于哪个数据块的哪个位移上面。



postgres=# select ctid, * from gaotab;
ctid   | id|name| deptno | age
---------+-----+--------+--------+-----
(0,1)   |   1 | gao    |   10 |30
(0,2)   |   2 | jian   |   11 |35
(0,3)   |   3 | tom    |   11 |30
(0,4)   |   4 | nam04|   12 |25
(0,5)   |   5 | nam05|   13 |40
(0,6)   |   6 | nam06|   12 |32
(0,7)   |   7 | nam07|   13 |30
(0,8)   |   8 | nam08|   14 |35
(0,9)   |   9 | nam09|   14 |30
(0,10)|10 | nam10|   14 |25
(0,11)|11 | nam11|   12 |40
(0,12)|12 | nam12|   13 |32
(0,13)|13 | nam13|   12 |30
(0,14)|14 | nam14|   13 |35
(0,15)|15 | nam15|   14 |30
(0,16)|16 | nam16|   14 |25
--More--
  执行计划
  [作者:技术者高健@博客园mail: luckyjackgao@gmail.com ]



postgres=# explain select * from gaotab where ctid='(0,10)';
QUERY PLAN                     
-------------------------------------------------------
Seq Scan on gaotab(cost=0.00..2.25 rows=1 width=17)
Filter: (ctid = '(0,10)'::tid)
(2 rows)
postgres=#


postgres=# explain select oid from pg_proc where ctid='(0,1)';
QUERY PLAN                     
-------------------------------------------------------
Tid Scan on pg_proc(cost=0.00..4.01 rows=1 width=4)
TID Cond: (ctid = '(0,1)'::tid)
(2 rows)
  虽然据说涉及到 ctid 的时候, Tid Scan 因为物理上的原因,可能会很快。但是有的时候还是没有 sequential 来的快。



postgres=#
postgres=# explain select name from gaotab where ctid='(0,1)';
QUERY PLAN                     
------------------------------------------------------
Tid Scan on gaotab(cost=0.00..4.01 rows=1 width=5)
TID Cond: (ctid = '(0,1)'::tid)
(2 rows)
postgres=# set session enable_seqscan=true;
SET
postgres=# explain select name from gaotab where ctid='(0,1)';
QUERY PLAN                     
------------------------------------------------------
Seq Scan on gaotab(cost=0.00..2.25 rows=1 width=5)
Filter: (ctid = '(0,1)'::tid)
(2 rows)
postgres=#
  [作者:技术者高健@博客园mail: luckyjackgao@gmail.com ]
  结束
页: [1]
查看完整版本: 对PostgreSQL的 ctid 的初步认识