我试图使用explain analysis来优化我的一些选择,我不明白为什么
postgresql使用顺序扫描而不是索引扫描:
explain analyze SELECT SUM(a.deure)-SUM(a.haver) as Value FROM assentaments a LEFT JOIN comptes c ON a.compte_id = c.id WHERE c.empresa_id=2 AND c.nivell=11 AND (a.data >='2007-01-01' AND a.data <='2007-01-31') AND c.codi_compte LIKE '6%'; ------------------------------------------------------------------------------------------------------------------------------------------------ Aggregate (cost=44250.26..44250.27 rows=1 width=12) (actual time=334.054..334.054 rows=1 loops=1) -> Nested Loop (cost=0.00..44249.20 rows=211 width=12) (actual time=65.277..333.179 rows=713 loops=1) -> Seq Scan on comptes c (cost=0.00..8001.72 rows=118 width=4) (actual time=0.053..64.287 rows=236 loops=1) Filter: (((codi_compte)::text ~~ '6%'::text) AND (empresa_id = 2) AND (nivell = 11)) -> Index Scan using index_compte_id on assentaments a (cost=0.00..307.16 rows=2 width=16) (actual time=0.457..1.138 rows=3 loops=236) Index Cond: (a.compte_id = c.id) Filter: ((a.data >= '2007-01-01'::date) AND (a.data <= '2007-01-31'::date)) Total runtime: 334.104 ms (8 rows)
我创建了一个自定义索引:
CREATE INDEX "index_multiple" ON "public"."comptes" USING btree(codi_compte ASC NULLS LAST,empresa_id ASC NULLS LAST,nivell ASC NULLS LAST);
而且我已经在comptes表上为这三个字段创建了三个新的索引,只是为了检查它是否需要索引扫描,但不是,结果是一样的:
CREATE INDEX "index_codi_compte" ON "public"."comptes" USING btree(codi_compte ASC NULLS LAST); CREATE INDEX "index_comptes" ON "public"."comptes" USING btree(codi_compte ASC NULLS LAST); CREATE INDEX "index_multiple" ON "public"."comptes" USING btree(codi_compte ASC NULLS LAST,nivell ASC NULLS LAST); CREATE INDEX "index_nivell" ON "public"."comptes" USING btree(nivell ASC NULLS LAST);
谢谢!
米
编辑:
assentaments.id和assentaments.data也有它们的索引
select count(*) FROM comptes => 148498 select count(*) from assentaments => 2128771 select count(distinct(codi_compte)) FROM comptes => 137008 select count(distinct(codi_compte)) FROM comptes WHERE codi_compte LIKE '6%' => 368 select count(distinct(codi_compte)) FROM comptes WHERE codi_compte LIKE '6%' AND empresa_id=2; => 303
解决方法
如果您希望TEXT上的索引对LIKE查询进行索引,则需要使用text_pattern_ops创建它,如下所示:
test=> CREATE TABLE t AS SELECT n::TEXT FROM generate_series( 1,100000 ) n; test=> CREATE INDEX tn ON t(n); test=> VACUUM ANALYZE t; test=> EXPLAIN ANALYZE SELECT * FROM t WHERE n LIKE '123%'; QUERY PLAN -------------------------------------------------------------------------------------------------- Seq Scan on t (cost=0.00..1693.00 rows=10 width=5) (actual time=0.027..14.631 rows=111 loops=1) Filter: (n ~~ '123%'::text) Total runtime: 14.664 ms test=> CREATE INDEX tn2 ON t(n text_pattern_ops); CREATE INDEX Temps : 267,589 ms test=> EXPLAIN ANALYZE SELECT * FROM t WHERE n LIKE '123%'; QUERY PLAN --------------------------------------------------------------------------------------------------------------- Bitmap Heap Scan on t (cost=5.25..244.79 rows=10 width=5) (actual time=0.089..0.121 rows=111 loops=1) Filter: (n ~~ '123%'::text) -> Bitmap Index Scan on tn2 (cost=0.00..5.25 rows=99 width=0) (actual time=0.077..0.077 rows=111 loops=1) Index Cond: ((n ~>=~ '123'::text) AND (n ~<~ '124'::text)) Total runtime: 0.158 ms
在这里看到细节:
http://www.postgresql.org/docs/9.1/static/indexes-opclass.html
如果您不想创建附加索引,而列是TEXT,则可以将“compte LIKE”6“’替换为”compte> =’6“和compte”7“,这是一个简单的索引范围条件.
test=> EXPLAIN ANALYZE SELECT * FROM t WHERE n >= '123' AND n < '124'; QUERY PLAN ----------------------------------------------------------------------------------------------------------- Index Scan using tn on t (cost=0.00..126.74 rows=99 width=5) (actual time=0.030..0.127 rows=111 loops=1) Index Cond: ((n >= '123'::text) AND (n < '124'::text)) Total runtime: 0.153 ms
在你的情况下,这个解决方案可能更好.