生成数据

create table test(id1 int, id2 int, id3 int);create index id1_idx on test using btree (id1);create index id2_idx on test using btree (id2);insert into test select t,t ,t from generate_series(10000000, 0, -1) as t;insert into test select 10000001, 10000001 , (random()*100000)::int from generate_series(1, 5000);analyze test;

测试SQL

explain analyze  select min(id1) from test where id2 = 10000001;

上面我们在列id1和id2分别创建了索引,我们的猜想是优化器会在id1_idx和id2_idx上选择一个最优的执行计划,But.... 请看下面

现象

下面是数据实际的执行计划,可以看到使用了id1_idx索引,然而执行时间很长

postgres=> explain analyze  select min(id1) from test where id2 = 10000001;QUERY PLAN
------------------------------------------------------------------------------------------------------------------------------------------
Result  (cost=103.28..103.29 rows=1 width=0) (actual time=9860.209..9860.209 rows=1 loops=1)InitPlan 1 (returns $0)->  Limit  (cost=0.43..103.28 rows=1 width=4) (actual time=9860.199..9860.202 rows=1 loops=1)->  Index Scan using id1_idx on test  (cost=0.43..445840.93 rows=4335 width=4) (actual time=9860.197..9860.197 rows=1 loops=1)Index Cond: (id1 IS NOT NULL)Filter: (id2 = 10000001)Rows Removed by Filter: 10000001
Planning time: 99.912 ms
Execution time: 9860.282 ms
(9 rows)Time: 10069.370 ms

我们换成id2_idx索引试试,改造一下SQL,min(id1)改成min(id1+0) 即可,结果令人惊讶,执行时间比使用id1_idx快很多。

postgres=> explain analyze  select min(id1+0) from test where id2 = 10000001;QUERY PLAN
----------------------------------------------------------------------------------------------------------------------------
Aggregate  (cost=295.38..295.39 rows=1 width=4) (actual time=1.878..1.878 rows=1 loops=1)->  Index Scan using id2_idx on test  (cost=0.43..273.70 rows=4335 width=4) (actual time=0.034..1.234 rows=5000 loops=1)Index Cond: (id2 = 10000001)
Planning time: 0.126 ms
Execution time: 1.931 ms
(5 rows)Time: 6.889 ms

分析及总结

执行计划由代价决定的,我们先看使用id1_idx索引的代价(cost=103.28..103.29),是怎么计算出来的呢?id1_idx索引总代价445840.93,满足条件记录数是4335, 我们需要的是最小值,因此只要找到索引第一条满足条件的记录即可,找到第一条记录的代价=445840.93/4335 ~= 103.29(优化器的代价模型是假设这4336条记录平均分布),而走id2_idx索引的代价为273.70,根据计算走id1_idx代价确实很低,那么是什么原因导致执行起来很慢呢?通过我们生产的数据看,10000001这个记录的数据分布集中在表的结尾,所以找到索引第一条记录代价并不是445840.93/4335,而是接近445840.93。

PG优化器对这样的case的优化并非完美,在生成执行计划的过程中可以结合一下数据分布特点,不断地优化代价模型。作为DBA,我们也要结合具体业务进行SQL优化,避免让优化器误判。

