對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 ] 

結束


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM