Why is ruby build in pagination bad?

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!

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

Eric G. 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 W.

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


Josh S.
http://blog.hasmanythrough.com

also be sure to check out paginating_find
(Welcome cardboardrocket.com - BlueHost.com).
I like the way it overloads the regular find method.

Mike

quick question about size. if i am only dealing with about 1000 items in
my database, is the build in pagination fine?

Philip H. 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

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 W.

Codefluency.com


Josh S.
http://blog.hasmanythrough.com

Eric G. 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