官方说法:

work_mem (integer)

Specifies the amount of memory to be used by internal sort operations and hash tables before writing to temporary disk files. The value defaults to four megabytes (4MB). Note that for a complex query, several sort or hash operations might be running in parallel; each operation will be allowed to use as much memory as this value specifies before it starts to write data into temporary files. Also, several running sessions could be doing such operations concurrently. Therefore, the total memory used could be many times the value of work_mem; it is necessary to keep this fact in mind when choosing the value. Sort operations are used for ORDER BYDISTINCT, and merge joins. Hash tables are used in hash joins, hash-based aggregation, and hash-based processing of IN subqueries.

声明内部排序操作和Hash表在开始使用临时磁盘文件之前使用的内存限制。 缺省数值是4兆字节(4MB)。请注意对于复杂的查询, 可能会并发行若干排序或者散列表操作;每个都会被允许使用这个参数获得这么多内存, 然后才会开始求助于临时文件。同样,好几个正在运行的会话可能会同时进行排序操作。 因此使用的总内存可能是work_mem的好几倍。 当选择这个值的时候,必须记住这个事实。 ORDER BYDISTINCT和融合连接都要用到排序操作。 Hash表在散列连接、散列为基础的聚合、散列为基础的IN子查询处理中都要用到。

生成一百万条记录

[postgres@sht-sgmhadoopdn- ~]$ perl -e '@c=("a".."z","A".."Z",0..9); print join("",map{$c[rand@c]}10..20+rand(40))."\n" for 1..1000000' > /tmp/random_strings
[postgres@sht-sgmhadoopdn- ~]$ ls -lh /tmp/random_strings
-rw-r--r-- postgres dba 31M Nov : /tmp/random_strings

创建对应表结构并导入数据

edbstore=# CREATE TABLE test (id serial PRIMARY KEY, random_text text );
CREATE TABLE
edbstore=# \d test
Table "public.test"
Column | Type | Modifiers
-------------+---------+---------------------------------------------------
id | integer | not null default nextval('test_id_seq'::regclass)
random_text | text |
Indexes:
"test_pkey" PRIMARY KEY, btree (id) edbstore=# \d
List of relations
Schema | Name | Type | Owner
--------+-------------+----------+----------
public | tb1 | table | postgres
public | test | table | postgres
public | test_id_seq | sequence | postgres
(3 rows) edbstore=# copy test (random_text) FROM '/tmp/random_strings';
COPY 1000000
edbstore=# select * from test limit 10;
id | random_text
----+-------------------------------------------------
1 | CKQyHTYH5VjeHRUC6YYLF8H5S
2 | G22uBhFmrlA17wTUzf
3 | ey6kX7I6etknzhEFCL
4 | 8LB6navSS8VyoIeqbJBx9RqB3O4AI8GIFExnM7s
5 | bvYt4dKGSiAun6yA5Q7owlKWJGEgD0nlxoBRZm8B
6 | qk1RfhXHwo2PNpbI4
7 | rnPterTw1a3Z3DoL8rhzlltUKb5
8 | l2TrrbDsBkAa5V5ZBKFE59k4T7sDKA58yrS0mJNssl7CJnF
9 | xM9HPgq6QMRsx1aOTqM0LPRQRYkQy50uV
10 | viSJ4p1i3O0dY8tKei3x
(10 rows)

通过每次获取不通的数据量来观察每次explain的执行方式

