Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 15th, 2012  |  syntax: None  |  size: 1.75 KB  |  hits: 10  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. postgresql fetch 100 random rows
  2. SELECT "Products"."Id"
  3.       , "Products"."Title"
  4.       , "Products"."ThumbHeight"
  5.       , "Products"."LargeImageWidth"
  6.       , "Products"."LargeImageHeight"
  7.       , "Products"."Url"
  8.       , "Products"."BrowseNodeId"
  9. FROM "Products"
  10. WHERE  "Products"."Id" = ANY(ARRAY(SELECT (random()*2233071)::int
  11.                 FROM generate_series(1, 100)));
  12.        
  13. --------------------------------------------------------------------------------
  14.  Bitmap Heap Scan on "Products"  (cost=60.48..100.46 rows=10 width=268)
  15.    Recheck Cond: ("Id" = ANY ($0))
  16.    InitPlan 1 (returns $0)
  17.      ->  Function Scan on generate_series  (cost=0.00..17.50 rows=1000 width=0)
  18.    ->  Bitmap Index Scan on "Products_pkey"  (cost=0.00..42.97 rows=10 width=0)
  19.      Index Cond: ("Id" = ANY ($0))
  20.        
  21. Bitmap Heap Scan on "Products"  (cost=60.48..100.46 rows=10 width=268) (actual time=77.702..80.944 rows=100 loops=1)
  22.    Recheck Cond: ("Id" = ANY ($0))
  23.    InitPlan 1 (returns $0)
  24.      ->  Function Scan on generate_series  (cost=0.00..17.50 rows=1000 width=0) (actual time=0.097..0.348 rows=100 loops=1)
  25.    ->  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)
  26.          Index Cond: ("Id" = ANY ($0))
  27.  Total runtime: 81.409 ms
  28.        
  29. SELECT "Products"."Id"
  30.       , "Products"."Title"
  31.       , "Products"."ThumbHeight"
  32.       , "Products"."LargeImageWidth"
  33.       , "Products"."LargeImageHeight"
  34.       , "Products"."Url"
  35.       , "Products"."BrowseNodeId"
  36. FROM "Products"
  37. ORDER BY random()
  38. LIMIT 100
  39.        
  40. create table RandProducts as select * from "Products" order by random();
  41. alter table RandProducts add column RandId serial8;
  42. create index on RandProducts(randid);
  43.        
  44. select * from Products where RandId between 8000 and 8100;