使用 IN 运算符查询的索引加快对 postgres 的 Min/Max 操作
Speed up Min/Max operation on postgres with index for IN operator query
我想在 postgres 中优化以下查询
SELECT(MIN("products"."shipping") AS minimal FROM "products" WHERE "products"."tag_id" IN (?)
索引像
CREATE INDEX my_index ON products (tag_id, shipping DESC);
不幸的是,这个只有在只有一个标签时才使用。几乎总是同时查询几个标签,但是 postgres 使用索引 products (shipping DESC)
这非常慢。
我可以做些什么来加快查询速度?
事实证明(请参阅评论),这个查询:
SELECT MIN(minimal) AS minimal
FROM (
SELECT MIN("products"."shipping") AS minimal
FROM "products"
WHERE "products"."tag_id" IN (?,?,?,?,?,?,?)
GROUP BY "tag_id"
) some_alias
能够以这种方式欺骗 PostgreSQL,使其性能更好,因为我猜,它在这种情况下使用了索引。
我想在 postgres 中优化以下查询
SELECT(MIN("products"."shipping") AS minimal FROM "products" WHERE "products"."tag_id" IN (?)
索引像
CREATE INDEX my_index ON products (tag_id, shipping DESC);
不幸的是,这个只有在只有一个标签时才使用。几乎总是同时查询几个标签,但是 postgres 使用索引 products (shipping DESC)
这非常慢。
我可以做些什么来加快查询速度?
事实证明(请参阅评论),这个查询:
SELECT MIN(minimal) AS minimal
FROM (
SELECT MIN("products"."shipping") AS minimal
FROM "products"
WHERE "products"."tag_id" IN (?,?,?,?,?,?,?)
GROUP BY "tag_id"
) some_alias
能够以这种方式欺骗 PostgreSQL,使其性能更好,因为我猜,它在这种情况下使用了索引。