Return-Path: X-Original-To: apmail-couchdb-user-archive@www.apache.org Delivered-To: apmail-couchdb-user-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 322009C20 for ; Sun, 15 Apr 2012 09:01:08 +0000 (UTC) Received: (qmail 62098 invoked by uid 500); 15 Apr 2012 09:01:06 -0000 Delivered-To: apmail-couchdb-user-archive@couchdb.apache.org Received: (qmail 62064 invoked by uid 500); 15 Apr 2012 09:01:06 -0000 Mailing-List: contact user-help@couchdb.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: user@couchdb.apache.org Delivered-To: mailing list user@couchdb.apache.org Received: (qmail 62039 invoked by uid 99); 15 Apr 2012 09:01:06 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 15 Apr 2012 09:01:06 +0000 X-ASF-Spam-Status: No, hits=1.5 required=5.0 tests=HTML_MESSAGE,RCVD_IN_DNSWL_LOW,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (nike.apache.org: domain of alon.keren@gmail.com designates 209.85.212.52 as permitted sender) Received: from [209.85.212.52] (HELO mail-vb0-f52.google.com) (209.85.212.52) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 15 Apr 2012 09:00:59 +0000 Received: by vbzb23 with SMTP id b23so4096658vbz.11 for ; Sun, 15 Apr 2012 02:00:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=G5rMeyXUWX5/eGfUxZgLKFUgj2YsUqL3VI1zvzVOVTs=; b=GdAP8AISK0Rvu0qjcfUKq/MxInyxXaOkA8i3Ywd/i2UiZoPgxZ27YXq+GjWZn1rasO 5mDE2PugtrX+s0/E6Tf3Ry4VboEYjqzIj71g8rgAQTIPQXIoTMRnZwEwcPkT0w7Fjb73 maLP3obLNewqOsW16avDX6WbakAC/4oWyOoXEvXjyD99M4Nk5kkpmuUf1idpBkXxqsDR i2lznERwabYqbNvOiEjfPqULbfXLUuJ1rArTrdUDeirKLE0eDS1UOojw54Ra2N5FlyIl jg0dh1Kx/CuAyT87LwAtI71LJ7KqyRxGe5Ox1NKPb7aXhbBWTGxA9Yg3vc3CvJuM0awx /idw== MIME-Version: 1.0 Received: by 10.52.72.169 with SMTP id e9mr3176042vdv.21.1334480438392; Sun, 15 Apr 2012 02:00:38 -0700 (PDT) Received: by 10.52.65.42 with HTTP; Sun, 15 Apr 2012 02:00:38 -0700 (PDT) In-Reply-To: <20120415061323.GD8135@translab.its.uci.edu> References: <20120415061323.GD8135@translab.its.uci.edu> Date: Sun, 15 Apr 2012 12:00:38 +0300 Message-ID: Subject: Re: Reduce just N rows? From: Alon Keren To: user@couchdb.apache.org Content-Type: multipart/alternative; boundary=20cf3071d0a27d787904bdb3f0e2 --20cf3071d0a27d787904bdb3f0e2 Content-Type: text/plain; charset=ISO-8859-1 On 15 April 2012 09:13, James Marca wrote: > CouchDB will compute reduced values for what you select. If you just > ask for values from A to B, it *only* will compute the reduced values > over that range. So you can get "clever" with the key value, using > something like > > map: emit( [user,game,trynumber], score); > > where trynumber is some value that is guaranteed to increase with each > completed game score stored. > > Your reduce could use the built-in Erlang _sum > > Then you can just request something like...hmm > > startKey=[user,game,BIGNUMBER]&order=descending&limit=10&reduce=false > (where BIGNUMBER is something bigger than the highest try number of game). > > This will give 10 values, and you can do the average lickety-split > client side, OR you can do one query to get highest try number, then > another to get between that game and ten back to let couch compute the > sum for you. > Thanks! I think a simpler alternative to 'trynumber' is the game's timestamp, and BIGNUMBER could be replaced by '{}' (see: http://wiki.apache.org/couchdb/View_collation). That's what I'm doing at the moment :) Unfortunately, as numbers of games and game-types grow, this would become pretty demanding in CPU time and number of calls to couch. > > I'd play around with that kind of an approach, but I would recommend > *against* computing the mean in the reduce, although you could do it. > It is much slower than using _count or _sum. Using my approach, Couch > will get the sum value, not the average, but you also should know the > rows and so can compute the simple arithmetic mean much faster than > serializing the data back and forth from JSON on the server. > > You're right, of course, and indeed my actual reduce is a sum operation - the average is calculated from its final value. > > As an aside, invariably I've found that when I use arrays for keys I > generally run into at least one case where I want to shuffle the order > of the keys and therefore generate an almost identical copy of the > view's data just to get a different organization of the tree. > > Hope that helps, > James > > > On Sun, Apr 15, 2012 at 02:02:21AM +0300, Alon Keren wrote: > > Hi, > > > > I have the following scenario: > > a player can play one of many types of games. After finishing a game, the > > player's score is kept in a new document. > > I'd like to list a player's average score in the latest 10 games, for all > > types of games. > > > > I don't see an easy way to solve this with CouchDB, unless there was a > > feature that limited (group-)reduce operations to just the first 10 of > all > > possible rows. > > > > Has anyone else encountered a similar situation? Would this feature be > > useful to others? If so, would it be technically feasible to implement? > > > > Thanks, > > > > Alon > --20cf3071d0a27d787904bdb3f0e2--