X-Original-To: pgsql-performance-postgresql.org@localhost.postgresql.org Received: from localhost (av.hub.org [200.46.204.144]) by postgresql.org (Postfix) with ESMTP id 97A6011F6CE6 for ; Mon, 10 Apr 2006 06:24:05 -0300 (ADT) X-Virus-Scanned: by amavisd-new at hub.org X-Spam-Score: 0.12 X-Spam-Level: X-Spam-Status: No, score=0.12 required=5 tests=[AWL=0.120] Received: from postgresql.org ([200.46.204.71]) by localhost (av.hub.org [200.46.204.144]) (amavisd-new, port 10024) with ESMTP id hIoM7ZLuHGBY for ; Mon, 10 Apr 2006 06:23:57 -0300 (ADT) X-Greylist: from auto-whitelisted by SQLgrey- Received: from pih-relay05.plus.net (pih-relay05.plus.net [212.159.14.132]) by postgresql.org (Postfix) with ESMTP id C33D511F6931 for ; Mon, 10 Apr 2006 06:23:59 -0300 (ADT) Received: from [213.162.97.75] (helo=mail.metronet.co.uk) by pih-relay05.plus.net with esmtp (Exim) id 1FSsck-00023r-0O; Mon, 10 Apr 2006 10:23:54 +0100 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 D413A736BE; Mon, 10 Apr 2006 10:25:19 +0100 (BST) Received: from [192.168.1.17] (client17.office.archonet.com [192.168.1.17]) by mainbox.archonet.com (Postfix) with ESMTP id 2ED4E15EA4; Mon, 10 Apr 2006 10:23:49 +0100 (BST) Message-ID: <443A2424.4020702@archonet.com> Date: Mon, 10 Apr 2006 10:23:48 +0100 From: Richard Huxton User-Agent: Thunderbird 1.5 (X11/20051201) MIME-Version: 1.0 To: Brendan Duddridge Cc: PostgreSQL Performance Subject: Re: OT: Data structure design question: How do they count References: In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Archive-Number: 200604/199 X-Sequence-Number: 18283 Brendan Duddridge wrote: > > Now, initially I thought they would just pre-compute these counts, but > the problem is, when you click on any of the above attribute values, > they reduce the remaining possible set of matching products (and set of > possible remaining attributes and attribute values) by the amount > displayed next to the attribute value selected. You can click on any > combination of attribute values to filter down the remaining set of > matching products, so there's a large combination of paths you can take > to arrive at a set of products you might be interested in. > > Do you think they are pre-computed? Or do you think they might use a > query similar to the following?: Pre-computed almost certainly, but at what level of granularity? And with application-level caching? > select pav.attribute_value_id, count(p.product_id) > from product_attribute_value pav, > attribute a, > product p > where a.attribute_id in (some set of attribute ids) and > pav.product_id = p.product_id and > pav.attribute_id = a.attribute_id and p.product_id in > (select product_id > from category_product > where category_id = some category id) and > p.is_active = 'true' > group by pav.attribute_value_id; > > It would seem to me that although the above query suggests a normalized > database structure, that joining with 3 tables plus a 4th table in the > sub-query with an IN qualifier and grouping to get the product counts > would take a VERY long time, especially on a possible result set of > 1,260,658 products. Hmm - I'm not sure I'd say this was necessarily normalised. In the example you gave there were three definite types of attribute: 1. Price range (< 20, 20-50, ...) 2. Product type (lighting, rugs, ...) 3. Store (art.com, homeannex, ...) Your example discards this type information. I'm also not sure it lets store A sell widgets for 19.99 and B for 25.99 So - let's look at how we might break this down into simple relations: product_types (product_id, prod_type, prod_subtype) product_availability (product_id, store_id, price_range) and so on for each set of parameters. Then, if PG isn't calculating fast enough I'd be tempted to throw in a summary table: product_counts(store_id, price_range, prod_type, prod_subtype, ..., num_products) Then total over this for the top-level queries. I'd also cache common top-level queries at the applicaton level anyway. -- Richard Huxton Archonet Ltd