日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

对PostgreSQL中 index only scan 的初步理解

發(fā)布時間:2025/3/21 数据库 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 对PostgreSQL中 index only scan 的初步理解 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

開始

所謂index only scan ,就是因為 建立 index時,所包含的字段集合,囊括了我們 查詢語句中的字段,這樣,提取出相應(yīng)的 index ,就不必再次提取數(shù)據(jù)塊了。

例子:

postgres=# \d gaotab;Table "public.gaotab"Column | Type | Modifiers --------+-----------------------+-----------id | integer | name | character varying(20) | deptno | integer | age | integer | postgres=# create index idx_id_dept on gaotab(id,deptno); CREATE INDEX postgres=# analyze gaotab; ANALYZE postgres=#postgres=# explain select id,deptno from gaotab where id=200;QUERY PLAN -------------------------------------------------------------------------------Index Only Scan using idx_id_dept on gaotab (cost=0.00..8.27 rows=1 width=8)Index Cond: (id = 200) (2 rows)

為了抵消cache 的影響,重新執(zhí)行 explain analyze

postgres=# explain analyze select id,deptno from gaotab where id=200;QUERY PLAN -------------------------------------------------------------------------------- -------------------------------------------Index Only Scan using idx_id_dept on gaotab (cost=0.00..8.27 rows=1 width=8) ( actual time=30.912..30.915 rows=1 loops=1)Index Cond: (id = 200)Heap Fetches: 1Total runtime: 47.390 ms (4 rows)postgres=#

再看看查詢中有 index 不包含的字段的情況:

[作者:技術(shù)者高健@博客園 ?mail:?luckyjackgao@gmail.com?]

postgres=# explain select id,name from gaotab where id=200;QUERY PLAN ---------------------------------------------------------------------------Index Scan using idx_id_dept on gaotab (cost=0.00..8.27 rows=1 width=10)Index Cond: (id = 200) (2 rows)postgres=# explain analyze select id,name from gaotab where id=200;QUERY PLAN -------------------------------------------------------------------------------- ---------------------------------------Index Scan using idx_id_dept on gaotab (cost=0.00..8.27 rows=1 width=10) (actu al time=47.043..47.044 rows=1 loops=1)Index Cond: (id = 200)Total runtime: 63.506 ms (3 rows)postgres=#

在這里,我們必須要注意的一點是:

如果是那種 帶 where 條件的,如果 前面用了 explain ,后面又對同一條語句用 explain analyze 的話,就會受到緩存的影響。

這樣就不夠準確了。

例如:

postgres=# explain select id,deptno from gaotab where id=200;QUERY PLAN -------------------------------------------------------------------------------Index Only Scan using idx_id_dept on gaotab (cost=0.00..8.27 rows=1 width=8)Index Cond: (id = 200) (2 rows)postgres=# explain analyze select id,deptno from gaotab where id=200;QUERY PLAN -------------------------------------------------------------------------------- -----------------------------------------Index Only Scan using idx_id_dept on gaotab (cost=0.00..8.27 rows=1 width=8) ( actual time=0.000..0.000 rows=1 loops=1)Index Cond: (id = 200)Heap Fetches: 1Total runtime: 0.006 ms (4 rows)postgres=#

學(xué)習(xí)

轉(zhuǎn)載于:https://www.cnblogs.com/gaojian/archive/2012/11/07/2759061.html

總結(jié)

以上是生活随笔為你收集整理的对PostgreSQL中 index only scan 的初步理解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。