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

  • 时间:
  • 浏览:0
  • 来源:新大发快三—大发彩票APP

(1 row)

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

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

music=> analyze verbose rock;

INFO:  analyzing "eric.music"

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

 relname |         last_analyze          

(1 row)

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

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

CREATE INDEX

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

music->  $$

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

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

INSERT 0 0

INFO:  analyzing "eric.rock"

ANALYZE

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

music$>       END IF;

  以下是实验帕累托图:

music=> select * from rock;

(1 row)

music-> BEFORE INSERT ON  music

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

                          Table "eric.music"

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

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

CREATE TABLE

 name   | text    |           | extended |              | 

music=> analyze verbose music ;

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

CREATE TABLE

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

music=> select * from pop;

 relname | last_analyze 

music$>  END;

music$>  BEGIN

  2 | Have a Nice Day | pop

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

CREATE INDEX

 id |  name  | style 

INFO:  analyzing "eric.pop"

CREATE INDEX

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

CREATE TRIGGER

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

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

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

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

 relname |         last_analyze          

CREATE TABLE

music=> analyze verbose pop;

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

ANALYZE

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

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

music$>  $$

 id     | integer |           | plain    |              | 

 relname | last_analyze 

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

INSERT 0 0

(1 row)

 relname |         last_analyze          

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

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

  女女网友认为在Analyze分区表的然后,非要看多主表的analyze日期更新到最新,然后子表的日期没法变化,他认为analyze应该是在分析主表的然后会将与之相关的子表一起去更新。

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

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

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

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

  2 | Have a Nice Day | pop

ANALYZE

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

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

(1 row)

(2 rows)

(1 row)

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

              pop,

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

CREATE FUNCTION

 id |      name       | style 

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

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

(1 row)

CREATE TABLE

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

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

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

 style  | text    |           | extended |              | 

 id |      name       | style 

 rock    | 

 relname | last_analyze 

  1 | 21 Gun          | rock

INFO:  analyzing "eric.music" inheritance tree

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

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

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

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

music=> CREATE TRIGGER insert_music_trigger 

Child tables: classic,

              jazz,

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

  1 | 21 Gun | rock

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

music$>       RETURN NULL;

music=> select * from music;

CREATE INDEX

 pop     | 

music->  LANGUAGE plpgsql ;

music=> \dS+ music

CREATE TABLE

music=>  CREATE OR REPLACE FUNCTION music_insert_trigger()

music->  RETURNS TRIGGER AS 

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

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

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

              rock

 rock    | 

(1 row)

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