开始

比如说我已经做好了对分区表的规则:

postgres=# CREATE OR REPLACE FUNCTION ptest_insert_trigger() RETURNS TRIGGER AS $$
postgres$#
postgres$# BEGIN
postgres$#
postgres$#    IF ( NEW.id <5000000 ) THEN
postgres$#        INSERT INTO ctest01 VALUES (NEW.*);
postgres$#    ELSIF ( NEW.id >= 5000000 ) THEN
postgres$#        INSERT INTO ctest02 VALUES (NEW.*);
postgres$#    ELSE
postgres$#        RAISE EXCEPTION 'Error while inserting data';
postgres$#    END IF;
postgres$#
postgres$#   RETURN NULL;
postgres$# END; $$ LANGUAGE plpgsql;
CREATE FUNCTION
postgres=#
postgres=# CREATE TRIGGER insert_ptest_trigger BEFORE INSERT ON ptest FOR EACH ROW
postgres-#   EXECUTE PROCEDURE ptest_insert_trigger();
CREATE TRIGGER
postgres=# 

就是说 ctest01 的数据, id<5000000, ctest02的数据, id>=5000000。

此时我的执行计划仍然是这个样子的:它似乎没有意识到我的ptest表的规则:

postgres=# explain select * from ptest where id=5000 or id=6000000;QUERY PLAN
-----------------------------------------------------------------------------------------------Result  (cost=0.00..54.93 rows=5 width=20)->  Append  (cost=0.00..54.93 rows=5 width=20)->  Seq Scan on ptest  (cost=0.00..0.00 rows=1 width=62)Filter: ((id = 5000) OR (id = 6000000))->  Bitmap Heap Scan on ctest01 ptest  (cost=19.49..27.46 rows=2 width=9)Recheck Cond: ((id = 5000) OR (id = 6000000))->  BitmapOr  (cost=19.49..19.49 rows=2 width=0)->  Bitmap Index Scan on ctest01_id_idx  (cost=0.00..9.74 rows=1 width=0)Index Cond: (id = 5000)->  Bitmap Index Scan on ctest01_id_idx  (cost=0.00..9.74 rows=1 width=0)Index Cond: (id = 6000000)->  Bitmap Heap Scan on ctest02 ptest  (cost=19.49..27.46 rows=2 width=9)Recheck Cond: ((id = 5000) OR (id = 6000000))->  BitmapOr  (cost=19.49..19.49 rows=2 width=0)->  Bitmap Index Scan on ctest02_id_idx  (cost=0.00..9.74 rows=1 width=0)Index Cond: (id = 5000)->  Bitmap Index Scan on ctest02_id_idx  (cost=0.00..9.74 rows=1 width=0)Index Cond: (id = 6000000)
(18 rows)postgres=# 

对where 条件,它把它应用到每一个分区子表上了!

这里要谈到一个参数:

constraint_exclusion (enum)

Controls the query planner's use of table constraints to optimize queries. The allowed values of constraint_exclusion areon (examine constraints for all tables), off (never examine constraints), and partition (examine constraints only for inheritance child tables and UNION ALL subqueries). partition is the default setting. It is often used with inheritance and partitioned tables to improve performance.

当其为on或者 partition 的时候,在我这个例子里都是一样效果(我的父表没有数据)

postgres=# show constraint_exclusion;constraint_exclusion
----------------------
 partition
