パスワードを忘れた? アカウント作成
660108 journal

dubwiseの日記: PostgreSQLのTips

日記 by dubwise

「このページはアレゲ流免許皆伝の忍者会の dubwise (17876)用です.」

■ Max関数が遅い
http://ml.postgresql.jp/pipermail/pgsql-jp/2003-September/006284.html

プライマリキーの MAX をとるのに、select max(hoge) from table のSQLを、
explain で確認すると、インデックスを使わず、シーケンシャルに読んでいる。

=# explain select max(mas_no) from g_master;
NOTICE: QUERY PLAN:

Aggregate (cost=14737.84..14737.84 rows=1 width=4)
    -> Seq Scan on g_master (cost=0.00..14609.27 rows=51427 width=4)

これは PostgreSQL の仕様らしい。
以下のSQLで行うと、インデックスを使うので速くなる。

>select mas_no from g_master order by mas_no desc offset 0 limit 1;

Limit (cost=0.00..3.24 rows=1 width=4)
    -> Index Scan Backward using g_master_pkey on g_master (cost=0.00..166442.75 rows=51427 width=4)

この議論は賞味期限が切れたので、アーカイブ化されています。 新たにコメントを付けることはできません。
typodupeerror

物事のやり方は一つではない -- Perlな人

読み込み中...