ToB企服应用市场:ToB评测及商务社交产业平台

标题: 2个场景实例讲解GaussDB(DWS)基表统计信息估算不准的处理方案 [打印本页]

作者: 北冰洋以北    时间: 2023-6-2 15:21
标题: 2个场景实例讲解GaussDB(DWS)基表统计信息估算不准的处理方案
摘要:通过2个实例场景讲解GaussDB(DWS)运维解决方案。
本文分享自华为云社区《GaussDB(DWS)运维 -- 基表统计信息估算不准的常见场景及处理方案》,作者:譡里个檔。
场景1:基表过滤字段存在的隐式类型时,基表行数估算偏小

这种场景绝大部分场景DWS能够处理,但是如果隐式类型转后的结果与统计信息中的字段枚举值的表达式不一样,就会导致估算的严重偏差
原始SQL如下
  1. SELECT * FROM dmgrpdi.dwl_inv_res_rpt_ci_grp_f WHERE period_id=202212 AND source_flag=1;
复制代码
对应的执行计划
  1.                                                     QUERY PLAN
  2. -------------------------------------------------------------------------------------------------------------------
  3.   id |                            operation                             | E-rows | E-memory | E-width |  E-costs
  4. ----+------------------------------------------------------------------+--------+----------+---------+-----------
  5. 1 | -> Row Adapter                                                  | 14160 | | 717 | 680025.43
  6. 2 | ->  Vector Streaming (type: GATHER) | 14160 | | 717 | 680025.43
  7. 3 | ->  Vector Partition Iterator                              | 14160 | 1MB      | 717 | 678241.33
  8. 4 | ->  Partitioned CStore Scan on dwl_inv_res_rpt_ci_grp_f | 14160 | 1MB      | 717 | 678241.33
  9.                   Predicate Information (identified by plan id)
  10. -------------------------------------------------------------------------------
  11. 3 --Vector Partition Iterator
  12.          Iterations: 1
  13. 4 --Partitioned CStore Scan on dwl_inv_res_rpt_ci_grp_f
  14.          Filter: ((period_id = 202212::numeric) AND ((source_flag)::bigint = 1))
  15.          Pushdown Predicate Filter: (period_id = 202212::numeric)
  16.          Partitions Selected by Static Prune: 36
复制代码
发现source_flag字段上存在隐式类型转换,查询字段source_flag的统计信息
  1. postgres=# SELECT most_common_vals,most_common_freqs, histogram_bounds  FROM pg_stats WHERE tablename = 'dwl_inv_res_rpt_ci_grp_f' AND attname = 'source_flag';
  2. most_common_vals | most_common_freqs | histogram_bounds
  3. ------------------+-----------------------------------+------------------
  4. {01,02,04,03}    | {.440034,.241349,.217413,.101089} | {05,06}
  5. (1 row)
复制代码
发现隐式类型转后的结果(1)与统计信息中的字段枚举值('01')的表达式不一样
处理方案:修改过滤条件,禁止类型转换,并且使用正确的常量值书写过滤条件

如上SQL语句中的source_flag=1修改为source_flag='01',修改后SQL语句如下
  1. SELECT * FROM dmgrpdi.dwl_inv_res_rpt_ci_grp_f WHERE period_id=202212 AND source_flag='01';
复制代码
查询新语句的执行计划
  1.                                                       QUERY PLAN
  2. ----------------------------------------------------------------------------------------------------------------------
  3.   id |                            operation                             |  E-rows | E-memory | E-width |  E-costs
  4. ----+------------------------------------------------------------------+-----------+----------+---------+-----------
  5. 1 | -> Row Adapter                                                  | 108359075 | | 717 | 480542.98
  6. 2 | ->  Vector Streaming (type: GATHER) | 108359075 | | 717 | 480542.98
  7. 3 | ->  Vector Partition Iterator                              | 108359075 | 1MB      | 717 | 478758.88
  8. 4 | ->  Partitioned CStore Scan on dwl_inv_res_rpt_ci_grp_f | 108359075 | 1MB      | 717 | 478758.88
  9.                            Predicate Information (identified by plan id)
  10. -------------------------------------------------------------------------------------------------
  11. 3 --Vector Partition Iterator
  12.          Iterations: 1
  13. 4 --Partitioned CStore Scan on dwl_inv_res_rpt_ci_grp_f
  14.          Filter: ((period_id = 202212::numeric) AND (source_flag = '01'::text))
  15.          Pushdown Predicate Filter: ((period_id = 202212::numeric) AND (source_flag = '01'::text))
  16.          Partitions Selected by Static Prune: 36
复制代码
场景2:基表在多列组合主键上过滤时,基表行数估算偏大

这种场景是因为DWS对基表上多个过滤条件之间采取弱相关性处理,当多个过滤条件是主键时,可能导致结果集估算偏大。
原始SQL如下
  1. SELECT * FROM mca.mca_period_rate_t mca_rate2
  2. WHERE period_number = '202208' AND from_currency_code = 'RMB' AND to_currency_code = 'USD'