edbstore=# show work_mem;
work_mem
----------
1MB
(1 row) edbstore=# EXPLAIN analyze SELECT * FROM test WHERE id <= 10 ORDER BY random_text ASC;
QUERY PLAN
------------------------------------------------------------------------------------------------------------------------
Sort (cost=8.73..8.75 rows=9 width=35) (actual time=0.188..0.202 rows=10 loops=1)
Sort Key: random_text
Sort Method: quicksort Memory: 25kB
-> Index Scan using test_pkey on test (cost=0.42..8.58 rows=9 width=35) (actual time=0.018..0.037 rows=10 loops=1)
Index Cond: (id <= 10)
Planning time: 1.435 ms
Execution time: 0.294 ms
(7 rows) edbstore=# EXPLAIN analyze SELECT * FROM test WHERE id <= 100 ORDER BY random_text ASC;
QUERY PLAN
----------------------------------------------------------------------------------------------------------------------------
Sort (cost=13.50..13.75 rows=100 width=35) (actual time=0.870..1.027 rows=100 loops=1)
Sort Key: random_text
Sort Method: quicksort Memory: 34kB
-> Index Scan using test_pkey on test (cost=0.42..10.18 rows=100 width=35) (actual time=0.022..0.218 rows=100 loops=1)
Index Cond: (id <= 100)
Planning time: 0.286 ms
Execution time: 1.248 ms
(7 rows) edbstore=# EXPLAIN analyze SELECT * FROM test WHERE id <= 1000 ORDER BY random_text ASC;
QUERY PLAN
------------------------------------------------------------------------------------------------------------------------------
Sort (cost=92.57..95.10 rows=1011 width=35) (actual time=8.846..10.251 rows=1000 loops=1)
Sort Key: random_text
Sort Method: quicksort Memory: 112kB
-> Index Scan using test_pkey on test (cost=0.42..42.12 rows=1011 width=35) (actual time=0.027..2.474 rows=1000 loops=1)
Index Cond: (id <= 1000)
Planning time: 0.286 ms
Execution time: 11.584 ms
(7 rows) edbstore=# EXPLAIN analyze SELECT * FROM test WHERE id <= 10000 ORDER BY random_text ASC;
QUERY PLAN
----------------------------------------------------------------------------------------------------------------------------------
Sort (cost=1049.39..1074.68 rows=10116 width=35) (actual time=144.963..160.943 rows=10000 loops=1)
Sort Key: random_text
Sort Method: external merge Disk: 448kB
-> Index Scan using test_pkey on test (cost=0.42..376.45 rows=10116 width=35) (actual time=0.063..22.225 rows=10000 loops=1)
Index Cond: (id <= 10000)
Planning time: 0.149 ms
Execution time: 173.841 ms
(7 rows) edbstore=# EXPLAIN analyze SELECT * FROM test WHERE id <= 100000 ORDER BY random_text ASC;
QUERY PLAN
--------------------------------------------------------------------------------------------------------------------------------------
Sort (cost=17477.39..17727.70 rows=100122 width=35) (actual time=1325.789..1706.516 rows=100000 loops=1)
Sort Key: random_text
Sort Method: external merge Disk: 4440kB
-> Index Scan using test_pkey on test (cost=0.42..3680.56 rows=100122 width=35) (actual time=0.088..214.490 rows=100000 loops=1)
Index Cond: (id <= 100000)
Planning time: 0.147 ms
Execution time: 1822.008 ms
(7 rows) edbstore=# EXPLAIN analyze SELECT * FROM test WHERE id <= 1000000 ORDER BY random_text ASC;
QUERY PLAN
------------------------------------------------------------------------------------------------------------------------
Sort (cost=202426.34..204926.34 rows=1000000 width=35) (actual time=8703.143..10160.421 rows=1000000 loops=1)
Sort Key: random_text
Sort Method: external merge Disk: 44504kB
-> Seq Scan on test (cost=0.00..20732.00 rows=1000000 width=35) (actual time=0.024..1021.491 rows=1000000 loops=1)
Filter: (id <= 1000000)
Planning time: 0.316 ms
Execution time: 10577.464 ms
(7 rows)
row Sort Method Execution time
10 quicksort  Memory: 25kB 0.294 ms
100 Sort Method: quicksort  Memory: 34kB 1.248 ms
1000 Sort Method: quicksort  Memory: 112kB 11.584 ms
10000 Sort Method: external merge  Disk: 448kB 173.841 ms
100000 Sort Method: external merge  Disk: 4440kB 1822.008 ms
1000000 Sort Method: external merge  Disk: 44504kB 10577.464 ms

通过上图我们可以看到,当sort的数据大于一万条时,explain显示排序方法从 quicksort in memory, 到external merge disk method,说明此时的work_mem的大小不能满足我们在内存的sort和hash表的需求。此时我们将work_mem参数的值调大

edbstore=# set work_mem="500MB";
SET
edbstore=# EXPLAIN analyze SELECT * FROM test WHERE id <= 1000000 ORDER BY random_text ASC;
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------
Sort (cost=120389.84..122889.84 rows=1000000 width=35) (actual time=6232.270..6884.121 rows=1000000 loops=1)
Sort Key: random_text
Sort Method: quicksort Memory: 112847kB
-> Seq Scan on test (cost=0.00..20732.00 rows=1000000 width=35) (actual time=0.015..659.035 rows=1000000 loops=1)
Filter: (id <= 1000000)
Planning time: 0.125 ms
Execution time: 7302.621 ms
(7 rows)
row Sort Method Execution time
1000000 quicksort  Memory: 112847kB 6887.851 ms

可以发现sort method从merg disk变成quicksort in memory。

https://www.depesz.com/2011/07/03/understanding-postgresql-conf-work_mem/

