Home > Software engineering >  How to make SQL query faster with paging?
How to make SQL query faster with paging?

Time:08-27

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.

CodePudding user response:

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.

  • Related