# Why is ruby build in pagination bad?

**URL:** <https://www.ruby-forum.com/t/why-is-ruby-build-in-pagination-bad/73216>\
**Category:** Rails\
**Created:** [October 23, 2006, 1:26am UTC](https://www.ruby-forum.com/t/why-is-ruby-build-in-pagination-bad/73216 "2006-10-23T01:26:07Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![ericgoodwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@ericgoodwin](https://www.ruby-forum.com/u/ericgoodwin)\
**Post date:** [October 23, 2006, 1:26am UTC](https://www.ruby-forum.com/t/why-is-ruby-build-in-pagination-bad/73216/1 "2006-10-23T01:26:07Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![ericgoodwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@ericgoodwin](https://www.ruby-forum.com/u/ericgoodwin)\
**Post date:** [October 24, 2006, 9:03pm UTC](https://www.ruby-forum.com/t/why-is-ruby-build-in-pagination-bad/73216/2 "2006-10-24T21:03:25Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![ericgoodwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@ericgoodwin](https://www.ruby-forum.com/u/ericgoodwin)\
**Post date:** [October 24, 2006, 9:45pm UTC](https://www.ruby-forum.com/t/why-is-ruby-build-in-pagination-bad/73216/3 "2006-10-24T21:45:11Z")

</div>

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](http://codefluency.com/2006/10/24/paginator-released)

–  
Josh S.  
[http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)

---

<div class="post-metadata">

**Author:** ![ericgoodwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@ericgoodwin](https://www.ruby-forum.com/u/ericgoodwin)\
**Post date:** [October 24, 2006, 10:54pm UTC](https://www.ruby-forum.com/t/why-is-ruby-build-in-pagination-bad/73216/4 "2006-10-24T22:54:55Z")

</div>

also be sure to check out paginating\_find  
([Welcome cardboardrocket.com - BlueHost.com](http://cardboardrocket.com/2006/09/06/paginating-find-now-even-sexier/)).  
I like the way it overloads the regular find method.

Mike

---

<div class="post-metadata">

**Author:** ![ericgoodwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@ericgoodwin](https://www.ruby-forum.com/u/ericgoodwin)\
**Post date:** [October 24, 2006, 10:38pm UTC](https://www.ruby-forum.com/t/why-is-ruby-build-in-pagination-bad/73216/5 "2006-10-24T22:38:23Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ericgoodwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@ericgoodwin](https://www.ruby-forum.com/u/ericgoodwin)\
**Post date:** [October 25, 2006, 2:29am UTC](https://www.ruby-forum.com/t/why-is-ruby-build-in-pagination-bad/73216/6 "2006-10-25T02:29:47Z")

</div>

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](http://codefluency.com/2006/10/24/paginator-released)
> 
> –  
> Josh S.  
> [http://blog.hasmanythrough.com](http://blog.hasmanythrough.com)

---

<div class="post-metadata">

**Author:** ![ericgoodwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@ericgoodwin](https://www.ruby-forum.com/u/ericgoodwin)\
**Post date:** [October 25, 2006, 10:46am UTC](https://www.ruby-forum.com/t/why-is-ruby-build-in-pagination-bad/73216/7 "2006-10-25T10:46:52Z")

</div>

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](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
