PostgreSQL对空表的行估算逻辑挺奇怪的,固定认为有2000多行。
点击(此处)折叠或打开
-
postgres=# create table tbchj(id int);
-
CREATE TABLE
-
postgres=# explain select * from tbchj;
-
QUERY PLAN
-
---------------------------------------------------------
-
Seq Scan on tbchj (cost=0.00..35.50 rows=2550 width=4)
-
(1 row)
-
-
postgres=# explain select * from tbchj where id =9;
-
QUERY PLAN
-
-------------------------------------------------------
-
Seq Scan on tbchj (cost=0.00..41.88 rows=13 width=4)
-
Filter: (id = 9)
-
(2 rows)
-
-
postgres=# analyze tbchj;
-
ANALYZE
-
postgres=# explain select * from tbchj;
-
QUERY PLAN
-
---------------------------------------------------------
-
Seq Scan on tbchj (cost=0.00..35.50 rows=2550 width=4)
-
(1 row)
-
插入数据后,行估算才能调整正确
点击(此处)折叠或打开
-
postgres=# insert into tbchj values(1);
-
INSERT 0 1
-
postgres=# explain select * from tbchj;
-
QUERY PLAN
-
---------------------------------------------------------
-
Seq Scan on tbchj (cost=0.00..35.50 rows=2550 width=4)
-
(1 row)
-
-
postgres=# analyze tbchj;
-
ANALYZE
-
postgres=# explain select * from tbchj;
-
QUERY PLAN
-
-----------------------------------------------------
-
Seq Scan on tbchj (cost=0.00..1.01 rows=1 width=4)
- (1 row)