Greenplum 自动统计信息收集 - 暨统计信息不准引入的broadcast motion一例

标签

PostgreSQL , Greenplum , 统计信息 , 自动统计信息 , broadcast motion , 执行计划


背景

数据库执行计划的好坏,与数据库的SQL优化器息息相关。Greenplum有两套优化器,legacy query optimizer 与 ORCA。

这两个优化器都是CBO优化器,都需要依赖统计信息,如果统计信息不准确,可能生成的执行计划就不准确。

例如我们有一个这样的QUERY,发现怎么跑都跑不出来。

观察执行计划,发现有一个节点用到了broadcast motion,也就是广播。

->  Broadcast Motion 512:512  (slice1; segments: 512)  (cost=0.00..6.13 rows=1 width=16)
  ->  Append-only Columnar Scan on xxxx  (cost=0.00..1.00 rows=1 width=16)

当JOIN字段并非分布键时,Greenplum会根据表的大小,选择重分布或广播。(小表广播,大表的话多阶段JOIN)。

《HybridDB PostgreSQL "Sort、Group、distinct 聚合、JOIN" 不惧怕数据倾斜的黑科技和原理 - 多阶段聚合》

而这个执行计划跑偏的SQL,恰恰是在一个大表上触发了广播(broadcast motion),这不正常。

select count(*) xxxx;  

返回有31亿数据。

查询pg_class,值有1条记录,占用0个BLOCK

select * from pg_class where relname='xxxx';  

relpages       | 1
reltuples      | 0

执行analyze,收集统计信息,执行计划恢复。

(Greenplum对于超级大表,收集统计信息时,会构建临时表来进行采样分析)

digoal=> analyze verbose xxxxxxxx;  

INFO:  Executing SQL: select sum(gp_statistics_estimate_reltuples_relpages_oid(c.oid))::float4[] from gp_dist_random('pg_class') c where c.oid=112293
INFO:  ANALYZE estimated reltuples=3091824896.000000, relpages=47509120.000000 for table xxxxxxxx
INFO:  ANALYZE building sample table of size 173762 on table xxxxxxxx because it has too many rows.
INFO:  Executing SQL: create table pg_temp.pg_analyze_112293_59 as (  select Ta.xx,....Ta.xxx  from public.xxxxxxxx as Ta where random() < 0.00005620053343591280281543731689453125 limit 173762  ) distributed randomly
INFO:  Created sample table pg_temp.pg_analyze_112293_59 with nrows=173762
INFO:  ANALYZE computing statistics on attribute xx
INFO:  Executing SQL: select count(*)::float4 from pg_temp_440803.pg_analyze_112293_59 as Ta where Ta.xx is null
INFO:  nullfrac = 0.178474
INFO:  Executing SQL: select avg(pg_column_size(Ta.xx))::float4 from pg_temp_440803.pg_analyze_112293_59 as Ta where Ta.xx is not null
INFO:  avgwidth = 21.418087
INFO:  Executing SQL: select count(*)::float4 from (select Ta.xx from pg_temp_440803.pg_analyze_112293_59 as Ta group by Ta.xx) as Tb
INFO:  count(ndistinct()) gives 142751.000000 values.
INFO:  Executing SQL: select count(v)::float4 from (select Ta.xx as v, count(Ta.xx) as f from pg_temp_440803.pg_analyze_112293_59 as Ta group by Ta.xx) as foo where f > 1
INFO:  ndistinct = -1.000000
..........

收集统计信息后,执行计划恢复,没有broadcast了,执行也秒级返回了。

让Greenplum自动收集统计信息

对于在函数内 或 函数外执行DML时,内核会跟踪表的记录数变更影响的数据量,我们可以设置什么时候收集统计信息:

none:不收集

on_no_stats:没有统计信息时,收集

on_change:当写入、更新量超过阈值(gp_autostats_on_change_threshold参数设置的行数,默认为20亿)后,自动收集统计信息。

