Ruby Forum Ruby on Rails > Why is ruby build in pagination bad?

Posted by Aryk Grosz (gotskill10)
on 23.10.2006 01:26
Hey guys,

Im trying to understand why the ruby pagination is slow and bad so that 
I can write a good one of my own.

From my understanding, the ruby paginator, just takes the full results 
set and display only the portion that you want it to display.

You then create a paginator object so that Ruby can create pagination 
links using the built in helper.

For example, im using ferret multi search with an offset and limit 
option. I then use the total_hits() method and make a call for 
pagination like this:

   def pages_for(size, options = {})
      default_options = {:per_page => 10}
      options = default_options.merge options
      pages = Paginator.new self, size, options[:per_page],
      (params[:page]||1)
      pages
   end

What is so slow about all this? Where will the slow down be? If you guys 
could point me to any good pagination plugins OR even articles about why 
the current one sucks that would be great. Thanks in advance!
Posted by Philip Hallstrom (Guest)
on 24.10.2006 21:03
(Received via mailing list)
> Im trying to understand why the ruby pagination is slow and bad so that
> I can write a good one of my own.
>
> From my understanding, the ruby paginator, just takes the full results
> set and display only the portion that you want it to display.

Yep.  Imagine you have a table with a million rows of data (with a lot 
of
text columns).  And you want to page through it ten at a time.  Why 
you'd
do this I dunno, but let's just pretend.

To display rows 1-10 Rails is going to return *all* million rows, create
objects for all of them, then hand you back the first 10.  So it's doing
this in SQL: SELECT * FROM big_table

If you create your own pager you can make this a lot more efficient by
doing something like this: SELECT * FROM big_table LIMIT 10 OFFSET 0

And you're only returning 10 rows hwich is going to be a lot faster than
returning 1 million...

-philip
Posted by Josh Susser (jsusser)
on 24.10.2006 21:45
Eric Gross wrote:
> Im trying to understand why the ruby pagination is slow and bad so that 
> I can write a good one of my own.
> 
> From my understanding, the ruby paginator, just takes the full results 
> set and display only the portion that you want it to display.
> 
> You then create a paginator object so that Ruby can create pagination 
> links using the built in helper.
> ...
> What is so slow about all this? Where will the slow down be? If you guys 
> could point me to any good pagination plugins OR even articles about why 
> the current one sucks that would be great. Thanks in advance!

How timely...  check out this new paginator gem by Bruce Williams

http://codefluency.com/2006/10/24/paginator-released

--
Josh Susser
http://blog.hasmanythrough.com
Posted by bbqPlate (Guest)
on 24.10.2006 22:38
quick question about size. if i am only dealing with about 1000 items in 
my database, is the build in pagination fine?


Philip Hallstrom wrote:
>> Im trying to understand why the ruby pagination is slow and bad so that
>> I can write a good one of my own.
>>
>> From my understanding, the ruby paginator, just takes the full results
>> set and display only the portion that you want it to display.
> 
> Yep.  Imagine you have a table with a million rows of data (with a lot 
> of
> text columns).  And you want to page through it ten at a time.  Why 
> you'd
> do this I dunno, but let's just pretend.
> 
> To display rows 1-10 Rails is going to return *all* million rows, create
> objects for all of them, then hand you back the first 10.  So it's doing
> this in SQL: SELECT * FROM big_table
> 
> If you create your own pager you can make this a lot more efficient by
> doing something like this: SELECT * FROM big_table LIMIT 10 OFFSET 0
> 
> And you're only returning 10 rows hwich is going to be a lot faster than
> returning 1 million...
> 
> -philip
Posted by Mike Garey (random52k)
on 24.10.2006 22:54
(Received via mailing list)
also be sure to check out paginating_find
(http://cardboardrocket.com/2006/09/06/paginating-find-now-even-sexier/).
I like the way it overloads the regular find method.

Mike
Posted by Aryk Grosz (gotskill10)
on 25.10.2006 02:29
Hmm, isnt this just using limit and offset to get only a section of your 
result set and then putting the TOTAL_RESULTS variable to know how many 
pages there are?

It uses the Paginator class which already comes with Ruby right.

Also, if im paginating less then like 7 or 8 pages, do I really have to 
worry about  pagination efficiency? I mean Im going to query a result 
set of like 70 or 80 and then break it up into pages, is that really 
something i have to get a sexy paginator over?


> 
> How timely...  check out this new paginator gem by Bruce Williams
> 
> http://codefluency.com/2006/10/24/paginator-released
> 
> --
> Josh Susser
> http://blog.hasmanythrough.com
Posted by Chris Mear (Guest)
on 25.10.2006 10:46
(Received via mailing list)
Eric Gross wrote:
> Im trying to understand why the ruby pagination is slow and bad so that
> I can write a good one of my own.
>
> From my understanding, the ruby paginator, just takes the full results
> set and display only the portion that you want it to display.

Nope, the built-in Rails paginator uses offset and limit to fetch only
the results for the current page.

According to this page:

http://glu.ttono.us/articles/2006/08/30/on-the-days-events

it's not the database-level stuff that's the problem, but the
view-level pagination helpers that have 'efficiency issues'. I haven't
been able to find a more detailed explanation, though.

Anyone care to enlighten us?

Chris