Received: from ll.mit.edu (LLMAIL.LL.MIT.EDU [129.55.12.40]) by postgresql.org (8.11.3/8.11.1) with ESMTP id f51Gb4E67210 for ; Fri, 1 Jun 2001 12:37:04 -0400 (EDT) (envelope-from gry@ll.mit.edu) Received: (from smtp@localhost) by ll.mit.edu (8.11.3/8.8.8) id f51GaqG29815 for ; Fri, 1 Jun 2001 12:36:52 -0400 (EDT) Received: from UNKNOWN( ), claiming to be "soi.llan" via SMTP by llmail, id smtpdAAA3Ma4Y4; Fri Jun 1 12:36:42 2001 Received: from sty (sty [ ]) by soi.llan (8.8.0/8.8.0) with SMTP id MAA22632 for ; Fri, 1 Jun 2001 12:36:41 -0400 (EDT) Date: Fri, 1 Jun 2001 12:36:41 -0400 From: george young To: pgsql-sql@postgresql.org Subject: select distinct too slow Message-Id: <20010601123641.4c0d96c0.gry@ll.mit.edu> X-Mailer: Sylpheed version 0.4.62 (GTK+ 1.2.8; i586-pc-linux-gnu) Organization: MIT Lincoln Laboratory Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Archive-Number: 200106/7 X-Sequence-Number: 3210 [postgresql 7.1.3, Sun ultrasparc, Solaris 2.5.1] create table myjunk(run_name text,wafer_num int2,pass int2,test_type text,yloc int2,xloc int2,test_num int,test_data float8,comments text,primary key(run_name,wafer_num,yloc,xloc,test_type,test_num)) [insert 240765 rows] vacuum analyze myjunk; There are only 3 distinct values of run_name right now. select distinct run_name from myjunk; [takes ~ 14 seconds] explain select distinct run_name from myjunk; Unique (cost=34896.75..35498.66 rows=24076 width=12) -> Sort (cost=34896.75..34896.75 rows=240765 width=12) -> Seq Scan on myjunk (cost=0.00..6785.65 rows=240765 width=12) How can I make this query be fast? I tried adding an index on run_name alone, that did not help. I guess I can keep a separate table with unique run_name's... sounds kind of ugly. If I did this, what sort of trigger or rule could maintain it automatically -- and how could it be done without grossly slowing down large data loads into that table? -- Your mouse has moved. Windows NT must be restarted for the change to take effect. Reboot now? [OK]