2

次のクエリは、postgresql 9.1 で約 300 ~ 400 ミリ秒かかります。テーブルには最大 2M 行が含まれています。このパフォーマンスは正当化されますか? 改善できますか?

SELECT "Products"."Id"
      , "Products"."Title"
      , "Products"."ThumbHeight"
      , "Products"."LargeImageWidth"
      , "Products"."LargeImageHeight"
      , "Products"."Url"
      , "Products"."BrowseNodeId"
FROM "Products"
WHERE  "Products"."Id" = ANY(ARRAY(SELECT (random()*2233071)::int
                FROM generate_series(1, 100)));

そして、ここに説明計画があります:

--------------------------------------------------------------------------------
 Bitmap Heap Scan on "Products"  (cost=60.48..100.46 rows=10 width=268)
   Recheck Cond: ("Id" = ANY ($0))
   InitPlan 1 (returns $0)
     ->  Function Scan on generate_series  (cost=0.00..17.50 rows=1000 width=0)
   ->  Bitmap Index Scan on "Products_pkey"  (cost=0.00..42.97 rows=10 width=0)
     Index Cond: ("Id" = ANY ($0))

説明分析:

Bitmap Heap Scan on "Products"  (cost=60.48..100.46 rows=10 width=268) (actual time=77.702..80.944 rows=100 loops=1)
   Recheck Cond: ("Id" = ANY ($0))
   InitPlan 1 (returns $0)
     ->  Function Scan on generate_series  (cost=0.00..17.50 rows=1000 width=0) (actual time=0.097..0.348 rows=100 loops=1)
   ->  Bitmap Index Scan on "Products_pkey"  (cost=0.00..42.97 rows=10 width=0) (actual time=77.601..77.601 rows=104 loops=1)
         Index Cond: ("Id" = ANY ($0))
 Total runtime: 81.409 ms

Id は主キーです。 "Products_pkey" PRIMARY KEY, btree ("Id")

ありがとう!

4

2 に答える 2

1

クエリと比較してこれを試してください。

SELECT "Products"."Id"
      , "Products"."Title"
      , "Products"."ThumbHeight"
      , "Products"."LargeImageWidth"
      , "Products"."LargeImageHeight"
      , "Products"."Url"
      , "Products"."BrowseNodeId"
FROM "Products"
ORDER BY random()
LIMIT 100
于 2012-05-30T14:48:28.303 に答える
0

これが私のユースケースに適したソリューションです(Webページに100個のランダムな製品を選択します):

  1. テーブルを複製する
  2. 表の行をシャッフルする
  3. 自動インクリメント列を追加
  4. ランダムな範囲から選択 (例: 100-200、1567000-1567100)

クエリ時間は2 ミリ秒未満になりました。

これが私が使用した一連のコマンドです。

create table RandProducts as select * from "Products" order by random();
alter table RandProducts add column RandId serial8;
create index on RandProducts(randid);

そして、100 のランダムな行を取得するには、次のようにします。

select * from Products where RandId between 8000 and 8100;
于 2012-05-31T05:15:52.400 に答える