2個場景實例講解GaussDB(DWS)基表統計信息估算不准的處理方案

来源:https://www.cnblogs.com/huaweiyun/archive/2023/06/02/17451676.html
-Advertisement-
Play Games

摘要:通過2個實例場景講解GaussDB(DWS)運維解決方案。 本文分享自華為雲社區《GaussDB(DWS)運維 -- 基表統計信息估算不准的常見場景及處理方案》,作者:譡里個檔。 場景1:基表過濾欄位存在的隱式類型時,基表行數估算偏小 這種場景絕大部分場景DWS能夠處理,但是如果隱式類型轉後的 ...


摘要:通過2個實例場景講解GaussDB(DWS)運維解決方案。

本文分享自華為雲社區《GaussDB(DWS)運維 -- 基表統計信息估算不准的常見場景及處理方案》,作者:譡里個檔。

場景1:基表過濾欄位存在的隱式類型時,基表行數估算偏小

這種場景絕大部分場景DWS能夠處理,但是如果隱式類型轉後的結果與統計信息中的欄位枚舉值的表達式不一樣,就會導致估算的嚴重偏差

原始SQL如下

SELECT * FROM dmgrpdi.dwl_inv_res_rpt_ci_grp_f WHERE period_id=202212 AND source_flag=1;

對應的執行計劃

                                                    QUERY PLAN
-------------------------------------------------------------------------------------------------------------------
  id |                            operation                             | E-rows | E-memory | E-width |  E-costs
 ----+------------------------------------------------------------------+--------+----------+---------+-----------
 1 | -> Row Adapter                                                  | 14160 | | 717 | 680025.43
 2 | ->  Vector Streaming (type: GATHER) | 14160 | | 717 | 680025.43
 3 | ->  Vector Partition Iterator                              | 14160 | 1MB      | 717 | 678241.33
 4 | ->  Partitioned CStore Scan on dwl_inv_res_rpt_ci_grp_f | 14160 | 1MB      | 717 | 678241.33
                  Predicate Information (identified by plan id)
 -------------------------------------------------------------------------------
 3 --Vector Partition Iterator
         Iterations: 1
 4 --Partitioned CStore Scan on dwl_inv_res_rpt_ci_grp_f
         Filter: ((period_id = 202212::numeric) AND ((source_flag)::bigint = 1))
         Pushdown Predicate Filter: (period_id = 202212::numeric)
         Partitions Selected by Static Prune: 36

發現source_flag欄位上存在隱式類型轉換,查詢欄位source_flag的統計信息

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';
 most_common_vals | most_common_freqs | histogram_bounds
------------------+-----------------------------------+------------------
 {01,02,04,03}    | {.440034,.241349,.217413,.101089} | {05,06}
(1 row)

發現隱式類型轉後的結果(1)與統計信息中的欄位枚舉值('01')的表達式不一樣

處理方案:修改過濾條件,禁止類型轉換,並且使用正確的常量值書寫過濾條件

如上SQL語句中的source_flag=1修改為source_flag='01',修改後SQL語句如下

SELECT * FROM dmgrpdi.dwl_inv_res_rpt_ci_grp_f WHERE period_id=202212 AND source_flag='01';

查詢新語句的執行計劃

                                                      QUERY PLAN
----------------------------------------------------------------------------------------------------------------------
  id |                            operation                             |  E-rows | E-memory | E-width |  E-costs
 ----+------------------------------------------------------------------+-----------+----------+---------+-----------
 1 | -> Row Adapter                                                  | 108359075 | | 717 | 480542.98
 2 | ->  Vector Streaming (type: GATHER) | 108359075 | | 717 | 480542.98
 3 | ->  Vector Partition Iterator                              | 108359075 | 1MB      | 717 | 478758.88
 4 | ->  Partitioned CStore Scan on dwl_inv_res_rpt_ci_grp_f | 108359075 | 1MB      | 717 | 478758.88
                           Predicate Information (identified by plan id)
 -------------------------------------------------------------------------------------------------
 3 --Vector Partition Iterator
         Iterations: 1
 4 --Partitioned CStore Scan on dwl_inv_res_rpt_ci_grp_f
         Filter: ((period_id = 202212::numeric) AND (source_flag = '01'::text))
         Pushdown Predicate Filter: ((period_id = 202212::numeric) AND (source_flag = '01'::text))
         Partitions Selected by Static Prune: 36

場景2:基表在多列組合主鍵上過濾時,基表行數估算偏大

這種場景是因為DWS對基表上多個過濾條件之間採取弱相關性處理,當多個過濾條件是主鍵時,可能導致結果集估算偏大。

原始SQL如下

SELECT * FROM mca.mca_period_rate_t mca_rate2
WHERE period_number = '202208' AND from_currency_code = 'RMB' AND to_currency_code = 'USD'

執行信息如下

 id |                      operation                       |       A-time | A-rows | E-rows | Peak Memory | E-memory | A-width | E-width | E-costs  
