PostgreSQL索引掃描時(shí)為什么index only scan不返回ctid
我們都知道在PostgreSQL中使用索引掃描時(shí),是通過(guò)索引中存儲(chǔ)的ctid去表中得到數(shù)據(jù)的。同時(shí)在PostgreSQL中如果要查詢的列都在索引中,我們還可以使用index only scan。
既然如此,當(dāng)我們?cè)诓樵冎杏玫絚tid時(shí),是否還能使用index only scan呢?
按理來(lái)說(shuō)是沒(méi)有問(wèn)題的,例如在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,取而代之的是普通的索引掃描。
為什么會(huì)這樣呢?ctid必然是包含在任何btree索引中的,為什么用到ctid的時(shí)候就不能用index only scan?
在網(wǎng)上看到類似的問(wèn)題:
解答是說(shuō)和HOT有關(guān),乍一看似乎有點(diǎn)道理,但是仔細(xì)想想,如果是HOT那么也會(huì)通過(guò)vm文件去判斷多版本,那么對(duì)于ctid我們只要通過(guò)vm文件判斷其可見(jiàn)性不是就可以了,至少當(dāng)表中沒(méi)有任何不可見(jiàn)的行時(shí)應(yīng)該要使用index only scan啊。
這其實(shí)因?yàn)樵谑褂胿m文件進(jìn)行可見(jiàn)性判斷前,優(yōu)化器在parse階段就已經(jīng)決定了是使用index scan還是index only scan,通過(guò)check_index_only函數(shù)來(lái)判斷是否使用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);
簡(jiǎn)單解釋下上面這段代碼的邏輯,pg在判斷是否使用index only scan時(shí),就是將索引列取出放到一個(gè)bitmap位圖index_canreturn_attrs中,將查詢用到的列放到一個(gè)bitmap位圖attrs_used中,然后判斷attrs_used位圖是否是index_canreturn_attrs的子集,如果是則使用index only scan,而這里的index_canreturn_attrs信息是從pg_index中去獲取的,自然是不會(huì)存放ctid的信息。
到此這篇關(guān)于PostgreSQL索引掃描時(shí)為什么index only scan不返回ctid的文章就介紹到這了,更多相關(guān)PostgreSQL index only scan內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
PostgreSQL使用MySQL外表的步驟詳解(mysql_fdw)
這篇文章主要介紹了PostgreSQL使用MySQL外表的步驟(mysql_fdw),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01
PostgreSQL中如何將對(duì)象oid和對(duì)象名相互轉(zhuǎn)換
文章介紹了在PostgreSQL中如何使用內(nèi)部數(shù)據(jù)類型將對(duì)象OID(對(duì)象標(biāo)識(shí)符)轉(zhuǎn)換為對(duì)象名,從而簡(jiǎn)化系統(tǒng)視圖的關(guān)聯(lián)查詢,文章還提供了不同類型OID與對(duì)象名之間的轉(zhuǎn)換關(guān)系,并通過(guò)示例展示了如何將對(duì)象名轉(zhuǎn)換為OID2024-11-11
Linux下創(chuàng)建Postgresql數(shù)據(jù)庫(kù)的方法步驟
PostgreSQL 是一種非常復(fù)雜的對(duì)象-關(guān)系型數(shù)據(jù)庫(kù)管理系統(tǒng)(ORDBMS),也是目前功能最強(qiáng)大,特性最豐富和最復(fù)雜的自由軟件數(shù)據(jù)庫(kù)系統(tǒng)。下面這篇文章主要給大家介紹了關(guān)于在Linux下創(chuàng)建Postgresql數(shù)據(jù)庫(kù)的方法步驟,需要的朋友可以參考,下面來(lái)一起看看吧。2017-07-07
對(duì)PostgreSQL中的慢查詢進(jìn)行分析和優(yōu)化的操作指南
在數(shù)據(jù)庫(kù)的世界里,慢查詢就像是路上的絆腳石,讓數(shù)據(jù)處理的道路變得崎嶇不平,想象一下,你正在高速公路上飛馳,突然遇到一堆減速帶,那感覺(jué)肯定糟透了,本文介紹了怎樣對(duì)?PostgreSQL?中的慢查詢進(jìn)行分析和優(yōu)化,需要的朋友可以參考下2024-07-07
PostgreSQL 查找當(dāng)前數(shù)據(jù)庫(kù)的所有表操作
這篇文章主要介紹了PostgreSQL 查找當(dāng)前數(shù)據(jù)庫(kù)的所有表操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-12-12
Linux 上 定時(shí)備份postgresql 數(shù)據(jù)庫(kù)的方法
這篇文章主要介紹了Linux 上 定時(shí)備份postgresql 數(shù)據(jù)庫(kù)的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02

