PostgreSQL Analyze分区表:主表与子表的统计信息问题

  • 时间:
  • 浏览:0

music=> create table pop (check(style = 'pop')) inherits(music);

music$>       ELSEIF (NEW.style = 'classic') THEN

CREATE TABLE

  1 | 21 Gun | rock

CREATE INDEX

(1 row)

music=> select * from music;

              pop,

music->  LANGUAGE plpgsql ;

INFO:  "pop": scanned 1 of 1 pages, containing 1 live rows and 0 dead rows; 1 rows in sample, 1 estimated total rows

 pop     | 

CREATE TABLE

music=> insert into music values(1,'21 Gun','rock');

INFO:  analyzing "eric.rock"

CREATE FUNCTION

music=> select * from pop;

----+-----------------+-------

 music   | 2016-02-18 22:29:56.528758-08

INSERT 0 0

INFO:  "music": scanned 0 of 0 pages, containing 0 live rows and 0 dead rows; 0 rows in sample, 0 estimated total rows

 id     | integer |           | plain    |              | 

 relname |         last_analyze          

INFO:  analyzing "eric.music"

(1 row)

 relname |         last_analyze          

(1 row)

  为此测试了一下,发现无论主表还是子表,还要单独的对其进行analyze不可否 更新其最新的统计信息。

music=> \dS+ music

 relname |         last_analyze          

(1 row)

ANALYZE

music$>           INSERT INTO jazz VALUES (NEW.*);

music=> create index music_classic_id on classic (id);

music=>  CREATE OR REPLACE FUNCTION music_insert_trigger()

music=> analyze verbose pop;

music=> select relname, last_analyze from pg_stat_user_tables where relname = 'rock';

music=> CREATE TRIGGER insert_music_trigger 

music=> insert into music values(2,'Have a Nice Day','pop');

---------+--------------

--------+---------+-----------+----------+--------------+-------------

music=> select relname, last_analyze from pg_stat_user_tables where relname = 'rock';

CREATE TABLE

music$>           INSERT INTO pop VALUES (NEW.*);

INFO:  "rock": scanned 1 of 1 pages, containing 1 live rows and 0 dead rows; 1 rows in sample, 1 estimated total rows

music=> create table rock (check(style = 'rock')) inherits(music);

music=> analyze verbose rock;

 id |  name  | style 

  2 | Have a Nice Day | pop

  1 | 21 Gun          | rock

 rock    | 

CREATE TABLE

 id |      name       | style 

INFO:  "pop": scanned 1 of 1 pages, containing 1 live rows and 0 dead rows; 1 rows in sample, 1 estimated total rows

----+--------+-------

Child tables: classic,

music$>           INSERT INTO pop VALUES (NEW.*);

music=>  create index music_rock_id on rock (id);

music=> select relname, last_analyze from pg_stat_user_tables where relname = 'music';

 style  | text    |           | extended |              | 

  2 | Have a Nice Day | pop

music$>           INSERT INTO classic VALUES (NEW.*);

music=> select * from rock;

INFO:  analyzing "eric.pop"

                          Table "eric.music"

CREATE INDEX

music$>       END IF;

music=> select relname, last_analyze from pg_stat_user_tables where relname = 'pop';

  以下是实验每种:

 name   | text    |           | extended |              | 

music=> select relname, last_analyze from pg_stat_user_tables where relname = 'rock';

music->  $$

---------+--------------

(1 row)

CREATE TABLE

music$>       ELSEIF (NEW.style = 'pop') THEN

INFO:  "rock": scanned 1 of 1 pages, containing 1 live rows and 0 dead rows; 1 rows in sample, 1 estimated total rows

music=> create table classic (check(style = 'classic')) inherits(music);

(1 row)

music$>  $$

              rock

CREATE INDEX

CREATE INDEX

ANALYZE

 relname | last_analyze 

              jazz,

  最近在PostgreSQL的Mail List中参与讨论了一一个多 多问题报告 报告 : ANALYZE'ing table hierarchies。

music->  RETURNS TRIGGER AS 

INSERT 0 0

music-> FOR EACH row EXECUTE PROCEDURE music_insert_trigger() ;

(1 row)

 rock    | 

music$>       RETURN NULL;

 rock    | 2016-02-18 22:34:16.526558-08

 Column |  Type   | Modifiers | Storage  | Stats target | Description 

music$>  END;

ANALYZE

 pop     | 2016-02-18 22:31:55.666556-08

  外国日本日本网友认为在Analyze分区表的后后,这麼 就看主表的analyze日期更新到最新,因此子表的日期这麼 变化,他认为analyze应该是在分析主表的后后会将与之相关的子表一起更新。

music-> BEFORE INSERT ON  music

(1 row)

CREATE TRIGGER

music$>       IF (NEW.style = 'rock') THEN

music=> create table music(id int,name text,style text);

music=> analyze verbose music ;

 relname | last_analyze 

music$>           INSERT INTO rock VALUES (NEW.*);

INFO:  analyzing "eric.music" inheritance tree

music=> select relname, last_analyze from pg_stat_user_tables where relname = 'pop';

---------+--------------

music=> create index music_jazz_id on jazz (id);

music$>  BEGIN

music$>       ELSEIF (NEW.style = 'pop') THEN

---------+-------------------------------

---------+-------------------------------

----+-----------------+-------

(2 rows)

music$>       ELSEIF (NEW.style = 'jazz') THEN

music=> create index music_pop_id on pop (id);

 relname | last_analyze 

music=>  create table jazz (check(style = 'jazz')) inherits(music);

 id |      name       | style 

---------+-------------------------------