X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [200.46.204.2]) by svr1.postgresql.org (Postfix) with ESMTP id 3CB01D1E0FD for ; Tue, 6 Apr 2004 17:25:59 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (neptune.hub.org [200.46.204.2]) (amavisd-new, port 10024) with ESMTP id 33440-10 for ; Tue, 6 Apr 2004 17:25:58 -0300 (ADT) Received: from mail4.speakeasy.net (mail4.speakeasy.net [216.254.0.204]) by svr1.postgresql.org (Postfix) with ESMTP id 6450CD1DB0E for ; Tue, 6 Apr 2004 17:25:55 -0300 (ADT) Received: (qmail 27061 invoked from network); 6 Apr 2004 20:25:56 -0000 Received: from dsl093-130-165.sfo4.dsl.speakeasy.net (HELO speakeasy.org) (kgeis@[66.93.130.165]) (envelope-sender ) by mail4.speakeasy.net (qmail-ldap-1.03) with AES256-SHA encrypted SMTP for ; 6 Apr 2004 20:25:56 -0000 Message-ID: <40731252.2030002@speakeasy.org> Date: Tue, 06 Apr 2004 13:25:54 -0700 From: Ken Geis User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.5) Gecko/20031007 X-Accept-Language: en-us, en MIME-Version: 1.0 To: pgsql-performance@postgresql.org Subject: plan problem Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200404/70 X-Sequence-Number: 6370 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. Ken