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)
PostgreSQLのTips More ログイン