日韩性视频-久久久蜜桃-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)容還不錯,歡迎將生活随笔推薦給好友。