[英]Optimize select query along with where clause to use index scan in postgres
我有一个表“ offer_facts”,其中有许多列,包括product_dimension_id(产品尺寸表的外键)和source_name(varchar)。 这两个列都已索引。 目前,此表中大约有12万行。 该表在不断增长(每天约2万)。
下面是查询和我得到的输出。
SELECT version() "PostgreSQL 9.4.4 on x86_64-unknown-linux-gnu, compiled by gcc (Ubuntu 4.8.2-19ubuntu1) 4.8.2, 64-bit"
EXPLAIN (ANALYZE, BUFFERS) SELECT DISTINCT product_dimension_id from offer_facts WHERE source_name='customer_conti'
输出是
HashAggregate (cost=36619.24..36621.37 rows=213 width=4) (actual time=2654.272..2655.064 rows=399 loops=1)
Group Key: product_dimension_id
Buffers: shared hit=2697 read=17687
-> Seq Scan on offer_facts (cost=0.00..35425.82 rows=477367 width=4) (actual time=0.021..1525.361 rows=479880 loops=1)
Filter: ((source_name)::text = 'customer_conti'::text)
Rows Removed by Filter: 723466
Buffers: shared hit=2697 read=17687
Planning time: 0.201 ms
Execution time: 2655.778 ms
我不确定为什么要执行Seq Scan而不是Index Scan。
我已经创建了索引
CREATE INDEX idx_offer_facts_dimensions ON offer_facts USING btree (source_name COLLATE pg_catalog."default", shop_dimension_id, time_dimension_id, date_dimension_id, source_dimension_id, product_dimension_id);
我已经抽真空并分析了桌子。
这是您的查询:
SELECT DISTINCT product_dimension_id
FROM offer_facts
WHERE source_name = 'customer_conti' ;
最佳索引是复合索引: offer_facts(source_name, product_dimension_id)
。 每列上的单个索引没有那么有用。 该查询可以利用索引扫描。 Postgres 应该足够聪明才能找到该执行路径。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.