对PostgreSQL中 index only scan 的初步理解
生活随笔
收集整理的這篇文章主要介紹了
对PostgreSQL中 index only scan 的初步理解
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
開(kāi)始
所謂index only scan ,就是因?yàn)?建立 index時(shí),所包含的字段集合,囊括了我們 查詢語(yǔ)句中的字段,這樣,提取出相應(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=#在這里,我們必須要注意的一點(diǎn)是:
如果是那種 帶 where 條件的,如果 前面用了 explain ,后面又對(duì)同一條語(yǔ)句用 explain analyze 的話,就會(huì)受到緩存的影響。
這樣就不夠準(zhǔn)確了。
例如:
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 的初步理解的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Javascript获取url参数值
- 下一篇: C#学习☞接口