PostgreSQL对空表的行估算逻辑挺奇怪的,固定认为有2000多行

点击(此处)折叠或打开

  1. postgres=# create table tbchj(id int);
  2. CREATE TABLE
  3. postgres=# explain select * from tbchj;
  4.                        QUERY PLAN
  5. ---------------------------------------------------------
  6.  Seq Scan on tbchj (cost=0.00..35.50 rows=2550 width=4)
  7. (1 row)

  8. postgres=# explain select * from tbchj where id =9;
  9.                       QUERY PLAN
  10. -------------------------------------------------------
  11.  Seq Scan on tbchj (cost=0.00..41.88 rows=13 width=4)
  12.    Filter: (id = 9)
  13. (2 rows)

  14. postgres=# analyze tbchj;
  15. ANALYZE
  16. postgres=# explain select * from tbchj;
  17.                        QUERY PLAN
  18. ---------------------------------------------------------
  19.  Seq Scan on tbchj (cost=0.00..35.50 rows=2550 width=4)
  20. (1 row)


插入数据后,行估算才能调整正确

点击(此处)折叠或打开

  1. postgres=# insert into tbchj values(1);
  2. INSERT 0 1
  3. postgres=# explain select * from tbchj;
  4.                        QUERY PLAN
  5. ---------------------------------------------------------
  6.  Seq Scan on tbchj (cost=0.00..35.50 rows=2550 width=4)
  7. (1 row)

  8. postgres=# analyze tbchj;
  9. ANALYZE
  10. postgres=# explain select * from tbchj;
  11.                      QUERY PLAN
  12. -----------------------------------------------------
  13.  Seq Scan on tbchj (cost=0.00..1.01 rows=1 width=4)
  14. (1 row)



10-09 08:43