網站首頁 編程語言 正文
我們都知道在PostgreSQL中使用索引掃描時,是通過索引中存儲的ctid去表中得到數據的。同時在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)
可以看到沒有再去使用index only scan,取而代之的是普通的索引掃描。
為什么會這樣呢?ctid必然是包含在任何btree索引中的,為什么用到ctid的時候就不能用index only scan?
在網上看到類似的問題:
傳送門
解答是說和HOT有關,乍一看似乎有點道理,但是仔細想想,如果是HOT那么也會通過vm文件去判斷多版本,那么對于ctid我們只要通過vm文件判斷其可見性不是就可以了,至少當表中沒有任何不可見的行時應該要使用index only scan啊。
這其實因為在使用vm文件進行可見性判斷前,優化器在parse階段就已經決定了是使用index scan還是index only scan,通過check_index_only函數來判斷是否使用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的信息。
原文鏈接:https://foucus.blog.csdn.net/article/details/122069198
相關推薦
- 2021-12-09 Typora自動編號的具體操作_其它綜合
- 2023-03-20 C#如何判斷.Net?Framework版本是否滿足軟件運行需要的版本_C#教程
- 2023-03-16 Python?asyncio異步編程簡單實現示例_python
- 2023-04-07 React?Fiber構建源碼解析_React
- 2022-05-11 Spring數據源及注解開發
- 2022-04-20 Django學習之路之請求與響應_python
- 2023-01-20 Python如何求取逆序數_python
- 2022-08-31 Python無法用requests獲取網頁源碼的解決方法_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支