(1 row)postgres=# explain select * from ptest where id=5000;QUERY PLAN
-------------------------------------------------------------------------------------------------Result  (cost=0.00..13.75 rows=2 width=36)->  Append  (cost=0.00..13.75 rows=2 width=36)->  Seq Scan on ptest  (cost=0.00..0.00 rows=1 width=62)Filter: (id = 5000)->  Index Scan using ctest01_id_idx on ctest01 ptest  (cost=0.00..13.75 rows=1 width=9)Index Cond: (id = 5000)
(6 rows)postgres=# postgres=# explain select * from ptest where id=600000;QUERY PLAN
-------------------------------------------------------------------------------------------------Result  (cost=0.00..13.75 rows=2 width=36)->  Append  (cost=0.00..13.75 rows=2 width=36)->  Seq Scan on ptest  (cost=0.00..0.00 rows=1 width=62)Filter: (id = 600000)->  Index Scan using ctest01_id_idx on ctest01 ptest  (cost=0.00..13.75 rows=1 width=9)Index Cond: (id = 600000)
(6 rows)postgres=# postgres=# explain select * from ptest where id=5000
UNION ALL
select * from ptest where id=6000000;QUERY PLAN
-------------------------------------------------------------------------------------------------------------Result  (cost=0.00..27.55 rows=4 width=36)->  Append  (cost=0.00..27.55 rows=4 width=36)->  Result  (cost=0.00..13.75 rows=2 width=36)->  Append  (cost=0.00..13.75 rows=2 width=36)->  Seq Scan on ptest  (cost=0.00..0.00 rows=1 width=62)Filter: (id = 5000)->  Index Scan using ctest01_id_idx on ctest01 ptest  (cost=0.00..13.75 rows=1 width=9)Index Cond: (id = 5000)->  Result  (cost=0.00..13.75 rows=2 width=36)->  Append  (cost=0.00..13.75 rows=2 width=36)->  Seq Scan on ptest  (cost=0.00..0.00 rows=1 width=62)Filter: (id = 6000000)->  Index Scan using ctest02_id_idx on ctest02 ptest  (cost=0.00..13.75 rows=1 width=9)Index Cond: (id = 6000000)
(14 rows)postgres=# postgres=# explain select * from ptest where id=5000 or id=6000000;QUERY PLAN
-----------------------------------------------------------------------------------------------Result  (cost=0.00..54.93 rows=5 width=20)->  Append  (cost=0.00..54.93 rows=5 width=20)->  Seq Scan on ptest  (cost=0.00..0.00 rows=1 width=62)Filter: ((id = 5000) OR (id = 6000000))->  Bitmap Heap Scan on ctest01 ptest  (cost=19.49..27.46 rows=2 width=9)Recheck Cond: ((id = 5000) OR (id = 6000000))->  BitmapOr  (cost=19.49..19.49 rows=2 width=0)->  Bitmap Index Scan on ctest01_id_idx  (cost=0.00..9.74 rows=1 width=0)Index Cond: (id = 5000)->  Bitmap Index Scan on ctest01_id_idx  (cost=0.00..9.74 rows=1 width=0)Index Cond: (id = 6000000)->  Bitmap Heap Scan on ctest02 ptest  (cost=19.49..27.46 rows=2 width=9)Recheck Cond: ((id = 5000) OR (id = 6000000))->  BitmapOr  (cost=19.49..19.49 rows=2 width=0)->  Bitmap Index Scan on ctest02_id_idx  (cost=0.00..9.74 rows=1 width=0)Index Cond: (id = 5000)->  Bitmap Index Scan on ctest02_id_idx  (cost=0.00..9.74 rows=1 width=0)Index Cond: (id = 6000000)
(18 rows)postgres=# 

