日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > 数据库 >内容正文

数据库

PostgreSQL索引探究

發(fā)布時間:2025/5/22 数据库 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 PostgreSQL索引探究 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

創(chuàng)建包含10個列(c01 - c10)的表my_table,用如下語句創(chuàng)建2個索引,并向表中插入6w條記錄。這6w條記錄的c01列,全部都是2017年04月21日的數(shù)據(jù)。

CREATE INDEX my_table_index1ON my_tableUSING btree(c05, c01, c02) TABLESPACE smart_history_index;CREATE INDEX my_table_index2ON my_tableUSING btree(c01, c02, c03, c04, c05) TABLESPACE smart_history_index;

使用以下SQL1語句查詢,查詢時間為2分鐘。
SQL1:

select * from my_table t1 where (select count(*) from my_table t2 where ((t1.c01 = t2.c01 ) or (t1.c01 is null and t2.c01 is null)) and ((t1.c02 = t2.c02 ) or (t1.c02 is null and t2.c02 is null)) and ((t1.c03 = t2.c03 ) or (t1.c03 is null and t2.c03 is null)) and ((t1.c04 = t2.c04 ) or (t1.c04 is null and t2.c04 is null)) and ((t1.c05 = t2.c05 ) or (t1.c05 is null and t2.c05 is null)) and ((t1.c06 = t2.c06 ) or (t1.c06 is null and t2.c06 is null)) and ((t1.c07 = t2.c07 ) or (t1.c07 is null and t2.c07 is null)) and ((t1.c08 = t2.c08 ) or (t1.c08 is null and t2.c08 is null)) and ((t1.c09 = t2.c09 ) or (t1.c09 is null and t2.c09 is null)) and ((t1.c10 = t2.c10 ) or (t1.c10 is null and t2.c10 is null)) )>1;

使用以下SQL2語句查詢,查詢時間為2.8秒。
SQL2:

select * from my_table t1 where (select count(*) from my_table t2 where ((t1.c01 = t2.c01 ) or (t1.c01 is null and t2.c01 is null)) and ((t1.c02 = t2.c02 ) or (t1.c02 is null and t2.c02 is null)) and ((t1.c03 = t2.c03 ) or (t1.c03 is null and t2.c03 is null)) and ((t1.c04 = t2.c04 ) or (t1.c04 is null and t2.c04 is null)) and ((t1.c05 = t2.c05 ) or (t1.c05 is null and t2.c05 is null)) and ((t1.c06 = t2.c06 ) or (t1.c06 is null and t2.c06 is null)) and ((t1.c07 = t2.c07 ) or (t1.c07 is null and t2.c07 is null)) and ((t1.c08 = t2.c08 ) or (t1.c08 is null and t2.c08 is null)) and ((t1.c09 = t2.c09 ) or (t1.c09 is null and t2.c09 is null)) and ((t1.c10 = t2.c10 ) or (t1.c10 is null and t2.c10 is null)) and (c01 >= '2017-04-20 00:00:00' and c01 < '2017-04-21 00:00:00') )>1 and (c01 >= '2017-04-20 00:00:00' and c01 < '2017-04-21 00:00:00');

雖然SQL2最后對于c01列的where條件并為實(shí)質(zhì)上減少過濾出的數(shù)據(jù)量。但是能夠顯著的提高查詢效率(60倍),進(jìn)一步使用以下SQL3語句監(jiān)視索引使用情況發(fā)現(xiàn),SQL2只調(diào)用了my_table_index2,而SQL1既調(diào)用了my_table_index1也調(diào)用了my_table_index2。
SQL3:

select relname, indexrelname, idx_scan, idx_tup_read, idx_tup_fetch from pg_stat_user_indexes where relname = 'my_table' order by idx_scan asc, idx_tup_read asc, idx_tup_fetch asc;