----+------------------------------------------------------+--------------------+--------+--------+-------------+----------+---------+---------+----------
 1 | -> Row Adapter                                      | 444.735 | 1 | 2033 | 227KB       | | | 321 | 22601.41 
 2 | ->  Vector Streaming (type: GATHER) | 444.720 | 1 | 2033 | 873KB       | | | 321 | 22601.41 
 3 | -> CStore Scan on mca_period_rate_t mca_rate2 | [435.167, 435.167] | 1 | 2033 | [5MB, 5MB] | 1MB      | | 321 | 22427.41 
                                                              Predicate Information (identified by plan id) 
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 3 --CStore Scan on mca_period_rate_t mca_rate2
        Filter: (((period_number)::text = '202208'::text) AND ((from_currency_code)::text = 'RMB'::text) AND ((to_currency_code)::text = 'USD'::text))
 Rows Removed by Filter: 425812
        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的定義

SELECT pg_get_tabledef('mca.mca_period_rate_t'::regclass);

查詢表mca.mca_period_rate_t定義

SELECT pg_get_tabledef('mca.mca_period_rate_t');
SET search_path = mca;
CREATE TABLE mca_period_rate_t (
seq numeric NOT NULL,
period_number character varying(10) NOT NULL,
from_currency_code character varying(20) NOT NULL,
to_currency_code character varying(20) NOT NULL,
begin_rate numeric(35,18),
end_rate numeric(35,18),
avg_rate numeric(35,18),
creation_date timestamp(0) without time zone NOT NULL,
created_by numeric NOT NULL,
last_update_date timestamp(0) without time zone,
last_updated_by numeric,
rmb_begin_rate numeric(35,18),
usd_begin_rate numeric(35,18),
rmb_end_rate numeric(35,18),
usd_end_rate numeric(35,18),
rmb_avg_rate numeric(35,18),
usd_avg_rate numeric(35,18),
crt_cycle_id numeric,
crt_job_instance_id numeric,
last_upd_cycle_id numeric,
upd_job_instance_id numeric,
cdc_key_id character varying(128) DEFAULT sys_guid(),
end_rate2 numeric(35,18),
avg_rate2 numeric(35,18),
last_period_end_rate numeric(35,18)
)
WITH (orientation=column, compression=low, colversion=2.0, enable_delta=false)
DISTRIBUTE BY REPLICATION
TO GROUP group_version1;
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) 多列統計信息

ANALYZE mca.mca_period_rate_t((period_number, from_currency_code, to_currency_code));

收集多列統計信息之後,基表的行數估算恢復正產

 id |                                      operation                                      |       A-time | A-rows | E-rows | Peak Memory | A-width | E-width | E-costs 
----+-------------------------------------------------------------------------------------+--------------------+--------+--------+-------------+---------+---------+---------
 1 | -> Row Adapter                                                                     | 195.504 | 1 | 1 | 227KB       | | 321 | 675.14 
 2 | ->  Vector Streaming (type: GATHER) | 195.491 | 1 | 1 | 873KB       | | 321 | 675.14 
 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 
                                                      Predicate Information (identified by plan id) 
----------------------------------------------------------------------------------------------------------------------------------------------------------
 3 --CStore Index Scan using mca_period_rate_u1 on mca_period_rate_t mca_rate2
 Index Cond: (((period_number)::text = '202208'::text) AND ((from_currency_code)::text = 'RMB'::text) AND ((to_currency_code)::text = 'USD'::text))

 

