PostgreSQL成本因子校準(二)

校準參數:sql

  random_page_cost(stap), cpu_index_cost(公式), cpu_operator_cost(公式)數據庫

步驟:緩存

1.爲了方便計算,其它參數常量設爲1:dom

  set random_page_cost=1;ide

  set cpu_tuple_cost=1;oop

  set cpu_index_tuple_cost=1;post

  set cpu_operator_cost=1;spa

  set enable_seqscan=off; set enable_bitmapscan=off;操作系統

  explain (analyze,verbose,costs,buffers,timing) select * from tbl_cost_align where id>1998999963;code

                                                                          QUERY PLAN                              
                                            
------------------------------------------------------------------------------------------------------------------
--------------------------------------------
 Index Scan using tbl_cost_align_id_idx on public.tbl_cost_align  (cost=174.00..20870.00 rows=5205 width=45) (actu
al time=0.076..32827.810 rows=4908 loops=1)
   Output: id, info, crt_time
   Index Cond: (tbl_cost_align.id > 1998999963)
   Buffers: shared hit=113 read=4811
 Planning time: 9.621 ms
 Execution time: 32835.450 ms
(6 rows)
Explain輸出

分析:執行計劃代表這是個索引掃描, 至於掃了多少個數據塊是未知的, 索引的tuples也是未知的, 已知的是cost和rows

20870.00 = blocks*random_page_cost + cpu_tuple_cost*5205+ cpu_index_tuple_cost*? + cpu_operator_cost(1)*?

2.更改cpu_operator_cost

  set cpu_operator_cost=2;

  set enable_seqscan=off; set enable_bitmapscan=off; explain (analyze,verbose,costs,buffers,timing) select * from tbl_cost_align where id>1998999963;

                                                                        QUERY PLAN                                
                                         
------------------------------------------------------------------------------------------------------------------
-----------------------------------------
 Index Scan using tbl_cost_align_id_idx on public.tbl_cost_align  (cost=348.00..26249.00 rows=5205 width=45) (actu
al time=0.032..13.791 rows=4908 loops=1)
   Output: id, info, crt_time
   Index Cond: (tbl_cost_align.id > 1998999963)
   Buffers: shared hit=4924
 Planning time: 0.165 ms
 Execution time: 20.082 ms
(6 rows)
Explain輸出

分析:26249.00 = blocks*random_page_cost + cpu_tuple_cost*5205+ cpu_index_tuple_cost*? + cpu_operator_cost(2)*?,獲得本例經過索引掃描的條數:26249.00-20870.00=5379

等式變爲20870.00 = blocks*random_page_cost + cpu_tuple_cost*5205+ cpu_index_tuple_cost*5205 + cpu_operator_cost(1)*5379

3.一樣的方法求blocks

  set random_page_cost = 2;

set enable_seqscan=off; set enable_bitmapscan=off; explain (analyze,verbose,costs,buffers,timing) select * from tbl_cost_align where id>1998999963;

                                                                        QUERY PLAN                                
                                         
------------------------------------------------------------------------------------------------------------------
-----------------------------------------
 Index Scan using tbl_cost_align_id_idx on public.tbl_cost_align  (cost=348.00..31330.00 rows=5205 width=45) (actu
al time=0.032..13.154 rows=4908 loops=1)
   Output: id, info, crt_time
   Index Cond: (tbl_cost_align.id > 1998999963)
   Buffers: shared hit=4924
 Planning time: 0.165 ms
 Execution time: 19.309 ms
(6 rows)
Explain

blocks=31330.00-26249.00=5081.00

更新公式:20870.00 = 5081*random_page_cost + cpu_tuple_cost*5205+ cpu_index_tuple_cost*5205 + cpu_operator_cost*5379

4.經過stap統計random_page_cost

  CHECKPOINT關閉數據庫;

  將操做系統的緩存刷入硬盤  sync; echo 3 > /proc/sys/vm/drop_caches

  以非0號cpu親和啓動數據庫 taskset -c 1 /usr/local/pgsql/bin/postgres >/dev/null 2>&1

  鏈接數據庫,查看pid:select pg_backend_pid();

  啓動監控代碼:同上

  執行:set enable_seqscan=off; set enable_bitmapscan=off; explain (analyze,verbose,costs,buffers,timing) select * from tbl_cost_align where id>1998999963;

  查看stap及explain輸出:

13094**4854**8752355
query__done explain (analyze,verbose,costs,buffers,timing) select * from tbl_cost_align where
id>1998999963;pid:13094
     value |-------------------------------------------------- count
      2048 |                                                      0
      4096 |                                                      0
      8192 |                                                      6
     16384 |                                                     27
     32768 |                                                      7
     65536 |                                                      3
    131072 |                                                      0
    262144 |                                                     21
    524288 |                                                      2
   1048576 |                                                      9
   2097152 |@@@@@@@@@                                           486
   4194304 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@                1782
   8388608 |@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@  2495
  16777216 |                                                      8
  33554432 |                                                      4
  67108864 |                                                      2
 134217728 |                                                      1
 268435456 |                                                      1
 536870912 |                                                      0
1073741824 |                                                      0
stap輸出
                                                                         QUERY PLAN                               
                                          
------------------------------------------------------------------------------------------------------------------
------------------------------------------
 Index Scan using tbl_cost_align_id_idx on public.tbl_cost_align  (cost=0.43..20415.52 rows=5205 width=45) (actual
 time=6.920..42273.537 rows=4908 loops=1)
   Output: id, info, crt_time
   Index Cond: (tbl_cost_align.id > 1998999963)
   Buffers: shared hit=110 read=4814
 Planning time: 148.140 ms
 Execution time: 42282.361 ms
(6 rows)
explain輸出

獲得random_page_cost=8.752355

5.更新等式

 42273.537=5081*8.752355+0.00158093450574257426*5205+cpu_index_tuple_cost*5205 + cpu_operator_cost*5379

  cpu_tuple_cost使用上面獲得的精確值

   cpu_index_tuple_cost和cpu_operator_cost的比例用系統默認的2 : 1-cpu_index_tuple_cost/cpu_operator_cost = 2,獲得準確值:

  cpu_operator_cost=-0.1576529787048674

相關文章
相關標籤/搜索