简体   繁体   中英

PHP + MySql Pagination with categories and no offset

I'm working with online marketplace site where users can sell their own stuff and visitors can browse marketplace by categories.

I have been reading about mysql pagination that if you use limit with offset, it will use alot resources and takes long time to execute if its huge database.

So, I don't want to use "LIMIT 10000, 20" as example, only if its last choise.

If there was no categories, this would do fine "SELECT * FROM products WHERE id > 10000 LIMIT 20"

But, as there is categories for products involved, I can't use that. Lets say that first product added to category id 10 is added after there is 2000 products in whole database. That product would get ID 2001.

So if I would browse first page of category id 10, this query would run: "SELECT * FROM products WHERE catid = 10 LIMIT 20"

If I go to next page, this runs: "SELECT * FROM products WHERE catid = 10 AND id > 20 LIMIT 20"

So, basicly visitor would see product 2001 in every page until he hits page 101 when query looks for products that id is over 2020 and thats why I can't use "WHERE id > something" in query.

Long story short, whats best way to deal with pagination when working with categories and doesnt eat so much performance?

I think in your case, you can simply run this query for example:

"SELECT * FROM products WHERE catid = 10"

And after it returns so much row, control it in the client side using jquery datatables. https://datatables.net/

This is what I'm using currently because it's very fast. No more querying needed as you move to another page of your pagination

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM