为PostgreSQL查询选择正确的索引

Bac*_*ing 3 postgresql indexing performance postgresql-8.4

简化表:

CREATE TABLE products (
product_no integer PRIMARY KEY,
sales integer,
status varchar(16),
category varchar(16));

CREATE INDEX index_products_sales ON products (sales);
CREATE INDEX index_products_status ON products (status);
CREATE INDEX index_products_category ON products (category);
Run Code Online (Sandbox Code Playgroud)

PostgreSQL版本是8.4.列"状态"和"类别"

15个类别中有2000万个产品/行.

最常用的查询之一是获得三个最畅销的产品,不包括'cat3'和'cat7'类别的产品:

SELECT product_no, sales 
FROM products 
WHERE status = 'something' AND category NOT IN ('cat3', 'cat7') 
ORDER BY sales DESC 
LIMIT 3;

Limit  (cost=0.00..8833.39 rows=3 width=12) (actual time=9235.332..9356.284 rows=3 loops=1)
   ->  Index Scan using index_products_sales on products  (cost=0.00..68935806.85 rows=23412 width=12) (actual time=9235.327..9356.278 rows=3 loops=1)
     Filter: (((category)::text <> ALL ('{cat3,cat7}'::text[])) AND ((status)::text = 'something'::text))
Run Code Online (Sandbox Code Playgroud)

使这个特定查询运行得更快的最佳索引是什么?

Erw*_*ter 13

使用此特定排序顺序创建部分多列索引:

CREATE INDEX products_status_sales_partial_idx ON products (status, sales DESC)
WHERE  category NOT IN ('cat3','cat7');
Run Code Online (Sandbox Code Playgroud)

稍微修改您的查询:

SELECT product_no, sales 
FROM   products 
WHERE  status = 'something'
AND    category NOT IN ('cat3', 'cat7') 
ORDER  BY status, sales DESC 
LIMIT  3;
Run Code Online (Sandbox Code Playgroud)

添加status作为该ORDER BY子句的第一个元素似乎是多余的和毫无意义的.但试一试.

为什么?

查询规划器不够智能,无法理解

WHERE  status = 'something' ...
ORDER  BY sales DESC
Run Code Online (Sandbox Code Playgroud)

索引的排序顺序(status, sales DESC)匹配为逻辑结果.因此,它将读取所有符合条件的行,排序并选择前3行.

通过添加status,ORDER BY您可以使查询计划程序直接从索引中读取前3个条目.预计加速几个数量级.

使用PostgreSQL 8.4和9.1进行测试.