pgstattuple2: デッドタプル推定のための統計的手法

11
Copyright (c) 2013 Uptime Technologies, LLC. All rights reserved. 1 pgstattuple2: デッドタプル推定のための統計的手法 2013/7/13 アップタイム・テクノロジーズ 永安 悟史

description

2013/7/13に開催されたPostgreSQLアンカンファレンスにおけるセッション「pgstattuple2: デッドタプル推定のための統計的手法」の資料です。

Transcript of pgstattuple2: デッドタプル推定のための統計的手法

Page 1: pgstattuple2: デッドタプル推定のための統計的手法

Copyright (c) 2013 Uptime Technologies, LLC. All rights reserved. 1

pgstattuple2:デッドタプル推定のための統計的手法

2013/7/13アップタイム・テクノロジーズ

永安 悟史

Page 2: pgstattuple2: デッドタプル推定のための統計的手法

Copyright (c) 2013 Uptime Technologies, LLC. All rights reserved. 2

モチベーション

• PostgreSQLはVACUUMによるメンテナンスが必要

• その契機のためにpgstattuple/pgstatindexによる調査が必要

• データ量が増大する昨今、pgstattuple/pgstatindexの発生させるI/Oは無視できない

• 調査によるパフォーマンスインパクトを減らしたい

Page 3: pgstattuple2: デッドタプル推定のための統計的手法

統計的手法の検討

• 本当にすべてのブロックをスキャンする必要があるのか?

• ブロックサンプリングの可能性を検討したい

• Robert Haasにも「それは便利だと思う」とコメントされた– Re: [RFC] pgstattuple/pgstatindex enhancement– http://bit.ly/18HdrIH

• 最近、統計を勉強してる

Copyright (c) 2013 Uptime Technologies, LLC. All rights reserved. 3

Page 4: pgstattuple2: デッドタプル推定のための統計的手法

デッドタプル(削除済みタプル)の推定

• デッドタプルの割合を推定したい(0~100%)

• ランダムにある程度のブロックをサンプリングする

• サンプルからテーブル全体のデッドタプル率を推定する

• 誤差をどう考えるか、どの程度サンプリングすればよいか

Copyright (c) 2013 Uptime Technologies, LLC. All rights reserved. 4

Page 5: pgstattuple2: デッドタプル推定のための統計的手法

統計的誤差

• サンプリングを行って母集団全体の推定を行うと、そこには統計的誤差が生じる

• 誤差を許容範囲内に収めるようなサンプリングを行う必要がある

• サンプルから推定した母平均は、95%の確率で「標本平均±標準誤差×1.96」に収まる

• 誤差は「母集団の数N、標本サイズn、標本標準偏差s」で決まる

Copyright (c) 2013 Uptime Technologies, LLC. All rights reserved. 5

標準誤差SE = 標準誤差 – Wikipediahttp://ja.wikipedia.org/wiki/標準誤差

Page 6: pgstattuple2: デッドタプル推定のための統計的手法

サンプルサイズと統計的誤差

• テーブル100万ブロック、デッドタプル平均20%、標準偏差20%の場合– 3000ブロックサンプルすると、誤差は±3.6%程度

Copyright (c) 2013 Uptime Technologies, LLC. All rights reserved. 6

Page 7: pgstattuple2: デッドタプル推定のための統計的手法

pgstattuple2

• pgstattuple2(regclass)– 3,000ブロック(=24MB)をランダムにサンプリング

• 使い方はpgstattuple()と同等– 標準誤差の数値をNOTICEとして出力する

Copyright (c) 2013 Uptime Technologies, LLC. All rights reserved. 7

postgres=# select * from pgstattuple2('pgbench_accounts');NOTICE: pgstattuple2: SE tuple_count 1297.31, tuple_len 156974.32, dead_tuple_count185.78, dead_tuple_len 22479.30, free_space 162814.40-[ RECORD 1 ]------+-----------table_len | 1394712576tuple_count | 10002023tuple_len | 1210244812tuple_percent | 86.77dead_tuple_count | 171785dead_tuple_len | 20786018dead_tuple_percent | 1.49free_space | 21401483free_percent | 1.53

Page 8: pgstattuple2: デッドタプル推定のための統計的手法

実測結果の比較

• 1.3GB(166,559ブロック)のうち、約1.8%をサンプリング– 推定精度は実用上十分と思われる。

– 実行時間は1/100程度(1,600ms→20ms)

Copyright (c) 2013 Uptime Technologies, LLC. All rights reserved. 8

※SF=100, CL=32, TX=5000, autovacuum=off

Page 9: pgstattuple2: デッドタプル推定のための統計的手法

Now available on Github!

• https://github.com/snaga/postgres

Copyright (c) 2013 Uptime Technologies, LLC. All rights reserved. 9

Page 10: pgstattuple2: デッドタプル推定のための統計的手法

Copyright (c) 2013 Uptime Technologies, LLC. All rights reserved. 10

Q&A

Page 11: pgstattuple2: デッドタプル推定のための統計的手法

Copyright (c) 2013 Uptime Technologies, LLC. All rights reserved. 11

【お問い合わせ先】アップタイム・テクノロジーズ合同会社担当:永安E-mail: [email protected]: http://www.uptime.jp/