免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2711 | 回复: 2
打印 上一主题 下一主题

《Batch insert in PostgreSQL[continuent 2]》 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-02-22 19:19 |只看该作者 |倒序浏览
自PostgreSQL9.0.1之后,explain的功能更强大了。语法也有如下更新:

EXPLAIN [ ( { ANALYZE boolean | VERBOSE boolean | COSTS boolean | BUFFERS boolean | FORMAT { TEXT | XML | JSON | YAML } } [, ...] ) ] statement
EXPLAIN [ ANALYZE ] [ VERBOSE ] statement

下面继续上一篇batch insert in PostgreSQL的几个BATCH操作,看看执行计划分别是什么样的:

test=> begin;explain (analyze,verbose true,costs true,buffers true,format json) execute t_test1(53,54,55,56);rollback;
BEGIN
                  QUERY PLAN                  
-----------------------------------------------
[                                            +
   {                                          +
     "Plan": {                                +
       "Node Type": "ModifyTable",            +
       "Operation": "Insert",                 +
       "Startup Cost": 0.00,                  +
       "Total Cost": 0.05,                    +
       "Plan Rows": 4,                        +
       "Plan Width": 8,                       +
       "Actual Startup Time": 0.049,          +
       "Actual Total Time": 0.049,            +
       "Actual Rows": 0,                      +
       "Actual Loops": 1,                     +
       "Shared Hit Blocks": 12,               +
       "Shared Read Blocks": 0,               +
       "Shared Written Blocks": 0,            +
       "Local Hit Blocks": 0,                 +
       "Local Read Blocks": 0,                +
       "Local Written Blocks": 0,             +
       "Temp Read Blocks": 0,                 +
       "Temp Written Blocks": 0,              +
       "Plans": [                             +
         {                                    +
           "Node Type": "Values Scan",        +
           "Parent Relationship": "Member",   +
           "Alias": "*VALUES*",               +
           "Startup Cost": 0.00,              +
           "Total Cost": 0.05,                +
           "Plan Rows": 4,                    +
           "Plan Width": 8,                   +
           "Actual Startup Time": 0.002,      +
           "Actual Total Time": 0.004,        +
           "Actual Rows": 4,                  +
           "Actual Loops": 1,                 +
           "Output": ["\"*VALUES*\".column1"],+
           "Shared Hit Blocks": 0,            +
           "Shared Read Blocks": 0,           +
           "Shared Written Blocks": 0,        +
           "Local Hit Blocks": 0,             +
           "Local Read Blocks": 0,            +
           "Local Written Blocks": 0,         +
           "Temp Read Blocks": 0,             +
           "Temp Written Blocks": 0           +
         }                                    +
       ]                                      +
     },                                       +
     "Triggers": [                            +
     ],                                       +
     "Total Runtime": 0.085                   +
   }                                          +
]
(1 row)

ROLLBACK
test=> begin;explain (analyze,verbose true,costs true,buffers true,format json) insert into tbl_test (id) values (53),(54),(55),(56);rollback;
BEGIN
                  QUERY PLAN                  
-----------------------------------------------
[                                            +
   {                                          +
     "Plan": {                                +
       "Node Type": "ModifyTable",            +
       "Operation": "Insert",                 +
       "Startup Cost": 0.00,                  +
       "Total Cost": 0.05,                    +
       "Plan Rows": 4,                        +
       "Plan Width": 8,                       +
       "Actual Startup Time": 0.048,          +
       "Actual Total Time": 0.048,            +
       "Actual Rows": 0,                      +
       "Actual Loops": 1,                     +
       "Shared Hit Blocks": 12,               +
       "Shared Read Blocks": 0,               +
       "Shared Written Blocks": 0,            +
       "Local Hit Blocks": 0,                 +
       "Local Read Blocks": 0,                +
       "Local Written Blocks": 0,             +
       "Temp Read Blocks": 0,                 +
       "Temp Written Blocks": 0,              +
       "Plans": [                             +
         {                                    +
           "Node Type": "Values Scan",        +
           "Parent Relationship": "Member",   +
           "Alias": "*VALUES*",               +
           "Startup Cost": 0.00,              +
           "Total Cost": 0.05,                +
           "Plan Rows": 4,                    +
           "Plan Width": 8,                   +
           "Actual Startup Time": 0.002,      +
           "Actual Total Time": 0.005,        +
           "Actual Rows": 4,                  +
           "Actual Loops": 1,                 +
           "Output": ["\"*VALUES*\".column1"],+
           "Shared Hit Blocks": 0,            +
           "Shared Read Blocks": 0,           +
           "Shared Written Blocks": 0,        +
           "Local Hit Blocks": 0,             +
           "Local Read Blocks": 0,            +
           "Local Written Blocks": 0,         +
           "Temp Read Blocks": 0,             +
           "Temp Written Blocks": 0           +
         }                                    +
       ]                                      +
     },                                       +
     "Triggers": [                            +
     ],                                       +
     "Total Runtime": 0.076                   +
   }                                          +
]
(1 row)

