简体   繁体   中英

How to make SQL query faster with paging?

I have an ecommerce website with a shop page, each shop page contain 6 products and user can filter according to size and category, then user must press next to get next 6 products. google pagespeed index says that TTFB is about 0.7 seconds which is mostly taken by SQL query yet i have only 23 product which i expect to increase dramatically when i add more products. mysql query looks like this in php file.

$q = "
    SELECT  products.name, products.colors, products.sizes,
            products.selling_price,
            products.id, products.size_chart, products.about
        FROM  products 
";
$q .= $filter_options;

$filter_options is returned from another function which makes the entire query to be like this on first page (page 1) when i echo $q without applying size and category filters only sorting.

    SELECT  products.name, products.colors, products.sizes,
            products.selling_price,
            products.id, products.size_chart, products.about
        FROM  products
        INNER JOIN  category  ON category.id = products.category_id
        WHERE  1 = 1
        ORDER BY  products.selling_price Desc
        LIMIT  0,6 

and here is the query with category and size filters are applied

    SELECT  products.name, products.colors, products.sizes,
            products.selling_price,
            products.id, products.size_chart, products.about
        FROM  products
        INNER JOIN  category  ON category.id = products.category_id
        WHERE  1 = 1
          AND  FIND_IN_SET('s', products.sizes)>0
          AND  category.name = 'Dresses'
        ORDER BY  products.selling_price Desc
        LIMIT  0,6 

My limitation are:

i cant limit number of results by WHERE products.id < $some_page_index since products ID are not continous since i add and remove products directly which makes id like 1 2 4 10 so it will result in less than 6 products on some pages.

If you are doing "pagination", don't use OFFSET , instead "remember where you left off": Pagination

Do you at least have these indexes?

category:  INDEX(name)
products:  INDEX(category_id, selling_price)

I recommend looking over the queries that are used, then building composite indexes starting with the column(s) tested with = .

There are practical limits on how many columns in an index and how many different indexes to have. If you have INDEX(a,b) , do not also have INDEX(a) . More: Index Cookbook

MySQL cannot provide a perfect indexing mechanism for an app like yours.

LIMIT offset, count; is only work when the records is small. If large table, you can use condition like:

ID is a primary key or unique key(not null)

select * from table where ID>last_query_id limit offset, count order by ID;

As the query optimizer can use ID for range scan to improve query performance.

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