點擊關註,第一時間瞭解華為雲新鮮技術~


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • > 安裝轉載:https://www.cnblogs.com/saryli/p/9729591.html > > 插件轉載:https://blog.csdn.net/nbdclw/article/details/107441772 #### 安裝及配置環境 ##### 第一步:下載並安裝erlan ...
  • 此日海光新至,為其接風飲宴。席間其樂融融,眾CPU互報姓名,曰:海光C86-3250、龍芯3A5000、飛騰D2000、兆芯KX-U6780A。其間海光3250言其太上蠻橫,只許子弟行走於伺服器和工作站之間,圍坐桌面乃是越矩,此番被拘於此方知桌面之妙,願以文會友,以人鑒己。老夫雖知其本意,卻亦有意相... ...
  • kprobe_events shell模式使用教程 kprobe 使用前提 需要內核啟用以下配置 CONFIG_KPROBES=y CONFIG_HAVE_KPROBES=y CONFIG_KPROBE_EVENT=y kprobe_events kprobe_events有兩種類型:kprobe, ...
  • ![](https://img2023.cnblogs.com/blog/3076680/202306/3076680-20230602231102469-1389179464.png) # 1. 基礎思想 ## 1.1. 預寫日誌記錄 ## 1.2. 兩階段提交 ## 1.3. 關係資料庫 # 2 ...
  • # Redis Redis是一個開源(BSD許可高性能的記憶體存儲的key-value資料庫! 可用作資料庫,高速緩存和消息隊列代理。它支持字元串、哈希表、列表(List)、集合(Set)、有序集合(Ordered Sets),點陣圖(bitmap),hyperloglogs,GEO等數據類型。內置複製 ...
  • 有很多朋友對中國文化歷史相關的數據感興趣,現有的中華上下五千年、世界五千年這類的數據記錄數還太少太少,於是今天就採集了一個中華歷史網站,共有效採集到近8萬條記錄。 分類彙總情況:野史秘聞(12273)、歷史人物(8840)、歷史雜談(7928)、文史百科(5635)、歷史趣聞(5282)、雜說歷史( ...
  • 這段時間破解了中高學生知識題庫,包含高中英語題庫、小學英語題庫、初中地理題庫、初中歷史題庫、高中歷史題庫、初中生物題庫,數據表結構都一樣,今天發的這份是上萬條的高中歷史題庫,截圖包含所有欄位,截圖下方有顯示共有記錄數。 參考項有:古代中國的政治制度(2846)、近代中國的民主革命(2493)、古代中 ...
  • 摘要:一起看一下GaussDB(for MySQL)是如何對執行計划進行緩存並加速Prepared Statement性能的。 本文分享自華為雲社區《執行計劃緩存,Prepared Statement性能躍升的秘密》,作者: GaussDB 資料庫。 引言 在資料庫系統中,SQL(Structure ...
一周排行
    -Advertisement-
    Play Games
  • 前言 在我們開發過程中基本上不可或缺的用到一些敏感機密數據,比如SQL伺服器的連接串或者是OAuth2的Secret等,這些敏感數據在代碼中是不太安全的,我們不應該在源代碼中存儲密碼和其他的敏感數據,一種推薦的方式是通過Asp.Net Core的機密管理器。 機密管理器 在 ASP.NET Core ...
  • 新改進提供的Taurus Rpc 功能,可以簡化微服務間的調用,同時可以不用再手動輸出模塊名稱,或調用路徑,包括負載均衡,這一切,由框架實現並提供了。新的Taurus Rpc 功能,將使得服務間的調用,更加輕鬆、簡約、高效。 ...
  • 順序棧的介面程式 目錄順序棧的介面程式頭文件創建順序棧入棧出棧利用棧將10進位轉16進位數驗證 頭文件 #include <stdio.h> #include <stdbool.h> #include <stdlib.h> 創建順序棧 // 指的是順序棧中的元素的數據類型,用戶可以根據需要進行修改 ...
  • 前言 整理這個官方翻譯的系列,原因是網上大部分的 tomcat 版本比較舊,此版本為 v11 最新的版本。 開源項目 從零手寫實現 tomcat minicat 別稱【嗅虎】心有猛虎,輕嗅薔薇。 系列文章 web server apache tomcat11-01-官方文檔入門介紹 web serv ...
  • C總結與剖析:關鍵字篇 -- <<C語言深度解剖>> 目錄C總結與剖析:關鍵字篇 -- <<C語言深度解剖>>程式的本質:二進位文件變數1.變數:記憶體上的某個位置開闢的空間2.變數的初始化3.為什麼要有變數4.局部變數與全局變數5.變數的大小由類型決定6.任何一個變數,記憶體賦值都是從低地址開始往高地 ...
  • 如果讓你來做一個有狀態流式應用的故障恢復,你會如何來做呢? 單機和多機會遇到什麼不同的問題? Flink Checkpoint 是做什麼用的?原理是什麼? ...
  • C++ 多級繼承 多級繼承是一種面向對象編程(OOP)特性,允許一個類從多個基類繼承屬性和方法。它使代碼更易於組織和維護,並促進代碼重用。 多級繼承的語法 在 C++ 中,使用 : 符號來指定繼承關係。多級繼承的語法如下: class DerivedClass : public BaseClass1 ...
  • 前言 什麼是SpringCloud? Spring Cloud 是一系列框架的有序集合,它利用 Spring Boot 的開發便利性簡化了分散式系統的開發,比如服務註冊、服務發現、網關、路由、鏈路追蹤等。Spring Cloud 並不是重覆造輪子,而是將市面上開發得比較好的模塊集成進去,進行封裝,從 ...
  • class_template 類模板和函數模板的定義和使用類似,我們已經進行了介紹。有時,有兩個或多個類,其功能是相同的,僅僅是數據類型不同。類模板用於實現類所需數據的類型參數化 template<class NameType, class AgeType> class Person { publi ...
  • 目錄system v IPC簡介共用記憶體需要用到的函數介面shmget函數--獲取對象IDshmat函數--獲得映射空間shmctl函數--釋放資源共用記憶體實現思路註意 system v IPC簡介 消息隊列、共用記憶體和信號量統稱為system v IPC(進程間通信機制),V是羅馬數字5,是UNI ...