复制代码
执行信息如下
  1. id |                      operation                       |       A-time | A-rows | E-rows | Peak Memory | E-memory | A-width | E-width | E-costs  
  2. ----+------------------------------------------------------+--------------------+--------+--------+-------------+----------+---------+---------+----------
  3. 1 | -> Row Adapter                                      | 444.735 | 1 | 2033 | 227KB       | | | 321 | 22601.41
  4. 2 | ->  Vector Streaming (type: GATHER) | 444.720 | 1 | 2033 | 873KB       | | | 321 | 22601.41
  5. 3 | -> CStore Scan on mca_period_rate_t mca_rate2 | [435.167, 435.167] | 1 | 2033 | [5MB, 5MB] | 1MB      | | 321 | 22427.41
  6.                                                               Predicate Information (identified by plan id)
  7. -------------------------------------------------------------------------------------------------------------------------------------------------------------------------
  8. 3 --CStore Scan on mca_period_rate_t mca_rate2
  9.         Filter: (((period_number)::text = '202208'::text) AND ((from_currency_code)::text = 'RMB'::text) AND ((to_currency_code)::text = 'USD'::text))
  10. Rows Removed by Filter: 425812
  11.         Pushdown Predicate Filter: (((period_number)::text = '202208'::text) AND ((from_currency_code)::text = 'RMB'::text) AND ((to_currency_code)::text = 'USD'::text))
复制代码
可以发现基表mca.mca_period_rate_t的行数估算严重偏大。
使用如下SQL语句查看表mca.mca_period_rate_t的定义
  1. SELECT pg_get_tabledef('mca.mca_period_rate_t'::regclass);
复制代码
查询表mca.mca_period_rate_t定义
  1. SELECT pg_get_tabledef('mca.mca_period_rate_t');
  2. SET search_path = mca;
  3. CREATE TABLE mca_period_rate_t (
  4. seq numeric NOT NULL,
  5. period_number character varying(10) NOT NULL,
  6. from_currency_code character varying(20) NOT NULL,
  7. to_currency_code character varying(20) NOT NULL,
  8. begin_rate numeric(35,18),
  9. end_rate numeric(35,18),
  10. avg_rate numeric(35,18),
  11. creation_date timestamp(0) without time zone NOT NULL,
  12. created_by numeric NOT NULL,
  13. last_update_date timestamp(0) without time zone,
  14. last_updated_by numeric,
  15. rmb_begin_rate numeric(35,18),
  16. usd_begin_rate numeric(35,18),
  17. rmb_end_rate numeric(35,18),
  18. usd_end_rate numeric(35,18),
  19. rmb_avg_rate numeric(35,18),
  20. usd_avg_rate numeric(35,18),
  21. crt_cycle_id numeric,
  22. crt_job_instance_id numeric,
  23. last_upd_cycle_id numeric,
  24. upd_job_instance_id numeric,
  25. cdc_key_id character varying(128) DEFAULT sys_guid(),
  26. end_rate2 numeric(35,18),
  27. avg_rate2 numeric(35,18),
  28. last_period_end_rate numeric(35,18)
  29. )
  30. WITH (orientation=column, compression=low, colversion=2.0, enable_delta=false)
  31. DISTRIBUTE BY REPLICATION
  32. TO GROUP group_version1;
  33. CREATE UNIQUE INDEX mca_period_rate_u1 ON mca.mca_period_rate_t USING cbtree (period_number, from_currency_code, to_currency_code) TABLESPACE pg_default;
复制代码
发现 (period_number, from_currency_code, to_currency_code) 为组合的唯一索引。
处理方案:对组合索引列收多列统计信息

注意此种方案只适用在基表比较小的情况下。因为多列统计信息需要使用百分比采样的方式计算统计信息,当表比较大时,统计信息计算耗时回很长。
针对如上查询语句执行如下语句收集(period_number, from_currency_code, to_currency_code) 多列统计信息
  1. ANALYZE mca.mca_period_rate_t((period_number, from_currency_code, to_currency_code));
复制代码
收集多列统计信息之后,基表的行数估算恢复正产
  1. id |                                      operation                                      |       A-time | A-rows | E-rows | Peak Memory | A-width | E-width | E-costs
  2. ----+-------------------------------------------------------------------------------------+--------------------+--------+--------+-------------+---------+---------+---------
  3. 1 | -> Row Adapter                                                                     | 195.504 | 1 | 1 | 227KB       | | 321 | 675.14
  4. 2 | ->  Vector Streaming (type: GATHER) | 195.491 | 1 | 1 | 873KB       | | 321 | 675.14
  5. 3 | -> CStore Index Scan using mca_period_rate_u1 on mca_period_rate_t mca_rate2 | [164.344, 164.344] | 1 | 1 | [5MB, 5MB] | | 321 | 501.14
  6.                                                       Predicate Information (identified by plan id)
  7. ----------------------------------------------------------------------------------------------------------------------------------------------------------
  8. 3 --CStore Index Scan using mca_period_rate_u1 on mca_period_rate_t mca_rate2
  9. Index Cond: (((period_number)::text = '202208'::text) AND ((from_currency_code)::text = 'RMB'::text) AND ((to_currency_code)::text = 'USD'::text))
复制代码
 
点击关注,第一时间了解华为云新鲜技术~

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4