PostgreSQL索引走错一例分析相关推荐

  1. 51单片机C语言程序100例分析(1)IO+C语言+头文件

    51单片机C语言程序100例分析(1)IO+C语言+头文件 \\\插播一条:文章末尾有惊喜哟~///  P1=0xfe;//P1=11111110B,即P1.0输出低电平} 分析:通过这短短的几行代码 ...

  2. Postgresql快照优化Globalvis新体系分析(性能大幅增强)

    相关: <Postgresql源码(18)PGPROC相关结构> <Postgresql源码(65)新快照体系Globalvis工作原理分析> <Postgresql快照 ...

  3. Makefile 实际用例分析(一) ------- 比较通用的一种架构

    这里不再说Makefile的基本知识,如果需要学习,那么请参考: 下载:makefile 中文手册 或者 点击打开链接 或者 跟我一起写Makefile( 陈皓 ) 这里说的是一般的实际的一个工程应该 ...

  4. mysql pos点是什么,MySQL 5.6 主从报错一例

    原标题:MySQL 5.6 主从报错一例 作者:八怪(高鹏) 中亦科技数据库专家 1. 问题现象 版本:MySQL 5.6,采用传统 binlog file & pos 方式配置的主从复制结构 ...

  5. MySQL 5.6 主从报错一例

    1. 问题现象 版本:MySQL 5.6,采用传统 binlog file & pos 方式配置的主从复制结构. 实例重启后,主从复制报错如上图所示. 2. 错误含义 错误分为2部分. 第一部 ...

  6. postgresql索引_PostgreSQL中的索引— 10(Bloom)

    postgresql索引 indexing engine and the interface of access methods, as well as 索引引擎和访问方法的接口,以及hash ind ...

  7. postgresql索引_PostgreSQL中的索引— 8(RUM)

    postgresql索引 indexing engine, the interface of access methods, and main access methods, such as: 索引引 ...

  8. 【奥卡姆剃刀】一条短信骗走所有财产的分析

    一条短信骗走所有财产的分析 文 | 奥卡姆剃刀 案件如上图所示,不了解的请自行搜索,我这里不再赘述,主要讲讲这个诈骗的关键点在哪里.为便于描述,下面的[我]就是诈骗犯,[你]就是被骗的那个中国移动手机 ...

  9. postgresql索引_PostgreSQL中的索引— 6(SP-GiST)

    postgresql索引 indexing engine, 索引引擎 , the interface of access methods, and three methods: 访问方法的接口以及三种 ...

最新文章

  1. 由旋转画廊,看自定义RecyclerView.LayoutManager
  2. 这篇文章太懂程序员了,扎心了
  3. c++中wstring 和 string的转换
  4. [轉]Exploit Linux Kernel Slub Overflow
  5. php mysql搜索包含数据查询慢,php – 在mysql中搜索关键字并获取至少包含5个关键字的数据...
  6. 使用Java反射(Reflect)、自定义注解(Customer Annotation)生成简单SQL语句
  7. Idea部署web项目 与 eclipse 的不同之处
  8. MWeb Pro for Mac(静态博客生成软件)
  9. 外接显示器,多屏幕显示,出现鼠标移动缓慢,卡顿
  10. qunee for html5,Qunee for HTML5(二)
  11. qmh(qtmediahub)插件研究
  12. 记一次奇怪的truecrypt解密,隐藏分区的MasterKey
  13. 时光机特效在哪里?推荐这三个实用软件给你
  14. 代码实现判断主机字节序(大端 or小端 )-- 面试题
  15. html打印为pdf表格显示不全,打印表格时内容显示不完整怎么办?四种方法解决WPS不完整问题...
  16. 射影几何----利用中心和渐进方向求渐近线的方程
  17. world2010 插入参考文献时有跳行的问题
  18. 用c语言编程写出杨辉三角,用C语言编写杨辉三角
  19. C语言程序设计(第4版)谭浩强著—学习笔记chapter one 程序设计与C语言
  20. 机器学习_深度学习毕设题目汇总——肺结节

热门文章

  1. urbansound8k下载地址
  2. 有关包络Spectral Envelope的疑问
  3. linux图形登陆报拒绝权限,Linux-权限被拒绝?
  4. linux 同步方法剖析,Linux 同步方法剖析
  5. linux tomcat 进程杀掉_Linux-(一)常用命令总结
  6. dede php 调用自定义字段,在dedecms搜索结果列表页调用自定义字段的方法(绝对可用)...
  7. kivy python 读取oracle数据库_Kivy和Python线程-如何在它们之间获取数据
  8. Springboot [日志管理LogBack]
  9. jquery的html,text,val的区别
  10. 打开文件数过多 linux,Linux 解决 打开的文件数太多