磨砺技术珠矶,践行数据之道,追求卓越价值


根据官方文档的说明 ​​http://www.postgresql.org/docs/9.2/static/sql-analyze.html​

Analyze 的基本作用:


ANALYZE collects statistics about the contents of tables in the database, and stores the results in the pg_statistic system catalog. Subsequently, the query planner uses these statistics to help determine the most efficient execution plans for queries.


那么,auto analyze 如何设置呢:

auto analyze 由 autovacuum 发起,舍此没有别的机会:


In the default PostgreSQL configuration, the autovacuum daemon (see Section 23.1.6) takes care of automatic analyzing of tables when they are first loaded with data, and as they change throughout regular operation. When autovacuum is disabled, it is a good idea to run ANALYZE periodically, or just after making major changes in the contents of a table. Accurate statistics will help the planner to choose the most appropriate query plan, and thereby improve the speed of query processing. A common strategy for read-mostly databases is to run VACUUM and ANALYZE once a day during a low-usage time of day. (This will not be sufficient if there is heavy update activity.)


看一下 配置:


#------------------------------------------------------------------------------ # AUTOVACUUM PARAMETERS #------------------------------------------------------------------------------  #autovacuum = on                        # Enable autovacuum subprocess?  'on'                                         # requires track_counts to also be on. #log_autovacuum_min_duration = -1       # -1 disables, 0 logs all actions and                                         # their durations, > 0 logs only                                         # actions running at least this number                                         # of milliseconds. #autovacuum_max_workers = 3             # max number of autovacuum subprocesses                                         # (change requires restart) #autovacuum_naptime = 1min              # time between autovacuum runs #autovacuum_vacuum_threshold = 50       # min number of row updates before                                         # vacuum #autovacuum_analyze_threshold = 50      # min number of row updates before                                         # analyze #autovacuum_vacuum_scale_factor = 0.2   # fraction of table size before vacuum #autovacuum_analyze_scale_factor = 0.1  # fraction of table size before analyze #autovacuum_freeze_max_age = 200000000  # maximum XID age before forced vacuum                                         # (change requires restart) #autovacuum_vacuum_cost_delay = 20ms    # default vacuum cost delay for                                         # autovacuum, in milliseconds;                                         # -1 means use vacuum_cost_delay #autovacuum_vacuum_cost_limit = -1      # default vacuum cost limit for                                         # autovacuum, -1 means use                                         # vacuum_cost_limit


analyze 还有一个弱点值得注意:


If the table being analyzed has one or more children, ANALYZE will gather statistics twice: once on the rows of the parent table only, and a second time on the rows of the parent table with all of its children. This second set of statistics is needed when planning queries that traverse the entire inheritance tree. The autovacuum daemon, however, will only consider inserts or updates on the parent table itself when deciding whether to trigger an automatic analyze for that table. If that table is rarely inserted into or updated, the inheritance statistics will not be up to date unless you run ANALYZE manually.


对于有主表和子表的,仅当主表被认为需要analyze时,才会进行 analayze 动作。