這篇文章主要講解了“PostgreSQL 統(tǒng)計(jì)信息在計(jì)算選擇率上的應(yīng)用分析”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來(lái)研究和學(xué)習(xí)“PostgreSQL 統(tǒng)計(jì)信息在計(jì)算選擇率上的應(yīng)用分析”吧!
成都創(chuàng)新互聯(lián)是一家專業(yè)提供阜陽(yáng)企業(yè)網(wǎng)站建設(shè),專注與成都網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)、html5、小程序制作等業(yè)務(wù)。10年已為阜陽(yáng)眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站制作公司優(yōu)惠進(jìn)行中。單條件等值查詢
測(cè)試數(shù)據(jù)生成腳本如下:
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腳本和執(zhí)行計(jì)劃:
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)
雖然都是等值查詢,但執(zhí)行計(jì)劃中dwbh='6323'和dwbh='24'返回的行數(shù)(rows)卻不一樣,一個(gè)是rows=30,一個(gè)是rows=10,從生成數(shù)據(jù)的腳本來(lái)看,'6323'和'24'的rows應(yīng)該是一樣的,但執(zhí)行計(jì)劃顯示的結(jié)果卻不同,原因是計(jì)算選擇率時(shí)'6323'出現(xiàn)在高頻值中,因此與其他值不同.
計(jì)算過(guò)程解析
查詢?cè)摿械慕y(tǒng)計(jì)信息:
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 |
條件語(yǔ)句是等值表達(dá)式,使用的操作符是"="(字符串等值比較,texteq/eqsel/eqjoinsel),因此使用的統(tǒng)計(jì)信息是高頻值MCV(注意:staop1=98,這是字符串等值比較).'6323'出現(xiàn)在高頻值中,選擇率為0.0003,因此rows=100,000x0.0003=30.而'24'沒(méi)有出現(xiàn)在高頻值中,選擇率=(1-0.0003)/abs(stadistinct)/Tuples=(1-0.0003)/abs(-0.10015)/100000=0.000099820269595606590000,rows=(1-0.0003)/abs(stadistinct)=10(取整).
單條件比較查詢
測(cè)試腳本:
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列的統(tǒng)計(jì)信息
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 |
查詢語(yǔ)句:
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),結(jié)合統(tǒng)計(jì)信息中MCV和直方圖使用,
由于2312均小于MCV中的值,因此根據(jù)MCV得出的選擇率為0.
根據(jù)直方圖計(jì)算的選擇率=(1-0.0003)x(23+(2312-2287-1)/(2388-2287))/100=0.2323065247,rows=100000x0.2323065247=23231(取整)
其中:
除高頻值外的其他數(shù)值占比=(1-0.0003)
直方圖中的總槽數(shù)=數(shù)組元素總數(shù)-1即101-1=100
2312落在第24個(gè)槽中,槽占比=(23+(2312-2287-1)/(2388-2287))/100
感謝各位的閱讀,以上就是“PostgreSQL 統(tǒng)計(jì)信息在計(jì)算選擇率上的應(yīng)用分析”的內(nèi)容了,經(jīng)過(guò)本文的學(xué)習(xí)后,相信大家對(duì)PostgreSQL 統(tǒng)計(jì)信息在計(jì)算選擇率上的應(yīng)用分析這一問(wèn)題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
新聞標(biāo)題:PostgreSQL統(tǒng)計(jì)信息在計(jì)算選擇率上的應(yīng)用分析-創(chuàng)新互聯(lián)
網(wǎng)頁(yè)鏈接:http://www.rwnh.cn/article24/ccjdje.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護(hù)、網(wǎng)站收錄、用戶體驗(yàn)、品牌網(wǎng)站建設(shè)、關(guān)鍵詞優(yōu)化、ChatGPT
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容