PostgreSQL索引掃描時為什么index only scan不返回ctid
我們都知道在PostgreSQL中使用索引掃描時,是通過索引中存儲的ctid去表中得到數(shù)據(jù)的。同時在PostgreSQL中如果要查詢的列都在索引中,我們還可以使用index only scan。
既然如此,當我們在查詢中用到ctid時,是否還能使用index only scan呢?
按理來說是沒有問題的,例如在Oracle中:
SQL> select rowid,id from t1 where id = 1; --------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | --------------------------------------------------------------------------- | 0 | SELECT STATEMENT | | 1 | 25 | 1 (0)| 00:00:01 | |* 1 | INDEX RANGE SCAN| IDX_T1 | 1 | 25 | 1 (0)| 00:00:01 | ---------------------------------------------------------------------------
我們的查詢包含了rowid,仍然不需要回表TABLE ACCESS BY INDEX ROWID BATCHED的步驟。但是在PostgreSQL似乎并不是這樣。
index only scan:
bill=# explain analyze select c1 from t1 where c1 = 10; QUERY PLAN --------------------------------------------------------------------------------------------------------------------- Index Only Scan using idx_t1 on t1 (cost=0.29..10.74 rows=523 width=4) (actual time=0.021..0.117 rows=523 loops=1) Index Cond: (c1 = 10) Heap Fetches: 0 Planning Time: 0.076 ms Execution Time: 0.196 ms (5 rows)
帶上ctid后:
bill=# explain analyze select ctid,c1 from t1 where c1 = 10; QUERY PLAN ----------------------------------------------------------------------------------------------------------------- Index Scan using idx_t1 on t1 (cost=0.29..81.71 rows=523 width=10) (actual time=0.038..0.447 rows=523 loops=1) Index Cond: (c1 = 10) Planning Time: 0.098 ms Execution Time: 0.537 ms (4 rows)
可以看到?jīng)]有再去使用index only scan,取而代之的是普通的索引掃描。
為什么會這樣呢?ctid必然是包含在任何btree索引中的,為什么用到ctid的時候就不能用index only scan?
在網(wǎng)上看到類似的問題:
解答是說和HOT有關,乍一看似乎有點道理,但是仔細想想,如果是HOT那么也會通過vm文件去判斷多版本,那么對于ctid我們只要通過vm文件判斷其可見性不是就可以了,至少當表中沒有任何不可見的行時應該要使用index only scan啊。
這其實因為在使用vm文件進行可見性判斷前,優(yōu)化器在parse階段就已經(jīng)決定了是使用index scan還是index only scan,通過check_index_only函數(shù)來判斷是否使用index only scan:
for (i = 0; i < index->ncolumns; i++) { int attno = index->indexkeys[i]; /* * For the moment, we just ignore index expressions. It might be nice * to do something with them, later. */ if (attno == 0) continue; if (index->canreturn[i]) index_canreturn_attrs = bms_add_member(index_canreturn_attrs, attno - FirstLowInvalidHeapAttributeNumber); else index_cannotreturn_attrs = bms_add_member(index_cannotreturn_attrs, attno - FirstLowInvalidHeapAttributeNumber); } index_canreturn_attrs = bms_del_members(index_canreturn_attrs, index_cannotreturn_attrs); /* Do we have all the necessary attributes? */ result = bms_is_subset(attrs_used, index_canreturn_attrs);
簡單解釋下上面這段代碼的邏輯,pg在判斷是否使用index only scan時,就是將索引列取出放到一個bitmap位圖index_canreturn_attrs中,將查詢用到的列放到一個bitmap位圖attrs_used中,然后判斷attrs_used位圖是否是index_canreturn_attrs的子集,如果是則使用index only scan,而這里的index_canreturn_attrs信息是從pg_index中去獲取的,自然是不會存放ctid的信息。
到此這篇關于PostgreSQL索引掃描時為什么index only scan不返回ctid的文章就介紹到這了,更多相關PostgreSQL index only scan內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Postgresql數(shù)據(jù)庫之創(chuàng)建和修改序列的操作
這篇文章主要介紹了Postgresql數(shù)據(jù)庫之創(chuàng)建和修改序列的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02PostgreSQL Public 模式的風險及安全遷移問題小結(jié)
本文主要討論了PostgreSQL中public模式的問題和解決方案,public模式默認對所有用戶開放訪問權(quán)限,容易發(fā)生命名沖突,且難以維護和隔離,修改或刪除它可能導致擴展無法正常工作,為解決這問題,建議新建模式,將public模式下的所有業(yè)務對象遷移過去2024-10-10PostgreSQL教程(十三):數(shù)據(jù)庫管理詳解
這篇文章主要介紹了PostgreSQL教程(十三):數(shù)據(jù)庫管理詳解,本文講解了概述、創(chuàng)建數(shù)據(jù)庫、修改數(shù)據(jù)庫配置、刪除數(shù)據(jù)庫、表空間,需要的朋友可以參考下2015-05-05PostgreSQL教程(四):數(shù)據(jù)類型詳解
這篇文章主要介紹了PostgreSQL教程(四):數(shù)據(jù)類型詳解,本文講解了數(shù)值類型、字符類型、布爾類型、位串類型、數(shù)組、復合類型等數(shù)據(jù)類型,需要的朋友可以參考下2015-05-05postgres 實現(xiàn)查詢某條數(shù)據(jù)的排名
這篇文章主要介紹了postgres 實現(xiàn)查詢某條數(shù)據(jù)的排名,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12PostgreSQL將數(shù)據(jù)加載到buffer cache中操作方法
這篇文章主要介紹了PostgreSQL將數(shù)據(jù)加載到buffer cache中,我們可以使用pg_prewarm插件來將指定的表加載到OS Buffer或者pg shared buffer中,具體操作方法跟隨小編一起看看吧2021-04-04