[Newbie] MyClass.by_tag slower than DB.view("myclass/by_tag")?

2 views
Skip to first unread message

Shajith

unread,
Dec 9, 2009, 3:04:54 AM12/9/09
to CouchRest
Hi,
I'm sure I am missing something basic here.

I have a CouchRest::ExtendedDocument subclass with some properties,
and have created around 300 documents in the database. I am defining a
view using view_by in the class, like so:

view_by :tag

Playing around in irb (script/console), I noticed that calling the
by_tag method on my class is kind of slow. Calling the view using the
'view' method on the database object is a lot faster. What gives?

Here's an irb session:

>> Benchmark.measure { BlogEntry.all.size }
=> #<Benchmark::Tms:0x111f03090 @real=1.65944004058838, @utime=1.19,
@cstime=0.0, @cutime=0.0, @total=1.33, @label="", @stime=0.14>
>> Benchmark.measure { COUCH.view("BlogEntry/all").size }
=> #<Benchmark::Tms:0x111e23378 @real=0.121485948562622,
@utime=0.0700000000000003, @cstime=0.0, @cutime=0.0,
@total=0.0700000000000003, @label="", @stime=0.0>

BlogEntry is the ExtendedDocument, and COUCH is the database I created
using a call like this:

COUCH = CouchRest.database!("http://127.0.0.1:5984/couchrest-test")

Thanks in advance
Shajith

will leinweber

unread,
Dec 9, 2009, 3:57:42 AM12/9/09
to couc...@googlegroups.com
The class method #all on your BlogPost subclass is building actual instances of BlogPost, and handing you an array of them.

Running the view directly is just returning you the data from couchdb straight in a hash. 


--

You received this message because you are subscribed to the Google Groups "CouchRest" group.
To post to this group, send email to couc...@googlegroups.com.
To unsubscribe from this group, send email to couchrest+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/couchrest?hl=en.



Shajith Chacko

unread,
Dec 9, 2009, 11:10:03 AM12/9/09
to couc...@googlegroups.com
On Wed, Dec 9, 2009 at 12:57 AM, will leinweber
<will.le...@gmail.com> wrote:
> The class method #all on your BlogPost subclass is building actual instances
> of BlogPost, and handing you an array of them.
> Running the view directly is just returning you the data from couchdb
> straight in a hash.

So I tried adding the BlogEntry initialization to the direct view
call. Here's the comparison again:

>> Benchmark.measure { BlogEntry.all.size }
=> #<Benchmark::Tms:0x1125243e8 @real=1.90700101852417, @utime=1.41,
@cstime=0.0, @cutime=0.0, @total=1.55, @label="", @stime=0.14>
>> Benchmark.measure { COUCH.view("BlogEntry/all")["rows"].collect {|r| BlogEntry.new(r)}.size }
=> #<Benchmark::Tms:0x1123ff008 @real=0.162764072418213,
@utime=0.100000000000001, @cstime=0.0, @cutime=0.0,
@total=0.120000000000002, @label="", @stime=0.0200000000000005>


The time taken by the direct view call nearly doubles when I add the
initialization as well, but it's still much faster than the
BlogEntry.all call.

> On Wed, Dec 9, 2009 at 2:04 AM, Shajith <deme...@gmail.com> wrote:
>> Here's an irb session:
>>
>> >> Benchmark.measure { BlogEntry.all.size }
>> => #<Benchmark::Tms:0x111f03090 @real=1.65944004058838, @utime=1.19,
>> @cstime=0.0, @cutime=0.0, @total=1.33, @label="", @stime=0.14>
>> >> Benchmark.measure { COUCH.view("BlogEntry/all").size }
>> => #<Benchmark::Tms:0x111e23378 @real=0.121485948562622,
>> @utime=0.0700000000000003, @cstime=0.0, @cutime=0.0,
>> @total=0.0700000000000003, @label="", @stime=0.0>
>>
>> BlogEntry is the ExtendedDocument, and COUCH is the database I created
>> using a call like this:
>>
>> COUCH = CouchRest.database!("http://127.0.0.1:5984/couchrest-test")

BTW: I am using version 0.33 of the gem.

Shajith

PS: I meant to send numbers for the by_tag view, but sent the "all"
numbers instead, sorry about the confusion. The disparity exists in
either, none the less.

Matt Aimonetti

unread,
Dec 9, 2009, 4:55:46 PM12/9/09
to couc...@googlegroups.com
You might want to compare the end result of both solutions. I believe, that BlogEntry.all will return all the docs using include_docs=true while COUCH.view("BlogEntry/all") will only return the doc ids and keys.

That would explain the speed difference.

- Matt


Shajith

unread,
Dec 9, 2009, 5:40:52 PM12/9/09
to CouchRest
Okay, thanks. I think that's what's going on.

Would it be right to assume that there is no difference in performance
between those two ways of calling a view?

Shajith
> > > On Wed, Dec 9, 2009 at 2:04 AM, Shajith <demer...@gmail.com> wrote:
> > >> Here's an irb session:
>
> > >> >> Benchmark.measure { BlogEntry.all.size }
> > >> => #<Benchmark::Tms:0x111f03090 @real=1.65944004058838, @utime=1.19,
> > >> @cstime=0.0, @cutime=0.0, @total=1.33, @label="", @stime=0.14>
> > >> >> Benchmark.measure { COUCH.view("BlogEntry/all").size }
> > >> => #<Benchmark::Tms:0x111e23378 @real=0.121485948562622,
> > >> @utime=0.0700000000000003, @cstime=0.0, @cutime=0.0,
> > >> @total=0.0700000000000003, @label="", @stime=0.0>
>
> > >> BlogEntry is the ExtendedDocument, and COUCH is the database I created
> > >> using a call like this:
>
> > >> COUCH = CouchRest.database!("http://127.0.0.1:5984/couchrest-test")
>
> > BTW: I am using version 0.33 of the gem.
>
> > Shajith
>
> > PS: I meant to send numbers for the by_tag view, but sent the "all"
> > numbers instead, sorry about the confusion. The disparity exists in
> > either, none the less.
>
> > --
>
> > You received this message because you are subscribed to the Google Groups
> > "CouchRest" group.
> > To post to this group, send email to couc...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > couchrest+...@googlegroups.com<couchrest%2Bunsubscribe@googlegroups .com>
> > .

Matt Aimonetti

unread,
Dec 9, 2009, 5:42:40 PM12/9/09
to couc...@googlegroups.com
There should be a very tiny difference, but only benchmarks would help us make sure of that.

- Matt

To unsubscribe from this group, send email to couchrest+...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages