0
我有2个表如下:为什么Postgres的不使用我的查询索引
tb_st:
Columns:
st_id | integer
st | character varying(80)
type | integer
Indexes:
PRIMARY KEY (st_id)
UNIQUE INDEX (st, type)
INDEX (st)
tb_pd:
Column
st_id | integer
bot_id | integer
Indexes:
PRIMARY KEY (st_id, bot_id)
INDEX (bot_id)
Foreign-key constraints:
FOREIGN KEY (st_id) REFERENCES tb_st(st_id)
当我解释查询:
select p.bot_id
from tb_pd p inner join
tb_st s
on p.st_id = s.st_id
where s.st = 'abc' and s.type = 1
的Postgres给了我这样的:
Nested Loop (cost=4.24..16.10 rows=11 width=194)
-> Seq Scan on tb_st s (cost=0.00..1.07 rows=1 width=186)
Filter: (((st)::text = 'abc'::text) AND (type = 1))
-> Bitmap Heap Scan on tb_pd p (cost=4.24..14.91 rows=11 width=8)
Recheck Cond: (st_id = s.st_id)
-> Bitmap Index Scan on tb_pd_pkey (cost=0.00..4.24 rows=11 width=0)
Index Cond: (st_id = s.st_id)
(7 rows)
经过一段时间,给了我这个完全相同的查询(仍然没有使用索引):
Nested Loop (cost=0.00..2.19 rows=1 width=4)
Join Filter: (p.st_id = s.st_id)
-> Seq Scan on tb_st s (cost=0.00..1.07 rows=1 width=4)
Filter: (((st)::text = 'abc'::text) AND (type = 1))
-> Seq Scan on tb_pd p (cost=0.00..1.05 rows=5 width=8)
(5 rows)
我的问题是:如果我只是过滤了一个st值和一个构成UNIQUE INDEX的类型值,为什么这个独特的索引没有被使用?
这就是发生了什么事。我使用数百万个假行填充了表以进行测试,解释使用了所有内容的索引!我喜欢postgres! –