postgres=# set session constraint_exclusion=on;
SET
postgres=# postgres=# explain select * from ptest where id=5000;QUERY PLAN
-------------------------------------------------------------------------------------------------Result  (cost=0.00..13.75 rows=2 width=36)->  Append  (cost=0.00..13.75 rows=2 width=36)->  Seq Scan on ptest  (cost=0.00..0.00 rows=1 width=62)Filter: (id = 5000)->  Index Scan using ctest01_id_idx on ctest01 ptest  (cost=0.00..13.75 rows=1 width=9)Index Cond: (id = 5000)
(6 rows)postgres=# postgres=#
postgres=# explain select * from ptest where id=6000000;QUERY PLAN
-------------------------------------------------------------------------------------------------Result  (cost=0.00..13.75 rows=2 width=36)->  Append  (cost=0.00..13.75 rows=2 width=36)->  Seq Scan on ptest  (cost=0.00..0.00 rows=1 width=62)Filter: (id = 6000000)->  Index Scan using ctest02_id_idx on ctest02 ptest  (cost=0.00..13.75 rows=1 width=9)Index Cond: (id = 6000000)
(6 rows)postgres=# postgres=# explain select * from ptest where id=5000
postgres-# UNION ALL
postgres-# select * from ptest where id=6000000;QUERY PLAN
-------------------------------------------------------------------------------------------------------------Result  (cost=0.00..27.55 rows=4 width=36)->  Append  (cost=0.00..27.55 rows=4 width=36)->  Result  (cost=0.00..13.75 rows=2 width=36)->  Append  (cost=0.00..13.75 rows=2 width=36)->  Seq Scan on ptest  (cost=0.00..0.00 rows=1 width=62)Filter: (id = 5000)->  Index Scan using ctest01_id_idx on ctest01 ptest  (cost=0.00..13.75 rows=1 width=9)Index Cond: (id = 5000)->  Result  (cost=0.00..13.75 rows=2 width=36)->  Append  (cost=0.00..13.75 rows=2 width=36)->  Seq Scan on ptest  (cost=0.00..0.00 rows=1 width=62)Filter: (id = 6000000)->  Index Scan using ctest02_id_idx on ctest02 ptest  (cost=0.00..13.75 rows=1 width=9)Index Cond: (id = 6000000)
(14 rows)postgres=# postgres=# explain select * from ptest where id=5000 or id=6000000;QUERY PLAN
-----------------------------------------------------------------------------------------------Result  (cost=0.00..54.93 rows=5 width=20)->  Append  (cost=0.00..54.93 rows=5 width=20)->  Seq Scan on ptest  (cost=0.00..0.00 rows=1 width=62)Filter: ((id = 5000) OR (id = 6000000))->  Bitmap Heap Scan on ctest01 ptest  (cost=19.49..27.46 rows=2 width=9)Recheck Cond: ((id = 5000) OR (id = 6000000))->  BitmapOr  (cost=19.49..19.49 rows=2 width=0)->  Bitmap Index Scan on ctest01_id_idx  (cost=0.00..9.74 rows=1 width=0)Index Cond: (id = 5000)->  Bitmap Index Scan on ctest01_id_idx  (cost=0.00..9.74 rows=1 width=0)Index Cond: (id = 6000000)->  Bitmap Heap Scan on ctest02 ptest  (cost=19.49..27.46 rows=2 width=9)Recheck Cond: ((id = 5000) OR (id = 6000000))->  BitmapOr  (cost=19.49..19.49 rows=2 width=0)->  Bitmap Index Scan on ctest02_id_idx  (cost=0.00..9.74 rows=1 width=0)Index Cond: (id = 5000)->  Bitmap Index Scan on ctest02_id_idx  (cost=0.00..9.74 rows=1 width=0)Index Cond: (id = 6000000)
(18 rows)postgres=# 

也就是说, constraint_exclusion 的识别能力也是有限的。对于 where 条件比较复杂的,也是无法处理的。

那么,constraint_exclusion off 时候,又如此?此时连对 id=5000 这样的,都需在所有的分区表里查询:

postgres=# set session constraint_exclusion=off;
SET
postgres=#
postgres=#
postgres=# explain select * from ptest where id=5000;QUERY PLAN
-------------------------------------------------------------------------------------------------Result  (cost=0.00..27.51 rows=3 width=27)->  Append  (cost=0.00..27.51 rows=3 width=27)->  Seq Scan on ptest  (cost=0.00..0.00 rows=1 width=62)Filter: (id = 5000)->  Index Scan using ctest01_id_idx on ctest01 ptest  (cost=0.00..13.75 rows=1 width=9)Index Cond: (id = 5000)->  Index Scan using ctest02_id_idx on ctest02 ptest  (cost=0.00..13.75 rows=1 width=9)Index Cond: (id = 5000)
(8 rows)postgres=# postgres=# explain select * from ptest where id=6000000;QUERY PLAN
-------------------------------------------------------------------------------------------------Result  (cost=0.00..27.51 rows=3 width=27)->  Append  (cost=0.00..27.51 rows=3 width=27)->  Seq Scan on ptest  (cost=0.00..0.00 rows=1 width=62)Filter: (id = 6000000)->  Index Scan using ctest01_id_idx on ctest01 ptest  (cost=0.00..13.75 rows=1 width=9)Index Cond: (id = 6000000)->  Index Scan using ctest02_id_idx on ctest02 ptest  (cost=0.00..13.75 rows=1 width=9)Index Cond: (id = 6000000)
(8 rows)postgres=# 

结束

