Received: from localhost (postgresql.org [64.49.215.8]) by postgresql.org (Postfix) with ESMTP id 59E2A4764A1 for ; Thu, 21 Nov 2002 09:24:01 -0500 (EST) Received: from mailstore-1.mail.knowledge.com (frida.london.knowtion.net [213.170.2.69]) by postgresql.org (Postfix) with ESMTP id E1B28476010 for ; Thu, 21 Nov 2002 09:22:48 -0500 (EST) Received: from [213.155.153.61] (helo=petergalbavydes) by mailstore-1.mail.knowledge.com with asmtp (Exim 3.33 #1) id 18EsEG-0007xO-00 for pgsql-sql@postgresql.org; Thu, 21 Nov 2002 14:22:52 +0000 Message-ID: <001d01c29169$79fd1d20$4528a8c0@cblan.mblox.com> From: "Peter Galbavy" To: Subject: help optimise this ? Date: Thu, 21 Nov 2002 14:22:51 -0000 Organization: Knowtion Ltd. MIME-Version: 1.0 Content-Type: text/plain; charset="Windows-1252" Content-Transfer-Encoding: 7bit X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 6.00.2800.1106 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106 X-Virus-Scanned: by AMaViS new-20020517 X-Archive-Number: 200211/328 X-Sequence-Number: 10470 I have a table of image 'instances' where the columns include: md5 char(32), -- the 'original' image md5 key file_md5 char(32) primary key, -- the md5 of each version of an image image_width int, image_length int I want to then find either the largest (max) or smallest (min) version of an image that falls within some range of sizes: e.g. select file_md5 from image_instance where image_width = (select min(image_width) from image_instance where md5 = '546b94e94851a56ee721f3b755f58462') and image_length = (select min(image_length) from image_instance where md5 = '546b94e94851a56ee721f3b755f58462') and md5 = '546b94e94851a56ee721f3b755f58462' and image_width between 0 and 160 and image_length between 0 and 160; Now, having to do three selects on 'md5' to limit the search seems a little unoptimal to me. Note that the test tables are small and I have no other indexes apart from the 'primary key' constraint yet - this is not my primary concern at this point, I would just like cleaner SQL. All I want back is (for some definition) the 'file_md5' that best matches my min/max criteria. I have not - and will leave for now - the case where a cropped image results in a scale change between width and length such that the min/max test returns a different set of rows for each dimension. Argh. And help given is greatly appreciated. rgds, -- Peter