Automatic Statistics Collection  

Greenplum Database can be set to automatically run ANALYZE on a table that either has no statistics or has
changed significantly when certain operations are performed on the table. For partitioned tables, automatic
statistics collection is only triggered when the operation is run directly on a leaf table, and then only the leaf
table is analyzed.  

Automatic statistics collection has three modes:
• none disables automatic statistics collection.
• on_no_stats triggers an analyze operation for a table with no existing statistics when any of the
commands CREATE TABLE AS SELECT, INSERT, or COPY are executed on the table.
• on_change triggers an analyze operation when any of the commands CREATE TABLE AS SELECT,
UPDATE, DELETE, INSERT, or COPY are executed on the table and the number of rows affected exceeds
the threshold defined by the gp_autostats_on_change_threshold configuration parameter.  

The automatic statistics collection mode is set separately for commands that occur within a procedural
language function and commands that execute outside of a function:
• The gp_autostats_mode configuration parameter controls automatic statistics collection behavior
outside of functions and is set to on_no_stats by default.
• The gp_autostats_mode_in_functions parameter controls the behavior when table operations are
performed within a procedural language function and is set to none by default.  

With the on_change mode, ANALYZE is triggered only if the number of rows affected exceeds the threshold
defined by the gp_autostats_on_change_threshold configuration parameter. The default value for this
parameter is a very high value, 2147483647, which effectively disables automatic statistics collection;
you must set the threshold to a lower number to enable it. The on_change mode could trigger large,
unexpected analyze operations that could disrupt the system, so it is not recommended to set it globally. It
could be useful in a session, for example to automatically analyze a table following a load.  

To disable automatic statistics collection outside of functions, set the gp_autostats_mode parameter to
none:  

gpconfigure -c gp_autostats_mode -v none  

To enable automatic statistics collection in functions for tables that have no statistics, change
gp_autostats_mode_in_functions to on_no_stats:  

gpconfigure -c gp_autostats_mode_in_functions -v on_no_stats  

Set the log_autostats system configuration parameter to on if you want to log automatic statistics
collection operations.

为了让数据库产生准确的执行计划,建议要么用户自己调度analyZE收集统计信息,要么自动收集。

时间: 2024-07-29 05:18:39

Greenplum 自动统计信息收集 - 暨统计信息不准引入的broadcast motion一例的相关文章

oracle 数据库统计信息收集

Statistic 对Oracle 是非常重要的. 它会收集数据库中对象的详细信息,并存储在相应的数据字典里. 根据这些统计信息, optimizer 可以对每个SQL 去选择最好的执行计划. 在9i 及之前的版本,在选择执行计划的时候会根据RBO(Rule-BasedOptimization)或者CBO来分析. 10g及以后版本只支持CBO(Cost-BasedOptimization).  优化器收集的统计信息包括如下内容:             1)Table statistics   

Oracle 12c数据库优化器统计信息收集的最佳实践

Oracle 12c数据库优化器统计信息收集的最佳实践 转载自     沃趣科技(ID:woqutech)  作者         刘金龙(译) 原文链接   http://www.oracle.com/technetwork/database/bi-datawarehousing/twp-bp-for-stats-gather-12c-1967354.pdf 导  语 Oracle优化器会为SQL语句产生所有可能的访问路径(执行计划),然后从中选择一条COST值最低的执行路径,这个cost值是

《Storm实时数据处理》一2.6 统计与持久化日志统计信息

2.6 统计与持久化日志统计信息 我们其实可以收集很多日志流的统计信息,但由于本节只是为了展示一些基本概念,因此我们只准备收集和处理单时间序列(每分钟)日志量.虽说如此,但这应该已经足以展示有关的设计和方法了,这些技巧能够帮助你在未来实现其他分析工作. 2.6.1 实战 Step01 下载storm-cassandra contrib项目并将其安装到Maven仓库中: Step02 在storm.cookbook.log包中,创建一个继承自BaseRichBolt的类VolumeCounting

