您好,登錄后才能下訂單哦!
這篇文章主要講解了“PostgreSQL 統計信息在計算選擇率上的應用分析”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“PostgreSQL 統計信息在計算選擇率上的應用分析”吧!
單條件等值查詢
測試數據生成腳本如下:
insert into t_grxx(dwbh,grbh,xm,xb,nl) select generate_series(1,100000)/10||'',generate_series(1,100000),'XM'||generate_series(1,100000), (case when (floor(random()*2)=0) then '男' else '女' end),floor(random() * 100 + 1)::int;
SQL腳本和執行計劃:
testdb=# explain verbose select * from t_grxx where dwbh = '6323'; QUERY PLAN ---------------------------------------------------------------------------------------- Index Scan using idx_t_grxx_dwbh on public.t_grxx (cost=0.29..46.90 rows=30 width=24) Output: dwbh, grbh, xm, xb, nl Index Cond: ((t_grxx.dwbh)::text = '6323'::text) (3 rows) testdb=# explain verbose select * from t_grxx where dwbh = '24'; QUERY PLAN ---------------------------------------------------------------------------------------- Index Scan using idx_t_grxx_dwbh on public.t_grxx (cost=0.29..20.29 rows=10 width=24) Output: dwbh, grbh, xm, xb, nl Index Cond: ((t_grxx.dwbh)::text = '24'::text) (3 rows)
雖然都是等值查詢,但執行計劃中dwbh='6323'和dwbh='24'返回的行數(rows)卻不一樣,一個是rows=30,一個是rows=10,從生成數據的腳本來看,'6323'和'24'的rows應該是一樣的,但執行計劃顯示的結果卻不同,原因是計算選擇率時'6323'出現在高頻值中,因此與其他值不同.
計算過程解析
查詢該列的統計信息:
testdb=# \x Expanded display is on. testdb=# select starelid,staattnum,stainherit,stanullfrac,stawidth,stadistinct from pg_statistic where starelid = 16742 and staattnum = 1; -[ RECORD 1 ]--------- starelid | 16742 staattnum | 1 stainherit | f stanullfrac | 0 stawidth | 4 stadistinct | -0.10015 testdb=# select staattnum,stakind1,staop1,stanumbers1,stavalues1, stakind2,staop2,stanumbers2,stavalues2, stakind3,staop3,stanumbers3,stavalues3 from pg_statistic where starelid = 16742 and staattnum = 1; -[ RECORD 1 ]---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- staattnum | 1 stakind1 | 1 staop1 | 98 stanumbers1 | {0.0003} stavalues1 | {6323} stakind2 | 2 staop2 | 664 stanumbers2 | stavalues2 | {0,1092,1181,1265,1350,1443,1529,1619,171,1797,1887,1972,2058,2151,2240,2334,2423,2520,2618,271,2798,2892,2987,3076,3162,3246,3332,3421,3510,3597,3685,3777,3860,3956,4051,4136,4227,4317,4408,45,4590,4671,4760,4850,4933,5025,5120,5210,5300,5396,548,5570,5656,5747,5835,5931,6017,6109,6190,6281,6374,6465,6566,6649,6735,6830,6921,7012,7101,7192,7278,737,7455,7544,7630,7711,7801,7895,7988,8081,8167,8260,8344,8430,8520,8615,8707,8809,8901,8997,9083,918,9272,9367,9451,9538,9630,9729,982,9904,9999} stakind3 | 3 staop3 | 664 stanumbers3 | {0.819578} stavalues3 |
條件語句是等值表達式,使用的操作符是"="(字符串等值比較,texteq/eqsel/eqjoinsel),因此使用的統計信息是高頻值MCV(注意:staop1=98,這是字符串等值比較).'6323'出現在高頻值中,選擇率為0.0003,因此rows=100,000x0.0003=30.而'24'沒有出現在高頻值中,選擇率=(1-0.0003)/abs(stadistinct)/Tuples=(1-0.0003)/abs(-0.10015)/100000=0.000099820269595606590000,rows=(1-0.0003)/abs(stadistinct)=10(取整).
單條件比較查詢
測試腳本:
testdb=# create table t_int(c1 int,c2 varchar(20)); CREATE TABLE testdb=# testdb=# insert into t_int select generate_series(1,100000)/10,'C2'||generate_series(1,100000)/100; INSERT 0 100000 testdb=# ANALYZE t_int; ANALYZE testdb=# select oid from pg_class where relname='t_int'; oid ------- 16755 (1 row)
查詢c1列的統計信息
testdb=# \x Expanded display is on. testdb=# select staattnum,stakind1,staop1,stanumbers1,stavalues1, testdb-# stakind2,staop2,stanumbers2,stavalues2, testdb-# stakind3,staop3,stanumbers3,stavalues3 testdb-# from pg_statistic testdb-# where starelid = 16755 testdb-# and staattnum = 1; -[ RECORD 1 ]--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- staattnum | 1 stakind1 | 1 staop1 | 96 stanumbers1 | {0.0003} stavalues1 | {8306} stakind2 | 2 staop2 | 97 stanumbers2 | stavalues2 | {0,108,215,318,416,514,611,704,809,912,1015,1111,1217,1312,1410,1511,1607,1705,1805,1903,2002,2094,2189,2287,2388,2487,2592,2695,2795,2896,2998,3112,3213,3304,3408,3507,3606,3707,3798,3908,4004,4106,4205,4312,4413,4505,4606,4714,4821,4910,5014,5118,5220,5321,5418,5516,5613,5709,5807,5916,6014,6127,6235,6341,6447,6548,6648,6741,6840,6931,7032,7131,7234,7330,7433,7532,7626,7727,7827,7925,8020,8120,8217,8322,8420,8525,8630,8730,8831,8934,9032,9128,9223,9323,9425,9527,9612,9706,9804,9904,9999} stakind3 | 3 staop3 | 97 stanumbers3 | {1} stavalues3 |
查詢語句:
testdb=# explain verbose select * from t_int where c1 < 2312; QUERY PLAN ------------------------------------------------------------------- Seq Scan on public.t_int (cost=0.00..1790.00 rows=23231 width=9) Output: c1, c2 Filter: (t_int.c1 < 2312) (3 rows)
SQL使用了非等值查詢(<,int4lt/scalarltsel/scalarltjoinsel),結合統計信息中MCV和直方圖使用,
由于2312均小于MCV中的值,因此根據MCV得出的選擇率為0.
根據直方圖計算的選擇率=(1-0.0003)x(23+(2312-2287-1)/(2388-2287))/100=0.2323065247,rows=100000x0.2323065247=23231(取整)
其中:
除高頻值外的其他數值占比=(1-0.0003)
直方圖中的總槽數=數組元素總數-1即101-1=100
2312落在第24個槽中,槽占比=(23+(2312-2287-1)/(2388-2287))/100
感謝各位的閱讀,以上就是“PostgreSQL 統計信息在計算選擇率上的應用分析”的內容了,經過本文的學習后,相信大家對PostgreSQL 統計信息在計算選擇率上的應用分析這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。