From user-return-17254-apmail-couchdb-user-archive=couchdb.apache.org@couchdb.apache.org Sun Jul 24 23:52:47 2011 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 CED137A48 for ; Sun, 24 Jul 2011 23:52:47 +0000 (UTC) Received: (qmail 7790 invoked by uid 500); 24 Jul 2011 23:52:46 -0000 Delivered-To: apmail-couchdb-user-archive@couchdb.apache.org Received: (qmail 7745 invoked by uid 500); 24 Jul 2011 23:52:45 -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 7736 invoked by uid 99); 24 Jul 2011 23:52:45 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 24 Jul 2011 23:52:45 +0000 X-ASF-Spam-Status: No, hits=1.6 required=5.0 tests=FREEMAIL_ENVFROM_END_DIGIT,FREEMAIL_FROM,HTML_MESSAGE,RCVD_IN_DNSWL_LOW,SPF_PASS,T_TO_NO_BRKTS_FREEMAIL X-Spam-Check-By: apache.org Received-SPF: pass (nike.apache.org: domain of tretiy3@gmail.com designates 209.85.160.180 as permitted sender) Received: from [209.85.160.180] (HELO mail-gy0-f180.google.com) (209.85.160.180) by apache.org (qpsmtpd/0.29) with ESMTP; Sun, 24 Jul 2011 23:52:37 +0000 Received: by gyg4 with SMTP id 4so3219154gyg.11 for ; Sun, 24 Jul 2011 16:52:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=7AKhILY2zj1Ju5B3L4oMLzsCAClFWnOWcWgBRKmy0Nw=; b=Uq7pAWIZWDhvfLsQVgvoT8J0QUloOo99eaC7mZI+cRbxBBHFYbN+15Q2BfN2A09n8T N7Hqx0PSs447m8G6cMdOOnqvie/bR8acMm4epjgc94Kit8ZiJQiq2B/rBZYSF3KpW7iI tVZymJ9bHXKz2emA6YX4Jetb0aBl/v/PKlvQo= MIME-Version: 1.0 Received: by 10.150.73.34 with SMTP id v34mr3950813yba.97.1311551535883; Sun, 24 Jul 2011 16:52:15 -0700 (PDT) Received: by 10.150.195.15 with HTTP; Sun, 24 Jul 2011 16:52:15 -0700 (PDT) In-Reply-To: <1B706292-E785-4F53-927E-2B0DC7E8434D@gmail.com> References: <1B706292-E785-4F53-927E-2B0DC7E8434D@gmail.com> Date: Mon, 25 Jul 2011 02:52:15 +0300 Message-ID: Subject: Re: 1 large view vs 2 smaller views From: Ganzha Alexey To: user@couchdb.apache.org Content-Type: multipart/alternative; boundary=000e0cd58e3a66a7dd04a8d963d7 X-Virus-Checked: Checked by ClamAV on apache.org --000e0cd58e3a66a7dd04a8d963d7 Content-Type: text/plain; charset=ISO-8859-1 Thats the map function function(doc) { emit([doc.dep,doc.price,doc.date,doc._id]); } Thats the query i use: /mydb/_design/mydb/_view/fetch?endkey=["auto", 0, ["0"], ""]&startkey=["auto", 1000000000000, ["9"], "\ufff0"]&descending=true it returns me all docs with the dep "auto" sorted by price, if prices are equal - by date, if price and date are equal - by _id thats some answers for that query: {"response":{"total_rows":16398,"offset":0,"rows":[ {"id":"60af978547e9aea0d530ffa634195844","key":["auto",610000000,["2011","07","13"],"60af978547e9aea0d530ffa634195844"],"value":null}}, {"id":"f905a48febec1c3e2b8023342f0b0a2e","key":["auto",17000000,["2011","07","08"],"f905a48febec1c3e2b8023342f0b0a2e"],"value":null}}, .. Its clear. I have no problem with that. Now, i need only fetch docs added last week. I mark all that docs with the attribute week:true. It is a choice. transform present view, just adding 1 parameter to the key like that: function(doc) { emit(['all', doc.dep,doc.price,doc.date,doc._id]); if (doc.week) emit(['week', doc.dep,doc.price,doc.date,doc._id]); } Now it is possible to get all docs like this /mydb/_design/mydb/_view/fetch?endkey=["all", "auto", 0, ["0"], ""]&startkey=["all", "auto", 1000000000000, ["9"], "\ufff0"]&descending=true and 'week' docs like this /mydb/_design/mydb/_view/fetch?endkey=["week", "auto", 0, ["0"], ""]&startkey=["week", "auto", 1000000000000, ["9"], "\ufff0"]&descending=true It will work.But the view transformed in such a way, will emit twice for every document having 'week' attr, and the key is increased. I thing it will grow rather large. The other solution - is just to make second view, which will emit only 'week' docs. Thats the question: what will you prefer? 1 solution (single view emiting twice for some docs), or second (2 separate views). Thanks. On Mon, Jul 25, 2011 at 1:41 AM, Dennis Clark wrote: > You won't be able to use just that view to get the sorbability you want, > then -- lexicographic ordering means that that your example will sequence > your keys by doc.depth first, so you couldn't query and get all docs with > the same date or date range. > > Perhaps best to step back a second and explain a little more about what > you're trying to do? We can probably help, but only with a hair more input. > > On Jul 24, 2011, at 6:24 PM, Ganzha Alexey wrote: > > > Thanks for your answer. > > > > Actually it is very important to sort by the other fields: dep, price. > even > > _id i need to separate docs with the same price. > > So, the only way i see - this additional "week" (''THIS_IS_FOR_ALL'', > > 'THIS_IS_FOR_LAST_WEEK_ > > ONLY') element in the key, at the first place. And i have to match this > > element exactly for all docs, or for week docs. > > > > > > On Mon, Jul 25, 2011 at 12:54 AM, Dennis Clark > wrote: > > > >> How important is it to you to have the view sorted by doc.dep? If the > >> answer > >> is "not at all," the idiomatically CouchDB way to do this is: > >> > >> emit(doc.date,[doc.dep,doc.price,doc._id]); > >> > >> and then let users query with startkey/endkey if they need only the docs > >> from the last week. This way you don't need some "in the last week" > field > >> that you have to keep updated. > >> > >> On Sun, Jul 24, 2011 at 5:46 PM, Ganzha Alexey > wrote: > >> > >>> Hi. > >>> I am trying figuring out whats the better. > >>> > >>> Say i have a view: > >>> ... > >>> emit([doc.dep,doc.price,doc.date,doc._id]); > >>> ... > >>> > >>> Some users need all docs. And some of them need only docs for the last > >>> week. > >>> Just add an attribute 'last_week' in required documents. > >>> Then, in the view i will check this attribute and there are 2 cases: > >>> > >>> Filter docs in 1 view > >>> > >>> emit(['THIS_IS_FOR_ALL',doc.dep,doc.price,doc.date,doc._id]); > >>> > >>> if (doc.last_week) > >>> > >> emit(['THIS_IS_FOR_LAST_WEEK_ONLY',doc.dep,doc.price,doc.date,doc._id]); > >>> > >>> i will be able to query this view with the additional key member (will > >> user > >>> need all docs, or only last week docs) > >>> > >>> Or make 2 separate views. First: > >>> > >>> emit([doc.dep,doc.price,doc.date,'doc._id]); > >>> > >>> and Second: > >>> > >>> if (doc.last_week) > >>> emit([doc.dep,doc.price,doc.date,doc._id]) > >>> > >>> First case will emit more keys, and the keys will be larger by 1 > element. > >>> But in second case, there are 2 views. > >>> > >>> What would you prefer. What is the better for performance? > >>> Thanks > >>> > >> > --000e0cd58e3a66a7dd04a8d963d7--