Received: from maia.hub.org (maia-2.hub.org [200.46.204.251]) by mail.postgresql.org (Postfix) with ESMTP id 072E31337B61 for ; Sun, 5 Dec 2010 12:48:22 -0400 (AST) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.251]) (amavisd-maia, port 10024) with ESMTP id 93572-04 for ; Sun, 5 Dec 2010 16:48:15 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from out.mail.amis.hr (out1.mail.amis.hr [188.129.0.4]) by mail.postgresql.org (Postfix) with ESMTP id C09711337B5B for ; Sun, 5 Dec 2010 12:48:14 -0400 (AST) Received: from out.fsec.sm.srv.amis.hr (out.fsec.sm.srv.amis.hr [192.168.106.22]) by out.mail.amis.hr (Postfix) with ESMTP id 05AA020057 for ; Sun, 5 Dec 2010 17:48:11 +0100 (CET) Received: from pps.filterd (out [127.0.0.1]) by out.fsec.sm.srv.amis.hr (8.14.3/8.14.3) with SMTP id oB5GlbDb014339 for ; Sun, 5 Dec 2010 17:48:10 +0100 Received: from smtp.amis.hr (smtp.sm.srv.amis.hr [192.168.106.39]) by out.fsec.sm.srv.amis.hr with ESMTP id syk2b8bp5-1 for ; Sun, 05 Dec 2010 17:48:10 +0100 A-Original-Client-UID: 188.129.106.69 splivalo Received: from [192.168.10.1] (cpe-188-129-106-69.dynamic.amis.hr [188.129.106.69]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) (Authenticated sender: splivalo) by smtp.amis.hr (Postfix) with ESMTPSA id 6A2D4273 for ; Sun, 5 Dec 2010 17:48:10 +0100 (CET) Message-ID: <4CFBC24A.90708@megafon.hr> Date: Sun, 05 Dec 2010 17:48:10 +0100 From: Mario Splivalo User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.1.15) Gecko/20101027 Lightning/1.0b1 Thunderbird/3.0.10 MIME-Version: 1.0 To: pgsql-sql@postgresql.org Subject: Re: Get the max viewd product_id for user_id References: <4CF8CC26.70206@megafon.hr> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:5.2.15, 1.0.148, 0.0.0000 definitions=2010-12-05_06:2010-12-04, 2010-12-05, 1970-01-01 signatures=0 X-Proofpoint-Spam-Details: rule=notspam policy=default score=0 spamscore=0 ipscore=0 suspectscore=1 phishscore=0 bulkscore=0 adultscore=0 classifier=spam adjust=0 reason=mlx engine=6.0.2-1010190000 definitions=main-1012050078 X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.9 tagged_above=-5 required=5 tests=BAYES_00=-1.9 X-Spam-Level: X-Archive-Number: 201012/25 X-Sequence-Number: 34861 On 12/03/2010 12:40 PM, Jayadevan M wrote: > Hello, > >> I went this way, but for a large number of user_id's, it's quite slow: >> >> CREATE VIEW v_views AS >> SELECT user_id, product_id, count(*) as views >> FROM viewlog >> GROUP BY user_id, product_id >> >> SELECT >> DISTINCT user_id, >> (SELECT product_id FROM v_views inn WHERE inn.user_id = out.user_id >> ORDER BY views DESC LIMIT 1) as product_id, >> (SELECT views FROM v_views inn WHERE inn.user_id = out.user_id ORDER > BY >> views DESC LIMIT 1) as views >> FROM >> v_views out >> > Does this work faster? > select x.user_id,y.product_id,x.count from > (select user_id, max(count ) as count from (select user_id,product_id, > count(*) as count from viewlog group by user_id,product_id) as x group by > user_id > ) as x inner join > (select user_id,product_id, count(*) as count1 from viewlog group by > user_id,product_id ) as y > on x.user_id=y.user_id and x.count=y.count1 > It does, yes. Actually, pretty silly of me not to implement it that way, thank you. Since I already have the view, the query now looks like this: select x.user_id, y.product_id, x.views from ( select user_id, max(views) as views from v_views group by user_id ) as x inner join v_views as y on x.user_id=y.user_id and x.views=y.views And CTEs would also help here :) Mario