網(wǎng)站首頁 編程語言 正文
我們都知道在PostgreSQL中使用索引掃描時(shí),是通過索引中存儲的ctid去表中得到數(shù)據(jù)的。同時(shí)在PostgreSQL中如果要查詢的列都在索引中,我們還可以使用index only scan。
既然如此,當(dāng)我們在查詢中用到ctid時(shí),是否還能使用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,取而代之的是普通的索引掃描。
為什么會(huì)這樣呢?ctid必然是包含在任何btree索引中的,為什么用到ctid的時(shí)候就不能用index only scan?
在網(wǎng)上看到類似的問題:
傳送門
解答是說和HOT有關(guān),乍一看似乎有點(diǎn)道理,但是仔細(xì)想想,如果是HOT那么也會(huì)通過vm文件去判斷多版本,那么對于ctid我們只要通過vm文件判斷其可見性不是就可以了,至少當(dāng)表中沒有任何不可見的行時(shí)應(yīng)該要使用index only scan啊。
這其實(shí)因?yàn)樵谑褂胿m文件進(jìn)行可見性判斷前,優(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時(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的信息。
原文鏈接:https://foucus.blog.csdn.net/article/details/122069198
相關(guān)推薦
- 2022-05-14 C++?STL中vector容器的使用_C 語言
- 2022-09-25 什么是模板引擎(web)?常見的模板引擎有哪些?thymeleaf的常用指令介紹
- 2023-01-29 switch多選擇結(jié)構(gòu)、循環(huán)結(jié)構(gòu)示例詳解_Swift
- 2022-12-21 PyGame實(shí)現(xiàn)初始化導(dǎo)入所有模塊方法詳解_python
- 2022-11-15 超詳細(xì)解析C++實(shí)現(xiàn)歸并排序算法_C 語言
- 2022-04-10 C#實(shí)現(xiàn)泛型動(dòng)態(tài)循環(huán)數(shù)組隊(duì)列的方法_C#教程
- 2022-05-10 原生ajax 在服務(wù)器響應(yīng)前撤銷請求
- 2022-04-17 Python數(shù)據(jù)預(yù)處理常用的5個(gè)技巧_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支