I am trying to find an efficient way to draw a random sample from a complex query. I also want it to be easy to use within my application.

So I've defined a view that encapsulates the query. The id in the "driving" table is exposed, and I run a query like:

select * from stats_record_view
 where id in (select id from driver_stats
               order by random()
               limit 30000);

driver_stats.id is unique, the primary key. The problem I'm having is that neither the ORDER BY nor the LIMIT change the uniqueness of that column, but the planner doesn't know that. It does a HashAggregate to make sure the results are unique. It thinks that 200 rows will come out of that operation, and then 200 rows is small enough that it thinks a Nested Loop is the best way to proceed from there.

I can post more query plan, but I don't think it would be that very helpful. I'm considering just making a sample table and creating an analogous view around that. I'd like to be able to keep this as simple as possible though.


---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
   (send "unregister YourEmailAddressHere" to [EMAIL PROTECTED])

Reply via email to