Есть запрос:
select DISTINCT (options.product_id) product_id,
(SELECT max( price ) FROM options WHERE products.id = options.product_id ) AS max_price,
(SELECT min( price ) FROM options WHERE products.id = options.product_id ) AS min_price,
products.slug,
products.name as name,
products.id as id,
products.picture_id,
meta_id,
products.description as description,
products.purchases,
updated_at,
priority
from "products" left join "product_locations" on "product_locations"."product_id" = "products"."id" left join "options" on "options"."product_id" = "products"."id" where "options"."price" >= 10 and "products"."id" in (select "product_id" from "product_categories" where "category_id" = 1405) and "products"."product_status_id" = 1 order by "priority" desc, "id" desc limit 20 offset 0
Нужно подсчитать количество страниц для такого запроса, сейчас реализировано так:
select COUNT( DISTINCT products.id ) from "products" left join "product_locations" on "product_locations"."product_id" = "products"."id" left join "options" on "options"."product_id" = "products"."id" where "products"."product_status_id" = 1 and "options"."price" >= 10 and "products"."id" in (select "product_id" from "product_categories" where "category_id" = 1405) limit 1
Но, на сайте будет несколько сотен тысяч товаров, может есть какие-то идеи по оптимизаци запросов?