ROLLBACK

test=> begin;explain (analyze,verbose true,costs true,buffers true,format json) insert into tbl_test (id) select 53 union all select 54 union all select 55 union all select 56;rollback;
BEGIN
                     QUERY PLAN                     
----------------------------------------------------
[                                                 +
   {                                               +
     "Plan": {                                     +
       "Node Type": "ModifyTable",                 +
       "Operation": "Insert",                      +
       "Startup Cost": 0.00,                       +
       "Total Cost": 0.09,                         +
       "Plan Rows": 4,                             +
       "Plan Width": 4,                            +
       "Actual Startup Time": 0.057,               +
       "Actual Total Time": 0.057,                 +
       "Actual Rows": 0,                           +
       "Actual Loops": 1,                          +
       "Shared Hit Blocks": 12,                    +
       "Shared Read Blocks": 0,                    +
       "Shared Written Blocks": 0,                 +
       "Local Hit Blocks": 0,                      +
       "Local Read Blocks": 0,                     +
       "Local Written Blocks": 0,                  +
       "Temp Read Blocks": 0,                      +
       "Temp Written Blocks": 0,                   +
       "Plans": [                                  +
         {                                         +
           "Node Type": "Result",                  +
           "Parent Relationship": "Member",        +
           "Startup Cost": 0.00,                   +
           "Total Cost": 0.09,                     +
           "Plan Rows": 4,                         +
           "Plan Width": 4,                        +
           "Actual Startup Time": 0.006,           +
           "Actual Total Time": 0.010,             +
           "Actual Rows": 4,                       +
           "Actual Loops": 1,                      +
           "Output": ["(53)"],                     +
           "Shared Hit Blocks": 0,                 +
           "Shared Read Blocks": 0,                +
           "Shared Written Blocks": 0,             +
           "Local Hit Blocks": 0,                  +
           "Local Read Blocks": 0,                 +
           "Local Written Blocks": 0,              +
           "Temp Read Blocks": 0,                  +
           "Temp Written Blocks": 0,               +
           "Plans": [                              +
             {                                     +
               "Node Type": "Append",              +
               "Parent Relationship": "Outer",     +
               "Startup Cost": 0.00,               +
               "Total Cost": 0.08,                 +
               "Plan Rows": 4,                     +
               "Plan Width": 4,                    +
               "Actual Startup Time": 0.002,       +
               "Actual Total Time": 0.005,         +
               "Actual Rows": 4,                   +
               "Actual Loops": 1,                  +
               "Shared Hit Blocks": 0,             +
               "Shared Read Blocks": 0,            +
               "Shared Written Blocks": 0,         +
               "Local Hit Blocks": 0,              +
               "Local Read Blocks": 0,             +
               "Local Written Blocks": 0,          +
               "Temp Read Blocks": 0,              +
               "Temp Written Blocks": 0,           +
               "Plans": [                          +
                 {                                 +
                   "Node Type": "Result",          +
                   "Parent Relationship": "Member",+
                   "Startup Cost": 0.00,           +
                   "Total Cost": 0.01,             +
                   "Plan Rows": 1,                 +
                   "Plan Width": 0,                +
                   "Actual Startup Time": 0.000,   +
                   "Actual Total Time": 0.000,     +
                   "Actual Rows": 1,               +
                   "Actual Loops": 1,              +
                   "Output": ["53"],               +
                   "Shared Hit Blocks": 0,         +
                   "Shared Read Blocks": 0,        +
                   "Shared Written Blocks": 0,     +
                   "Local Hit Blocks": 0,          +
                   "Local Read Blocks": 0,         +
                   "Local Written Blocks": 0,      +
                   "Temp Read Blocks": 0,          +
                   "Temp Written Blocks": 0        +
                 },                                +
                 {                                 +
                   "Node Type": "Result",          +
                   "Parent Relationship": "Member",+
                   "Startup Cost": 0.00,           +
                   "Total Cost": 0.01,             +
                   "Plan Rows": 1,                 +
                   "Plan Width": 0,                +
                   "Actual Startup Time": 0.001,   +
                   "Actual Total Time": 0.002,     +
                   "Actual Rows": 1,               +
                   "Actual Loops": 1,              +
                   "Output": ["54"],               +
                   "Shared Hit Blocks": 0,         +
                   "Shared Read Blocks": 0,        +
                   "Shared Written Blocks": 0,     +
                   "Local Hit Blocks": 0,          +
                   "Local Read Blocks": 0,         +
                   "Local Written Blocks": 0,      +
                   "Temp Read Blocks": 0,          +
                   "Temp Written Blocks": 0        +
                 },                                +
                 {                                 +
                   "Node Type": "Result",          +
                   "Parent Relationship": "Member",+
                   "Startup Cost": 0.00,           +
                   "Total Cost": 0.01,             +
                   "Plan Rows": 1,                 +
                   "Plan Width": 0,                +
                   "Actual Startup Time": 0.000,   +
                   "Actual Total Time": 0.000,     +
                   "Actual Rows": 1,               +
                   "Actual Loops": 1,              +
                   "Output": ["55"],               +
                   "Shared Hit Blocks": 0,         +
                   "Shared Read Blocks": 0,        +
                   "Shared Written Blocks": 0,     +
                   "Local Hit Blocks": 0,          +
                   "Local Read Blocks": 0,         +
                   "Local Written Blocks": 0,      +
                   "Temp Read Blocks": 0,          +
                   "Temp Written Blocks": 0        +
                 },                                +
                 {                                 +
                   "Node Type": "Result",          +
                   "Parent Relationship": "Member",+
                   "Startup Cost": 0.00,           +
                   "Total Cost": 0.01,             +
                   "Plan Rows": 1,                 +
                   "Plan Width": 0,                +
                   "Actual Startup Time": 0.000,   +
                   "Actual Total Time": 0.000,     +
                   "Actual Rows": 1,               +
                   "Actual Loops": 1,              +
                   "Output": ["56"],               +
                   "Shared Hit Blocks": 0,         +
                   "Shared Read Blocks": 0,        +
                   "Shared Written Blocks": 0,     +
                   "Local Hit Blocks": 0,          +
                   "Local Read Blocks": 0,         +
                   "Local Written Blocks": 0,      +
                   "Temp Read Blocks": 0,          +
                   "Temp Written Blocks": 0        +
                 }                                 +
               ]                                   +
             }                                     +
           ]                                       +
         }                                         +
       ]                                           +
     },                                            +
     "Triggers": [                                 +
     ],                                            +
     "Total Runtime": 0.096                        +
   }                                               +
]
(1 row)

ROLLBACK;

执行效率从高到低排序:

insert into tbl_test (id) values (53),(54),(55),(56);

execute t_test1(53,54,55,56);

insert into tbl_test (id) select 53 union all select 54 union all select 55 union all select 56;

另外需要注意的是EXPLAIN ANALYZE将真实的运行被解释的SQL,如果有修改操作,慎用EXPLAIN ANALYZE,即使要用的话,一定要BEGIN;,ROLLBACK;

论坛徽章:
0
2 [报告]
发表于 2012-02-14 10:59 |只看该作者
很想了解,但没看懂。

论坛徽章:
0
3 [报告]
发表于 2012-02-14 16:02 |只看该作者
有空试试PostgreSQL9
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP