[pgsql-jp: 28811] Re: 集計処理を快適にしたい

桝形 誠二 s.masugata @ digicom.dnp.co.jp
2003年 1月 28日 (火) 18:58:58 JST


桝形です。

DVさんがお答えになられた、

> > ご質問のクエリですが、こんなのではどうでしょう?
> > 
> > select no2, count(distinct no1) from test4
> > group by no2
> > order by 2 desc
> > limit 5

EXPLAIN実行結果は、

 Limit  (cost=34514.75..34514.76 rows=5 width=10)
   ->  Sort  (cost=34514.75..34764.75 rows=100000 width=10)
         Sort Key: count(DISTINCT no1)
         ->  Aggregate  (cost=0.00..25115.00 rows=100000 width=10)
               ->  Group  (cost=0.00..22615.00 rows=1000000 width=10)
                     ->  Index Scan using test4_no2_idndex_key on test4  (cost=0.00..20115.00 rows=1000000 width=10)
(6 rows)

となり、駄目なようです。
実際にデータを取得すると80秒くらいかかりました。

また、しょうなりさんがお答えになられた、

> SELECT no2, count(*) as cnt FROM test4 group by no2
>  order by count(*) desc offset 0 limit 5
> 
> で、どうでしょうか?
> 100万件はさすがに試してませんが。

EXPLAIN実行結果は、

 Limit  (cost=34261.60..34261.61 rows=5 width=6)
   ->  Sort  (cost=34261.60..34511.60 rows=100000 width=6)
         Sort Key: count(*)
         ->  Aggregate  (cost=0.00..25115.00 rows=100000 width=6)
               ->  Group  (cost=0.00..22615.00 rows=1000000 width=6)
                     ->  Index Scan using test4_no2_idndex_key on test4  (cost=0.00..20115.00 rows=1000000 width=6)
(6 rows)

となり、駄目なようです。
実際にデータを取得すると50秒くらいかかりました。

下記( 最初 )のクエリでは、

select * from (
               select no2, count( * ) as cnt from test4
               group by no2 offset 0 limit 5
              ) as test
order by cnt desc;

30秒くらいかかって、今のところ一番よい結果を出しています。

せっかく返答して頂いたのに、よい報告を出せずにすみません。
_______________________

   桝形 誠二( Masugata Seiji )
   E-Mail : s.masugata @ digicom.dnp.co.jp

 ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄





pgsql-jp メーリングリストの案内