为什么我的Postgres SQL Query不使用索引

时间:2014-05-03 21:25:58

标签: performance postgresql postgresql-9.2 heroku-postgres

我有一个约23M行的表(sales_points)。它在(store_id,book_id)上有一个b树索引。我希望以下查询使用该索引,但EXPLAIN表示它正在进行顺序扫描:

select distinct store_id, book_id from sales_points

以下是EXPLAIN的输出:

Unique  (cost=2050448.88..2086120.31 rows=861604 width=8)
  ->  Sort  (cost=2050448.88..2062339.35 rows=23780957 width=8)
        Sort Key: store_id, book_id
        ->  Seq Scan on sales_points  (cost=0.00..1003261.87 rows=23780957 width=8)

如果我这样做,它确实使用索引:

select distinct book_id from sales_points where store_id = 1

以下是此查询的EXPLAIN输出:

HashAggregate  (cost=999671.02..999672.78 rows=587 width=4)
  ->  Bitmap Heap Scan on sales_points  (cost=55576.17..998149.04 rows=3043963 width=4)
        Recheck Cond: (store_id = 1)
        ->  Bitmap Index Scan on index_sales_points_on_store_id_and_book_id  (cost=0.00..55423.97 rows=3043963 width=0)
              Index Cond: (store_id = 1)

这是表DDL:

CREATE TABLE sales_points
(
  id serial NOT NULL,
  book_id integer,
  store_id integer,
  date date,
  created_at timestamp without time zone,
  updated_at timestamp without time zone,
  avg_list_price numeric(5,2),
  royalty_amt numeric(9,2),
  currency character varying(255),
  settlement_date date,
  paid_sales integer,
  paid_returns integer,
  free_sales integer,
  free_returns integer,
  lent_units integer,
  lending_revenue numeric(9,2),
  is_placeholder boolean,
  distributor_id integer,
  source1_id integer,
  source2_id integer,
  source3_id integer,
  CONSTRAINT sales_points_pkey PRIMARY KEY (id)
)
WITH (
  OIDS=FALSE
);

这是索引表达式:

CREATE INDEX index_sales_points_on_store_id_and_book_id
  ON sales_points
  USING btree
  (store_id, book_id);

那么为什么Postgres不会使用索引来加速SELECT?

1 个答案:

答案 0 :(得分:0)

好吧,我认为你的索引在需要时正常工作。您的第一个查询没有WHERE子句,因此Postgres无论如何都必须检索表中的所有记录。

仅用于测试,您可以通过禁用顺序扫描来强制使用索引:

SET enable_seqscan = OFF;

Postgres根据varoius条件选择扫描计划。取自:http://www.postgresql.org/docs/9.2/static/indexes-examine.html

  

...当不使用索引时,它可以用于测试以强制使用它们。有一些运行时参数可以关闭各种计划类型。例如,关闭顺序扫描(enable_seqscan)和嵌套循环连接(enable_nestloop)这些是最基本的计划,将迫使系统使用不同的计划。如果系统仍然选择顺序扫描或嵌套循环连接,那么可能有更基本的原因导致索引未被使用...