对PostgreSQL中 index only scan 的初步理解

本文涉及的产品
云原生数据库 PolarDB PostgreSQL 版,标准版 2核4GB 50GB
云原生数据库 PolarDB MySQL 版,通用型 2核8GB 50GB
简介:

开始

所谓index only scan ,就是因为 建立 index时,所包含的字段集合,囊括了我们 查询语句中的字段,这样,提取出相应的 index ,就不必再次提取数据块了。

例子:

复制代码
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 的影响,重新执行 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: 1
 Total runtime: 47.390 ms
(4 rows)

postgres=# 
复制代码

再看看查询中有 index 不包含的字段的情况:

[作者:技术者高健@博客园  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: 1
 Total runtime: 0.006 ms
(4 rows)

postgres=# 
复制代码

学习






本文转自健哥的数据花园博客园博客,原文链接:http://www.cnblogs.com/gaojian/archive/2012/11/07/2759061.html,如需转载请自行联系原作者

相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
目录
相关文章
|
1月前
|
存储 关系型数据库 MySQL
MySQL中为什么要使用索引合并(Index Merge)?
通过这些内容的详细介绍和实际案例分析,希望能帮助您深入理解索引合并及其在MySQL中的
130 10
|
7月前
|
关系型数据库 MySQL 测试技术
MySQL 报错 ERROR 1709: Index column size too large
MySQL 报错 ERROR 1709: Index column size too large
310 4
|
8月前
|
缓存 关系型数据库 MySQL
MySQL数据库——InnoDB引擎-架构-内存结构(Buffer Pool、Change Buffer、Adaptive Hash Index、Log Buffer)
MySQL数据库——InnoDB引擎-架构-内存结构(Buffer Pool、Change Buffer、Adaptive Hash Index、Log Buffer)
136 3
|
9月前
|
存储 关系型数据库 MySQL
MySQL的优化利器⭐️Multi Range Read与Covering Index是如何优化回表的?
本文以小白的视角使用通俗易懂的流程图深入浅出分析Multi Range Read与Covering Index是如何优化回表
|
9月前
|
SQL 关系型数据库 MySQL
mysql查询语句的访问方法const、ref、ref_or_null、range、index、all
mysql查询语句的访问方法const、ref、ref_or_null、range、index、all
|
9月前
|
SQL 关系型数据库 MySQL
923.【mysql】 only full group by 模式
923.【mysql】 only full group by 模式
302 1
|
9月前
|
关系型数据库 MySQL 索引
mysql中force index强制索引
mysql中force index强制索引
126 0
|
9月前
|
关系型数据库 MySQL
mysql 5.5.62版本建表语句报错: Index column size too large. The maximum column size is 767 bytes
mysql 5.5.62版本建表语句报错: Index column size too large. The maximum column size is 767 bytes
386 0
|
9月前
|
关系型数据库 MySQL
MySQL【问题 02】报错 1709 - Index column size too large. The maximum column size is 767 bytes. 可能是最简单的方法
MySQL【问题 02】报错 1709 - Index column size too large. The maximum column size is 767 bytes. 可能是最简单的方法
277 0
|
关系型数据库 MySQL 数据库
Mysql中key与index区别
Mysql中key与index区别