Pagination Best Practices

Talk about writing modules and plugins for CMS Made Simple, or about specific core functionality. This board is for PHP programmers that are contributing to CMSMS not for site developers
Post Reply
tmk
Forum Members
Forum Members
Posts: 25
Joined: Sun Sep 23, 2007 4:07 am

Pagination Best Practices

Post by tmk »

I'm not that deep into the internals of MySQL... so please excuse my ignorance when it comes to efficiency.

Let's say I have a (MySQL) database of several thousand records that is updated/added to quite frequently . I want to show, 'pagerows' records at a time with several SQL WHERE conditions. The WHERE conditions may return 0 to all records, depending on the critera.

All I really need are a the pagerows rows of data,  and links for Previous, Page-x-of-y , and Next

Which might be the more efficient:

1) Run a SELECT COUNT(*) to get total # of matching records, then run the real SELECT query with the fields and data I want with a LIMIT clause (LIMIT start,pagerows)

2) Run the SELECT query with the fields and data I want using the  and perform a while loop : While ($row=$result->FetchRow() {...}) fetching the start through start+pagerows.

3) Your suggestions

Thanks
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm
Location: Fernie British Columbia, Canada

Re: Pagination Best Practices

Post by calguy1000 »

Pick option 1

two queries:
  a) get the total count of matching records
      this allows you to determine page count, etc.
  b) get the matching records for the current page using a limit parameter.

that's the way I do it in all of the modules I write that require pagination.

The other option will give you nothing but problems with php memory requirements and performance.
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
tmk
Forum Members
Forum Members
Posts: 25
Joined: Sun Sep 23, 2007 4:07 am

[solved] Pagination Best Practices

Post by tmk »

Calguy, many thanks...

I was thinking #1, but was looking for confirmation, and I value your judgement.
Post Reply

Return to “Developers Discussion”