PostgreSQL分区表的执行计划相关推荐

  1. 查找mysql的分区情况_MySQL 5.5 查看分区表的执行计划

    --创建测试表 mysql> CREATE TABLE trb1 (id INT, name VARCHAR(50), purchased DATE) ->     PARTITION B ...

  2. PostgreSQL中的执行计划

    PostgreSQL中的执行计划 EXPLAN 预生成执行计划 EXPLAN sql 真实执行计划 explan analyze sql 输出详细内容 explan(analyze on, timin ...

  3. 对PostgreSQL Nested Loop 执行计划的初步学习

    开始 伪代码如下: for (i = 0; i < length(outer); i++)for (j = 0; j < length(inner); j++)if (outer[i] = ...

  4. PostgreSQL SQL OUTLINE插件sr_plan (保存、篡改、固定 执行计划)

    标签 PostgreSQL , sql plan outline , 执行计划篡改 , query rewrite , sr_plan , pg plan hint 背景 功能较为强大的数据库,通常都 ...

  5. Postgresql学习笔记之——SQL 执行计划

    一.执行计划的解释 1.explain 命令 语法: EXPLAIN [ ( option [, ...] ) ] statement EXPLAIN [ ANALYZE ] [ VERBOSE ] ...

  6. 一次搞定各种数据库SQL执行计划

    作者 | 董旭阳TonyDong 出品 | CSDN 博客 执行计划(execution plan,也叫查询计划或者解释计划)是数据库执行 SQL 语句的具体步骤,例如通过索引还是全表扫描访问表中的数 ...

  7. 6.读懂mysql执行计划

    文章目录 1.执行计划概念和语法 1.执行计划的概念 2.执行计划的语法 1.常规执行计划语法 2.扩展执行计划的语法 3.分区表的执行计划语法 2.执行计划包含的信息 1.id​:查询的顺序 2.s ...

  8. oracle分区表执行计划分区合并,利用ORACLE分区技术提高管理和性能_PART2

    接PART1:http://blog.chinaunix.net/uid/7655508.html 11g interval分区: 1)11g之前创建日期范围分区,经常是预先创建一部分,等即将用完重新 ...

  9. PostgreSQL查询当前执行中SQL的执行计划——pg_show_plans

    点击上方"蓝字" 关注我们,享更多干货! 执行计划存储 如果同样的SQL要执行很多遍,且每次都是同样的执行计划.每次都发生硬解析,则会消耗大量时间.类似于Oracle存放执行计划的 ...

最新文章

  1. HJ86 求最大连续bit数
  2. 赠书 | 读懂生成对抗神经网络 GAN,看这文就够了
  3. python流程图-python中的图表渲染(流程图可视化)
  4. Redis 持久化策略 : RDB持久化、AOF持久化、混合持久化
  5. Python数据分析学习笔记01:安装相关软件、导入扩展模块与集成开发环境
  6. [ZJOI2012]灾难(建图)
  7. Asp.net MVC的ViewData与ViewBag以及TemplateData的使用与区别
  8. 点云配准ICPNDT
  9. 安卓10终于来了,能吃上的果然只有这些手机!
  10. Halcon之Variation Model
  11. iOS tableview的第二页数据刷新特定某个cell数据的解决办法
  12. 上市公司“掘金”大数据 多领域大数据应用受热捧
  13. Halium 9 尝鲜 -- 在小米平板4上的移植 (六)
  14. airpak模拟案例,Airpak模拟教程-体育馆通风模拟案例-CFD数值模拟教程airpak.pdf
  15. matlab分布式计算报告,简单的matlab分布式计算
  16. 工业物联网体系架构概述及基于工业物联网的智能制造
  17. 【论文阅读】Text Gestalt: Stroke-Aware Scene Text Image Super-Resolution
  18. Python爬虫--爬取妹子网
  19. 女人的美丽应该自己欣赏
  20. 爬取国家统计局人口与就业统计数据,看了你的文章,我朋友晚上托梦告诉我牢饭很好吃

热门文章

  1. 石墨烯可将硬盘容量提高十倍,剑桥在Nature子刊发表最新研究
  2. 张艺谋镜头里的科技力量:为世界注入5G之心
  3. 上海名校CS专业第一本科生:我5天里打工3天,丝毫不影响GPA,可见「教学」有多荒谬...
  4. 如何提高PyTorch“炼丹”速度?这位小哥总结了17种方法,可直接上手更改的那种...
  5. 百度Q2日进2.9个亿,新基建推动Apollo上位!李彦宏开招管培生:亲自选亲自带...
  6. Apollo仿真「训练有素」,长沙无人驾驶出租「轻车熟路」
  7. 移动版“全功能”Photoshop发布!还有AI剪视频一键传抖音、一键抠图功能上线 | Adobe MAX 2019...
  8. 直播报名 | NVIDIA公开课:Style-Gan的架构与实现
  9. 自学机器学习,怎么才能找到工作啊?至少要避开十大雷区 | Reddit高热
  10. 为什么工作10年你的工资还不如新来的实习生