王建冬:中美欧三地现代信息服务业发展水平统计研究

本文系国务院第二次全国经济普查后期开发重点项目"信息服务业发展的国际比较研究"成果之一. 王建冬,男,1982年生,博士,国家信息中心信息化研究部助理研究员,国家信息中心网络政府研究中心咨询二部主任,研究方向:网络用户行为与政府信息服务创新. 童楠楠,女,1989年生,国家信息中心信息化研究部,研究方向:公共政策研究.电子政务. 1 引言 20世纪后半叶以来,信息技术突飞猛进发展,以及服务经济在全球经济结构中越来越占据的主导地位,使得信息技术产业和服务经济之间的交叉地带成为新经济形态不

人人都是 DBA(XV)锁信息收集脚本汇编

原文:人人都是 DBA(XV)锁信息收集脚本汇编 什么?有个 SQL 执行了 8 秒! 哪里出了问题?臣妾不知道啊,得找 DBA 啊. DBA 人呢?离职了!!擦!!! 程序员在无处寻求帮助时,就得想办法自救,努力让自己变成 "伪 DBA". 索引 查看 Session 对应的 Thread 和当前 Command 侦测 Deadlocking 或阻塞问题 查看 Task 执行中哪个 Wait Type 最慢 查看当前 Task 的运行情况 查看 Lock Waits 状态 查看 La

收集优化统计数据(Optimizer Statistics)的最佳实践方法

介绍oracle优化器对一个sql语句检测所有可能的执行计划并选择一个成本值最小的,这里的成本代表了一个特定执行计划的资源使用情况.为了让优化器能准确的判断一个执行计划的成本它必须要关于sql语句要访问的所有对象(表或索引)的信息同时还要有运行sql语句的系统信息. 这些必要的信息通常称为优化器统计信息.理解和管理优化器统计信息是优化sql执行的关键.知道何时以及如何收集统计信息对于维护可以接受的性能来说至关重要. 这里将介绍在常见的oracle数据库场景中何时以及如何来收集统计信息.它包含以下

ORACLE性能诊断―学习statspack笔记(四)[扩展statspack收集服务器统计]

oracle|笔记|服务器|统计|性能 ORACLE性能诊断―学习statspack笔记(四)[扩展statspack收集服务器统计]   作者:刘颖博 时间:2004-3-22 mail:liuyingbo@126.com,请指正   转载请注明出处及作者   说明:由于STATSPACK并不能获取全面分析性能问题所需要的所有信息,所以需要扩展其收集服务器的统计信息.(本文环境REDHAT Linux7.2)   VMSTAT介绍 通过STATSPACK收集服务器信息,主要通过收集VMSTAT

绝密追踪:利用像素图片收集攻击目标信息

本文讲的是绝密追踪:利用像素图片收集攻击目标信息,网络犯罪团伙正在滥用一种常见的邮件营销手段.通过名为"像素图片追踪"(pixel tracking)的技术,他们可以收集攻击目标的网络信息提高钓鱼攻击效率. 像素图片追踪是一门古老的技艺,已有数十年历史.它通常表现为一张在邮件里嵌入的1*1像素.透明色或和背景色相同的图片. 当用户收到嵌有像素图片(也称作信标)的邮件时,如果没有设置默认手动加载图片,打开邮件会从发送者服务器下载图片,对方就能知道你已经打开了邮件. 来看看一段像素追踪的代

T-SQL性能调整——信息收集

原文:T-SQL性能调整--信息收集 IO信息(自服务器启动开始) --Database IO analysis WITH IOFORDATABASE AS ( SELECT DB_NAME(VFS.database_id) AS DatabaseName , CASE WHEN smf.type = 1 THEN 'LOG_FILE' ELSE 'DATA_FILE' END AS DatabaseFile_type , SUM(VFS.num_of_bytes_written) AS IO_