PostgreSQL work_mem理解的更多相关文章

  1. postgresql spi开发笔记

    #include "postgres.h" #include "fmgr.h" #include <string.h> #ifdef PG_MODU ...

  2. GitLab在Centos下的安装步骤

    第一步:(安装工具包) sudo yum install curl openssh-server postfix cronie sudo service postfix start sudo chkc ...

  3. Jenkins + Ansible + Gitlab之gitlab篇

    前言 持续交付 版本控制器:Gitlab.GitHub 持续集成工具:jenkins 部署工具:ansible  课程安排 Gitlab搭建与流程使用 Ansible环境配置与Playbook编写规范 ...

  4. 搞IT,算法编程不错的学习网址 & 一些专栏博客大神的地址(汇总)

    博客专栏大神 王晓华(算法的乐趣) 算法系列:http://blog.csdn.net/orbit/article/category/830251 PostgreSQL深入理解内核系列:http:// ...

  5. 二、CentOS 7安装部署GitLab服务器(解决邮箱发信问题)

    一.环境安装(10.0.0) 1.安装依赖软件 yum -y install policycoreutils policycoreutils-python openssh-server openssh ...

  6. PostgreSQL Replication之第一章 理解复制概念(1)

    PostgreSQL Replication系列翻译自PostgreSQL Replication一书 在本章中,将会介绍不同的复制概念,您会了解哪些类型的复制对哪一种实用场景是最合适的. 在本章的最 ...

  7. PostgreSQL Replication之第七章 理解Linux高可用(1)

    高可用(HA)是工业长期持续的,不间断的服务.在本章,您将了解高可用软件的历史,概念和实现与PostgreSQL复制和高可用之间的关系. 本章将详细地讲述如下主题: •理解高可用性的目的 •衡量可用性 ...

  8. PostgreSQL Replication之第三章 理解即时恢复(1)

    到现在为止,您已经掌握了一定的理论.因为生活不仅由理论组成(它可能同样重要),是时候深入实际的工作了. 本章的目标是让您明白如何恢复数据到一个给定的时间点.当您的系统崩溃或者有人意外地删除了一个表,不 ...

  9. PostgreSQL Replication之第二章 理解PostgreSQL的事务日志(5)

    2.5 XLOG的内部结构 我们将使用事务贯穿本书,并让您在技术层面上更深地洞察事情是如果工作的,我们已经增加了这部分专门处理XLOG的内部工作机制.我们会尽量避免前往下降到C级,因为这将超出本书的范 ...

随机推荐

  1. Java UTC时间与本地时间互相转换

    协调世界时,又称世界统一时间.世界标准时间.国际协调时间.由于英文(CUT)和法文(TUC)的缩写不同,作为妥协,简称UTC. 这套时间系统被应用于许多互联网和万维网的标准中,例如,网络时间协议就是协 ...

  2. 知识在与温故、总结-再读CLR

    序 CLR,通用语言运行时,每个.Net 程序猿,都会第一时间接触到.记得2008年,第一次学习Jeffrey Richter的CLR Via C#,读的懵懵懂懂,大抵因为编码太少,理解的只是概念和皮 ...

  3. VKD224B触摸芯片调试笔记

    1.通过阅读datasheet了解芯片怎么使用,一般datasheet会提供参考电路.和相应的电气参数. 通过上面的表格可以知道芯片的供电,所需电流. 这个芯片可以通过引脚选择模式.通过上面的选项设置 ...

  4. Android项目开发第二天,关于GitHub

    一. 今天在网上学习了如何使用GitHub,了解了GitHub是干什么的. 作为开源代码库以及版本控制系统,Github拥有超过900万开发者用户.随着越来越多的应用程序转移到了云上,Github已经 ...

  5. json字符串、json对象、数组之间的转换

    json字符串转化成json对象 // jquery的方法 var jsonObj = $.parseJSON(jsonStr) //js 的方法 var jsonObj = JSON.parse(j ...

  6. Docker 部署 elk + filebeat

    Docker 部署 elk + filebeat kibana 开源的分析与可视化平台logstash 日志收集工具 logstash-forwarder(原名lubmberjack)elastics ...

  7. spool例子

    set head offset echo offset feed offset heads offset pages 50000SET NEWPAGE NONEcolumn yesterday new ...

  8. Spring 学习——Spring注解——Autowiring(自动装配)

    装配方式 方式一:默认 方式二:byName:根据属性名称自动装配.会查找Bean容器内部所有初始化的与属性名成相同的Bean,自动装配.(需要通过set方法注入,注入Bean的id名称需要和实体类的 ...

  9. 王之泰201771010131《面向对象程序设计(java)》第九周学习总结

    第一部分:理论知识学习部分 第7章异常.日志.断言和调试 概念:异常.异常类型.异常声明.异常抛出. 异常捕获1.异常处理技术2.断言的概念及使用3.基本的调试技巧 1)异常的概念 a.Java的异常 ...

  10. AjaxPro对象参数传递

    1.客户端配置 2.服务端注册,配置 对象参数用到的方法 3.Web.config文件配置 需要注意的是ajax以及ajaxpro调用的方法必须是静态方法,如果存在非静态方法可以从页面作为参数传递