|
| 1 | +## PostgreSQL prepared statement和simple query的profile及性能差异 |
| 2 | + |
| 3 | +### 作者 |
| 4 | +digoal |
| 5 | + |
| 6 | +### 日期 |
| 7 | +2016-04-26 |
| 8 | + |
| 9 | +### 标签 |
| 10 | +PostgreSQL , prepared statement , simple query , profile |
| 11 | + |
| 12 | +---- |
| 13 | + |
| 14 | +## 背景 |
| 15 | +prepared statement是非常重要的高并发SQL优化手段之一,效果也显而易见。 |
| 16 | + |
| 17 | +下面是测试,同时观察绑定和不绑定的情况下的profile。 |
| 18 | + |
| 19 | +在未使用绑定变量的时候,新增或上升了一些硬解析相关的CODE。 |
| 20 | + |
| 21 | +测试数据 |
| 22 | + |
| 23 | +``` |
| 24 | +postgres=# create table test(id int primary key, info text); |
| 25 | + |
| 26 | +postgres=# insert into test select generate_series(1,1000000),'test'; |
| 27 | + |
| 28 | +postgres=# create or replace function f1(int) returns setof text as $$ |
| 29 | + select info from test where id=$1; |
| 30 | +$$ language sql; |
| 31 | +``` |
| 32 | + |
| 33 | +测试用例 |
| 34 | + |
| 35 | +``` |
| 36 | +vi test.sql |
| 37 | +\setrandom id 1 1000000 |
| 38 | +select f1(:id); |
| 39 | +``` |
| 40 | + |
| 41 | +使用绑定变量 |
| 42 | + |
| 43 | +``` |
| 44 | +pgbench -M prepared -n -r -P 5 -f ./test.sql -c 64 -j 64 -T 100 |
| 45 | + |
| 46 | +progress: 10.0 s, 526016.9 tps, lat 0.120 ms stddev 0.033 |
| 47 | +progress: 15.0 s, 523072.8 tps, lat 0.121 ms stddev 0.027 |
| 48 | +progress: 20.0 s, 523305.2 tps, lat 0.121 ms stddev 0.017 |
| 49 | +progress: 25.0 s, 523320.9 tps, lat 0.121 ms stddev 0.015 |
| 50 | +progress: 30.0 s, 523290.4 tps, lat 0.121 ms stddev 0.016 |
| 51 | +progress: 35.0 s, 523216.3 tps, lat 0.121 ms stddev 0.015 |
| 52 | +progress: 40.0 s, 523046.3 tps, lat 0.121 ms stddev 0.022 |
| 53 | +progress: 45.0 s, 523200.9 tps, lat 0.121 ms stddev 0.015 |
| 54 | +progress: 50.0 s, 523853.5 tps, lat 0.121 ms stddev 0.016 |
| 55 | +progress: 55.0 s, 526587.1 tps, lat 0.120 ms stddev 0.005 |
| 56 | +progress: 60.0 s, 526710.0 tps, lat 0.120 ms stddev 0.008 |
| 57 | +``` |
| 58 | + |
| 59 | +TOP 调用 |
| 60 | + |
| 61 | +``` |
| 62 | +perf top |
| 63 | + |
| 64 | + PerfTop: 62851 irqs/sec kernel:12.9% exact: 0.0% [1000Hz cycles], (all, 64 CPUs) |
| 65 | +--------------------------------------------------------------------------------------------- |
| 66 | + samples pcnt function DSO |
| 67 | + _______ _____ _____________________________ ____________________________________ |
| 68 | + 39828.00 4.3% AllocSetAlloc /home/digoal/pgsql9.5.2/bin/postgres |
| 69 | + 33282.00 3.6% SearchCatCache /home/digoal/pgsql9.5.2/bin/postgres |
| 70 | + 23098.00 2.5% base_yyparse /home/digoal/pgsql9.5.2/bin/postgres |
| 71 | + 21318.00 2.3% GetSnapshotData /home/digoal/pgsql9.5.2/bin/postgres |
| 72 | + 13218.00 1.4% hash_search_with_hash_value /home/digoal/pgsql9.5.2/bin/postgres |
| 73 | + 11399.00 1.2% _int_malloc /lib64/libc-2.12.so |
| 74 | + 11362.00 1.2% LWLockAcquire /home/digoal/pgsql9.5.2/bin/postgres |
| 75 | + 11151.00 1.2% palloc /home/digoal/pgsql9.5.2/bin/postgres |
| 76 | + 9536.00 1.0% __GI_vfprintf /lib64/libc-2.12.so |
| 77 | + 9160.00 1.0% __strcmp_sse42 /lib64/libc-2.12.so |
| 78 | + 8997.00 1.0% schedule [kernel.kallsyms] |
| 79 | + 8921.00 1.0% __strlen_sse42 /lib64/libc-2.12.so |
| 80 | + 8799.00 0.9% nocachegetattr /home/digoal/pgsql9.5.2/bin/postgres |
| 81 | + 8431.00 0.9% MemoryContextAllocZeroAligned /home/digoal/pgsql9.5.2/bin/postgres |
| 82 | + 8314.00 0.9% expression_tree_walker /home/digoal/pgsql9.5.2/bin/postgres |
| 83 | + 7968.00 0.9% core_yylex /home/digoal/pgsql9.5.2/bin/postgres |
| 84 | + 7193.00 0.8% _bt_compare /home/digoal/pgsql9.5.2/bin/postgres |
| 85 | + 6402.00 0.7% _int_free /lib64/libc-2.12.so |
| 86 | + 6185.00 0.7% memcpy /lib64/libc-2.12.so |
| 87 | + 5988.00 0.6% fmgr_info_cxt_security /home/digoal/pgsql9.5.2/bin/postgres |
| 88 | + 5749.00 0.6% __GI___libc_malloc /lib64/libc-2.12.so |
| 89 | + 5697.00 0.6% PostgresMain /home/digoal/pgsql9.5.2/bin/postgres |
| 90 | + 5444.00 0.6% fmgr_sql /home/digoal/pgsql9.5.2/bin/postgres |
| 91 | + 5372.00 0.6% LWLockRelease /home/digoal/pgsql9.5.2/bin/postgres |
| 92 | + 4917.00 0.5% grouping_planner /home/digoal/pgsql9.5.2/bin/postgres |
| 93 | + 4902.00 0.5% ExecInitExpr /home/digoal/pgsql9.5.2/bin/postgres |
| 94 | + 4626.00 0.5% pfree /home/digoal/pgsql9.5.2/bin/postgres |
| 95 | + 4607.00 0.5% doCustom /home/digoal/pgsql9.5.2/bin/pgbench |
| 96 | + 4537.00 0.5% DirectFunctionCall1Coll /home/digoal/pgsql9.5.2/bin/postgres |
| 97 | + 4521.00 0.5% fget_light [kernel.kallsyms] |
| 98 | + 4329.00 0.5% pqParseInput3 /home/digoal/pgsql9.5.2/lib/libpq.so.5.8 |
| 99 | + 4164.00 0.4% AllocSetFree /home/digoal/pgsql9.5.2/bin/postgres |
| 100 | + 4013.00 0.4% hash_any /home/digoal/pgsql9.5.2/bin/postgres |
| 101 | + 3998.00 0.4% new_list /home/digoal/pgsql9.5.2/bin/postgres |
| 102 | + 3994.00 0.4% do_select [kernel.kallsyms] |
| 103 | + 3653.00 0.4% LockReleaseAll /home/digoal/pgsql9.5.2/bin/postgres |
| 104 | + 3618.00 0.4% hash_search /home/digoal/pgsql9.5.2/bin/postgres |
| 105 | + 3505.00 0.4% palloc0 /home/digoal/pgsql9.5.2/bin/postgres |
| 106 | + 3457.00 0.4% ScanKeywordLookup /home/digoal/pgsql9.5.2/bin/postgres |
| 107 | + 3390.00 0.4% FunctionCall2Coll /home/digoal/pgsql9.5.2/bin/postgres |
| 108 | + 3296.00 0.4% LockAcquireExtended /home/digoal/pgsql9.5.2/bin/postgres |
| 109 | + 3275.00 0.4% __memset_sse2 /lib64/libc-2.12.so |
| 110 | + 3201.00 0.3% __cfree /lib64/libc-2.12.so |
| 111 | + 3125.00 0.3% lappend /home/digoal/pgsql9.5.2/bin/postgres |
| 112 | + 3004.00 0.3% exec_bind_message /home/digoal/pgsql9.5.2/bin/postgres |
| 113 | + 2995.00 0.3% __strcpy_ssse3 /lib64/libc-2.12.so |
| 114 | + 2992.00 0.3% device_not_available [kernel.kallsyms] |
| 115 | +``` |
| 116 | + |
| 117 | +不使用绑定变量 |
| 118 | + |
| 119 | +性能明显下降 |
| 120 | + |
| 121 | +``` |
| 122 | +pgbench -M simple -n -r -P 5 -f ./test.sql -c 64 -j 64 -T 100 |
| 123 | + |
| 124 | +progress: 10.0 s, 480056.6 tps, lat 0.132 ms stddev 0.028 |
| 125 | +progress: 15.0 s, 480355.0 tps, lat 0.132 ms stddev 0.019 |
| 126 | +progress: 20.0 s, 480321.8 tps, lat 0.132 ms stddev 0.020 |
| 127 | +progress: 25.0 s, 480246.2 tps, lat 0.132 ms stddev 0.019 |
| 128 | +progress: 30.0 s, 480274.6 tps, lat 0.132 ms stddev 0.020 |
| 129 | +progress: 35.0 s, 480286.1 tps, lat 0.132 ms stddev 0.018 |
| 130 | +progress: 40.0 s, 480229.3 tps, lat 0.132 ms stddev 0.020 |
| 131 | +progress: 45.0 s, 480095.6 tps, lat 0.132 ms stddev 0.021 |
| 132 | +progress: 50.0 s, 480098.9 tps, lat 0.132 ms stddev 0.020 |
| 133 | +progress: 55.0 s, 480066.5 tps, lat 0.132 ms stddev 0.025 |
| 134 | +progress: 60.0 s, 480148.3 tps, lat 0.132 ms stddev 0.021 |
| 135 | +``` |
| 136 | + |
| 137 | +TOP 调用 |
| 138 | + |
| 139 | +``` |
| 140 | +perf top |
| 141 | + PerfTop: 65503 irqs/sec kernel:12.3% exact: 0.0% [1000Hz cycles], (all, 64 CPUs) |
| 142 | +---------------------------------------------------------------------------------------------- |
| 143 | + samples pcnt function DSO |
| 144 | + _______ _____ ______________________________ ____________________________________ |
| 145 | + 45824.00 4.6% AllocSetAlloc /home/digoal/pgsql9.5.2/bin/postgres |
| 146 | + 38982.00 3.9% base_yyparse /home/digoal/pgsql9.5.2/bin/postgres |
| 147 | + 35333.00 3.6% SearchCatCache /home/digoal/pgsql9.5.2/bin/postgres |
| 148 | + 23770.00 2.4% GetSnapshotData /home/digoal/pgsql9.5.2/bin/postgres |
| 149 | + 12440.00 1.3% palloc /home/digoal/pgsql9.5.2/bin/postgres |
| 150 | + 12092.00 1.2% hash_search_with_hash_value /home/digoal/pgsql9.5.2/bin/postgres |
| 151 | + 12092.00 1.2% _int_malloc /lib64/libc-2.12.so |
| 152 | + 11911.00 1.2% core_yylex /home/digoal/pgsql9.5.2/bin/postgres 上升 |
| 153 | + 11286.00 1.1% LWLockAcquire /home/digoal/pgsql9.5.2/bin/postgres |
| 154 | + 10893.00 1.1% __strcmp_sse42 /lib64/libc-2.12.so |
| 155 | + 10759.00 1.1% MemoryContextAllocZeroAligned /home/digoal/pgsql9.5.2/bin/postgres 上升 |
| 156 | + 9946.00 1.0% expression_tree_walker /home/digoal/pgsql9.5.2/bin/postgres 上升 |
| 157 | + 9175.00 0.9% schedule [kernel.kallsyms] |
| 158 | + 9049.00 0.9% nocachegetattr /home/digoal/pgsql9.5.2/bin/postgres |
| 159 | + 8859.00 0.9% __strlen_sse42 /lib64/libc-2.12.so |
| 160 | + 8020.00 0.8% __GI_vfprintf /lib64/libc-2.12.so |
| 161 | + 7396.00 0.7% _int_free /lib64/libc-2.12.so |
| 162 | + 6847.00 0.7% __GI___libc_malloc /lib64/libc-2.12.so |
| 163 | + 6842.00 0.7% _bt_compare /home/digoal/pgsql9.5.2/bin/postgres |
| 164 | + 6468.00 0.7% grouping_planner /home/digoal/pgsql9.5.2/bin/postgres |
| 165 | + 5468.00 0.6% fmgr_sql /home/digoal/pgsql9.5.2/bin/postgres |
| 166 | + 5403.00 0.5% memcpy /lib64/libc-2.12.so |
| 167 | + 5328.00 0.5% LWLockRelease /home/digoal/pgsql9.5.2/bin/postgres |
| 168 | + 5277.00 0.5% fmgr_info_cxt_security /home/digoal/pgsql9.5.2/bin/postgres |
| 169 | + 5024.00 0.5% ExecInitExpr /home/digoal/pgsql9.5.2/bin/postgres |
| 170 | + 4819.00 0.5% DirectFunctionCall1Coll /home/digoal/pgsql9.5.2/bin/postgres |
| 171 | + 4620.00 0.5% new_list /home/digoal/pgsql9.5.2/bin/postgres |
| 172 | + 4582.00 0.5% fget_light [kernel.kallsyms] |
| 173 | + 4563.00 0.5% ScanKeywordLookup /home/digoal/pgsql9.5.2/bin/postgres |
| 174 | + 4501.00 0.5% doCustom /home/digoal/pgsql9.5.2/bin/pgbench |
| 175 | + 4453.00 0.4% AllocSetFree /home/digoal/pgsql9.5.2/bin/postgres |
| 176 | + 4354.00 0.4% pfree /home/digoal/pgsql9.5.2/bin/postgres |
| 177 | + 4096.00 0.4% pqParseInput3 /home/digoal/pgsql9.5.2/lib/libpq.so.5.8 |
| 178 | + 4050.00 0.4% do_select [kernel.kallsyms] |
| 179 | + 4000.00 0.4% lappend /home/digoal/pgsql9.5.2/bin/postgres |
| 180 | + 3892.00 0.4% hash_any /home/digoal/pgsql9.5.2/bin/postgres |
| 181 | + 3863.00 0.4% __memset_sse2 /lib64/libc-2.12.so |
| 182 | + 3798.00 0.4% expression_tree_mutator /home/digoal/pgsql9.5.2/bin/postgres 下降 |
| 183 | + 3777.00 0.4% palloc0 /home/digoal/pgsql9.5.2/bin/postgres |
| 184 | + 3773.00 0.4% check_stack_depth /home/digoal/pgsql9.5.2/bin/postgres 新增 |
| 185 | + 3643.00 0.4% heap_getsysattr /home/digoal/pgsql9.5.2/bin/postgres 新增 |
| 186 | + 3487.00 0.4% SearchSysCache /home/digoal/pgsql9.5.2/bin/postgres 新增 |
| 187 | + 3485.00 0.4% LockReleaseAll /home/digoal/pgsql9.5.2/bin/postgres |
| 188 | + 3460.00 0.3% eval_const_expressions_mutator /home/digoal/pgsql9.5.2/bin/postgres 新增 |
| 189 | + 3444.00 0.3% FunctionCall2Coll /home/digoal/pgsql9.5.2/bin/postgres |
| 190 | + 3419.00 0.3% __strcpy_ssse3 /lib64/libc-2.12.so |
| 191 | + 3201.00 0.3% LockAcquireExtended /home/digoal/pgsql9.5.2/bin/postgres |
| 192 | +``` |
| 193 | + |
| 194 | + |
| 195 | + |
| 196 | +<a rel="nofollow" href="http://info.flagcounter.com/h9V1" ><img src="http://s03.flagcounter.com/count/h9V1/bg_FFFFFF/txt_000000/border_CCCCCC/columns_2/maxflags_12/viewers_0/labels_0/pageviews_0/flags_0/" alt="Flag Counter" border="0" ></a> |
| 197 | + |
0 commit comments