X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by svr1.postgresql.org (Postfix) with ESMTP id 4D3FBD709F for ; Tue, 11 Oct 2005 09:32:16 -0300 (ADT) Received: from svr1.postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id 86688-08 for ; Tue, 11 Oct 2005 12:31:51 +0000 (GMT) Received: from mail.metronet.co.uk (mail.metronet.co.uk [213.162.97.75]) by svr1.postgresql.org (Postfix) with ESMTP id E1500D7124 for ; Tue, 11 Oct 2005 09:31:52 -0300 (ADT) Received: from mainbox.archonet.com (84-51-143-99.archon037.adsl.metronet.co.uk [84.51.143.99]) by smtp.metronet.co.uk (MetroNet Mail) with ESMTP id 6C570431672; Tue, 11 Oct 2005 13:31:08 +0100 (BST) Received: from localhost (localhost.localdomain [127.0.0.1]) by mainbox.archonet.com (Postfix) with ESMTP id 16DAC15EDB; Tue, 11 Oct 2005 13:29:59 +0100 (BST) Received: from mainbox.archonet.com ([127.0.0.1]) by localhost (mainbox [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 16223-02; Tue, 11 Oct 2005 13:29:55 +0100 (BST) Received: from [192.168.1.17] (client17.office.archonet.com [192.168.1.17]) by mainbox.archonet.com (Postfix) with ESMTP id 07DAD15ED5; Tue, 11 Oct 2005 13:29:54 +0100 (BST) Message-ID: <434BB042.7000309@archonet.com> Date: Tue, 11 Oct 2005 13:29:54 +0100 From: Richard Huxton User-Agent: Thunderbird 1.4.1 (X11/20051006) MIME-Version: 1.0 To: Frank Bax Cc: pgsql-sql@postgresql.org Subject: Re: SEVEN cross joins?!?!? References: <5.2.1.1.0.20051011071510.0364d850@pop6.sympatico.ca> In-Reply-To: <5.2.1.1.0.20051011071510.0364d850@pop6.sympatico.ca> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Status: No, hits=0.486 required=5 tests=[AWL=-0.421, FORGED_RCVD_HELO=0.05, MANY_EXCLAMATIONS=0, PLING_QUERY=0.857] X-Spam-Level: X-Archive-Number: 200510/83 X-Sequence-Number: 22936 Frank Bax wrote: > I have a table with only 434 rows in it. Two important columns are > "itemid" and "locn". Each item must be in one of seven locations. We > need to create a "combo" by selecting one item from each of seven > locations; then determine which "combo" is the "best" according to our > analysis (see below). > > A subselect for items in a location looks something like: > (select * from suit_item where locn='Head' AND username='Walter' ORDER > BY itemid LIMIT 10) as Head > > One subselect for each location, cross join them all and the query > generates 10,000,000 combinations! Without the "LIMIT 10", there are > 78 * 37 * 91 * 81 * 99 * 47 * 1 = 98,981,901,018 results returned for > username='Walter' (the only user at the moment). The large volume is > causing a problem for my systems! The "ORDER BY itemid" was added only > so that same 10 items were processed on different computer systems I > tested this query on. Only one item for 7th locn in the database at the > moment. Frank - it might just be me, but I've read your email twice and despite all the information I still don't have any idea what you are trying to do. Are you saying that you WANT to generate a cross-join, score the millions of results and then pick the best 10? It's doing what you want, but you'd like it to be faster. Or are you saying that you'd like to avoid the explosion in rows altogether? In either case - I don't suppose you could provide a real example of the query, so we can see exactly what you're trying to do. -- Richard Huxton Archonet Ltd