顯然my_table_index2肯定比my_table_index1的辨識度更高。之所以SQL優(yōu)化解析器會做出這樣的選擇,猜測可能是因?yàn)閙y_table_index2中列c01處于第一個的位置,而SQL2最后的c01查詢條件剛好暗示強(qiáng)化了解釋器去選擇my_table_index2。
在此基礎(chǔ)上我進(jìn)行了進(jìn)一步的實(shí)驗(yàn):如果drop掉my_table_index1只保留my_table_index2,則無論是SQL1還是SQL2都能達(dá)到大約2.8秒的查詢速度;如果drop掉my_table_index2只保留my_table_index1,則無論是SQL1還是SQL2都需要2分鐘才能完成查詢;當(dāng)然my_table_index1也并不是一無是處,如果將兩個索引全部drop掉,那么做一次查詢(全表掃描)大約需要10分鐘。

最后說兩點(diǎn)啟示:
1.索引不能隨便建,如果建的不好,不僅影響插入效率,也會影響查詢效率。
2.SQL語句優(yōu)化之路,任重而道遠(yuǎn)。

總結(jié)

以上是生活随笔為你收集整理的PostgreSQL索引探究的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 欧美高清一区二区 | 无码人妻丰满熟妇啪啪 | 偷偷操99| 蜜桃精品成人影片 | 亚洲国产一二 | 亚洲免费视频网站 | 99热首页 | 一区二区国产视频 | jizz日本在线 | 亚洲国产tv| 91狠狠爱 | 国产av电影一区 | 91精品91| 欧美午夜剧场 | 成人亚洲一区 | 日本精品在线播放 | 91视频在线免费看 | 又爽av| 亚洲一卡二卡 | 韩国午夜av| 在线观看亚洲免费视频 | 日本人与黑人做爰视频 | 午夜尤物| 天天弄天天操 | 精品免费在线 | 久久久久网 | 国产成人免费在线 | 香蕉网站在线观看 | 打屁股疼的撕心裂肺的视频 | 91成品视频 | 午夜av影院 | 农村少妇久久久久久久 | 日韩精品在线网站 | 91丨porny丨露出 | 销魂奶水汁系列小说 | 特黄一区 | 国产性生活视频 | 欧美日韩在线播放三区四区 | 污污网站在线观看视频 | 殴美一级黄色片 | 亚洲精品久 | 亚洲jizzjizz| 好吊色欧美一区二区三区视频 | www.色就是色 | 潘金莲激情呻吟欲求不满视频 | 清纯唯美亚洲色图 | 日本 奴役 捆绑 受虐狂xxxx | 69天堂网 | 中文在线免费视频 | 欧美一级xxx | 九九超碰 | 亚洲春色av | 蜜桃成熟时李丽珍在线观看 | 在线观看免费高清在线观看 | 99热久久这里只有精品 | 亚洲日本香蕉视频 | 一级黄色片一级黄色片 | 中文字幕亚洲天堂 | 日韩不卡高清视频 | 日韩免费黄色片 | 亚洲天天 | 神马午夜视频 | 一级高清毛片 | 国产伦精品一区二区三区免费视频 | 熟睡侵犯の奶水授乳在线 | 青苹果av| 亚洲成肉网 | 亚洲国产精品毛片av不卡在线 | 校园激情av | 日韩免费视频一区二区视频在线观看 | 亚洲GV成人无码久久精品 | 91国产一区二区 | 精品熟妇无码av免费久久 | 四虎影视国产精品 | 国产一区二区三区精品愉拍 | mm131美女大尺度私密照尤果 | 久福利 | av在线免费播放网址 | 久草在在线 | 欧美一区二区三区久久妖精 | 国产精品高清无码在线观看 | 日本久久综合 | 日韩在线精品视频一区二区涩爱 | 黄色成人毛片 | 97超碰人人澡人人爱学生 | 久久精品日韩无码 | 久久精品—区二区三区舞蹈 | 91狠狠| 四季av在线一区二区三区 | 黑人巨大精品人妻一区二区 | 俺也去婷婷 | www.亚洲黄色| 蜜臀av免费在线观看 | 中文在线免费看视频 | 正在播放超嫩在线播放 | 日美毛片 | 91成人免费在线视频 | 亚洲天天操 | 国产伦精品一区三区精东 |