国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

POSTGRESQL(PG) 性能優(yōu)化之like全文檢索優(yōu)化

這篇具有很好參考價(jià)值的文章主要介紹了POSTGRESQL(PG) 性能優(yōu)化之like全文檢索優(yōu)化。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

使用like操作可以進(jìn)行字符串比較,全文檢索等,性能相對(duì)比較差,有些情況下可以通過建立索引來(lái)提升性能。下面我們通過使用TPCH orders表作為例子,來(lái)進(jìn)行說明。但是請(qǐng)注意, not like是不能用任何索引的,BTREE不支持!=操作,只能進(jìn)行=和范圍查找。

TPCH orders表的定義如下,其中o_comment列是varchar類型的字符串:

tpch10=# \d orders
                          Table "public.orders"
     Column      |         Type          | Collation | Nullable | Default
-----------------+-----------------------+-----------+----------+---------
 o_orderkey      | integer               |           | not null |
 o_custkey       | integer               |           | not null |
 o_orderstatus   | character(1)          |           | not null |
 o_totalprice    | numeric(15,2)         |           | not null |
 o_orderdate     | date                  |           | not null |
 o_orderpriority | character(15)         |           | not null |
 o_clerk         | character(15)         |           | not null |
 o_shippriority  | integer               |           | not null |
 o_comment       | character varying(79) |           | not null |
Indexes:
    "orders_pkey" PRIMARY KEY, btree (o_orderkey)
Foreign-key constraints:
    "orders_o_custkey_fkey" FOREIGN KEY (o_custkey) REFERENCES customer(c_custkey)
Referenced by:
    TABLE "lineitem" CONSTRAINT "lineitem_l_orderkey_fkey" FOREIGN KEY (l_orderkey) REFERENCES orders(o_orderkey)

1.?前匹配 LIKE ‘prefix%’范圍查詢

前匹配可以使用BTREE索引進(jìn)行范圍查詢,在沒有任何索引的情況下,PG默認(rèn)使用seqscan順序掃描:

tpch10=# explain analyze select * from orders where o_comment like 'request%';
                                                    QUERY PLAN
------------------------------------------------------------------------------------------------------------------
 Seq Scan on orders  (cost=0.00..448412.00 rows=75000 width=234) (actual time=0.662..5098.904 rows=56870 loops=1)
   Filter: ((o_comment)::text ~~ 'request%'::text)
   Rows Removed by Filter: 14943130
 Planning Time: 0.268 ms
 Execution Time: 5103.280 ms
(5 rows)

我們?cè)趏_comment列加一下BTREE索引,然后再次嘗試:

tpch10=# create index on orders(o_comment);
CREATE INDEX
tpch10=# analyze orders;
ANALYZE
tpch10=# explain analyze select * from orders where o_comment like 'request%';
                                                    QUERY PLAN
------------------------------------------------------------------------------------------------------------------
 Seq Scan on orders  (cost=0.00..448412.00 rows=75000 width=234) (actual time=0.662..5098.904 rows=56870 loops=1)
   Filter: ((o_comment)::text ~~ 'request%'::text)
   Rows Removed by Filter: 14943130
 Planning Time: 0.268 ms
 Execution Time: 5103.280 ms
(5 rows)
tpch10=# explain analyze select * from orders where o_comment like 'request';
                                                          QUERY PLAN
-------------------------------------------------------------------------------------------------------------------------------
 Index Scan using orders_o_comment_idx on orders  (cost=0.56..8.58 rows=1 width=107) (actual time=0.188..0.188 rows=0 loops=1)
   Index Cond: ((o_comment)::text = 'request'::text)
   Filter: ((o_comment)::text ~~ 'request'::text)
 Planning Time: 0.408 ms
 Execution Time: 0.250 ms
(5 rows)

發(fā)現(xiàn)PG在like ‘prefix%’并沒有使用BTREE索引,而精確匹配可以使用索引,問題在于默認(rèn)的collate是en_US.UTF-8,而不是C,將o_comment列改為C collate再進(jìn)行嘗試。使用alter table命令將o_comment列改成collate C,可以查看到o_comment列的Collation屬性是C。

tpch10=# alter table orders alter column o_comment TYPE VARCHAR(79) collate "C";
ALTER TABLE
tpch10=# \d orders
                          Table "public.orders"
     Column      |         Type          | Collation | Nullable | Default
-----------------+-----------------------+-----------+----------+---------
 o_orderkey      | integer               |           | not null |
 o_custkey       | integer               |           | not null |
 o_orderstatus   | character(1)          |           | not null |
 o_totalprice    | numeric(15,2)         |           | not null |
 o_orderdate     | date                  |           | not null |
 o_orderpriority | character(15)         |           | not null |
 o_clerk         | character(15)         |           | not null |
 o_shippriority  | integer               |           | not null |
 o_comment       | character varying(79) | C         | not null |
Indexes:
    "orders_pkey" PRIMARY KEY, btree (o_orderkey)
    "orders_o_comment_idx" btree (o_comment)
Foreign-key constraints:
    "orders_o_custkey_fkey" FOREIGN KEY (o_custkey) REFERENCES customer(c_custkey)
Referenced by:
    TABLE "lineitem" CONSTRAINT "lineitem_l_orderkey_fkey" FOREIGN KEY (l_orderkey) REFERENCES orders(o_orderkey)

tpch10=#

再執(zhí)行一次之前的語(yǔ)句,發(fā)現(xiàn)使用了創(chuàng)建的BTREE索引,時(shí)間從5103ms提升到232ms,可以看到性能得到極大提升。

tpch10=# explain analyze select * from orders where o_comment like 'request%';
                                                                QUERY PLAN
------------------------------------------------------------------------------------------------------------------------------------------
 Bitmap Heap Scan on orders  (cost=3477.31..168092.77 rows=75000 width=234) (actual time=50.869..228.043 rows=56870 loops=1)
   Filter: ((o_comment)::text ~~ 'request%'::text)
   Heap Blocks: exact=51160
   ->  Bitmap Index Scan on orders_o_comment_idx  (cost=0.00..3458.56 rows=75000 width=0) (actual time=33.213..33.213 rows=56870 loops=1)
         Index Cond: (((o_comment)::text >= 'request'::text) AND ((o_comment)::text < 'requesu'::text))
 Planning Time: 0.528 ms
 Execution Time: 232.989 ms
(7 rows)

tpch10=#

2. 后匹配 LIKE ‘%prefix’范圍查詢

后匹配的方式不能使用BTREE索引,原因在于BTREE索引只能做大于、大于等于、等于、小于、小于等于等操作決定的。因此這時(shí)候使用reverse index來(lái)處理后匹配的文本檢索。

比如我們使用相同的例子,只是like匹配的是‘%request’,PG依舊使用順序掃描,而沒有使用之前建的索引。

tpch10=# explain analyze select * from orders where o_comment like '%request';
                                                    QUERY PLAN
-------------------------------------------------------------------------------------------------------------------
 Seq Scan on orders  (cost=0.00..448412.00 rows=151515 width=107) (actual time=0.043..8640.280 rows=57305 loops=1)
   Filter: ((o_comment)::text ~~ '%request'::text)
   Rows Removed by Filter: 14942695
 Planning Time: 1.065 ms
 Execution Time: 8645.223 ms
(5 rows)

我們給o_comment列建一個(gè)reverse的索引,會(huì)看到執(zhí)行語(yǔ)句會(huì)選用reverse索引,時(shí)間由8645ms降到162ms,提升效率非常高。因此在這種情況下可以考慮使用reverse索引來(lái)提高后匹配的檢索效率。

tpch10=# create index on orders(reverse(o_comment));
CREATE INDEX
tpch10=# analyze orders;
ANALYZE

tpch10=# explain analyze select * from orders where reverse(o_comment) like reverse('%request');
                                                              QUERY PLAN
--------------------------------------------------------------------------------------------------------------------------------------
 Index Scan using orders_reverse_idx on orders  (cost=0.56..8.59 rows=1500 width=107) (actual time=0.142..157.412 rows=57305 loops=1)
   Index Cond: ((reverse((o_comment)::text) >= 'tseuqer'::text) AND (reverse((o_comment)::text) < 'tseuqes'::text))
   Filter: (reverse((o_comment)::text) ~~ 'tseuqer%'::text)
 Planning Time: 0.620 ms
 Execution Time: 162.048 ms
(5 rows)

tpch10=#

3. 中間匹配'%prefix%'范圍查詢

對(duì)于中間匹配的全文檢索,以上兩種索引都不能解決,PG提供了GIN索引,可以使用PG提供的pg_trgm插件來(lái)加速查詢。GIN索引可以應(yīng)用于前匹配、后匹配和中間匹配,但是因?yàn)镚IN本身比較昂貴,對(duì)于前匹配和后匹配,可以使用上面提到的方式,中間匹配采用GIN索引,但是也需要考慮索引維護(hù)本身的代價(jià)。

在沒有使用GIN索引時(shí)候,PG依舊使用順序掃描:

tpch10=# explain analyze select * from orders where o_comment like '%request%';
                                                      QUERY PLAN
----------------------------------------------------------------------------------------------------------------------
 Seq Scan on orders  (cost=0.00..448442.18 rows=2273093 width=107) (actual time=0.108..8773.358 rows=2318296 loops=1)
   Filter: ((o_comment)::text ~~ '%request%'::text)
   Rows Removed by Filter: 12681704
 Planning Time: 0.836 ms
 Execution Time: 8901.078 ms
(5 rows)

tpch10=# explain analyze select * from orders where reverse(o_comment) like reverse('%request%');
                                                      QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------
 Seq Scan on orders  (cost=0.00..485948.21 rows=3030791 width=107) (actual time=0.069..18095.022 rows=2318296 loops=1)
   Filter: (reverse((o_comment)::text) ~~ '%tseuqer%'::text)
   Rows Removed by Filter: 12681704
 Planning Time: 4.783 ms
 Execution Time: 18218.893 ms
(5 rows)

但是GIN索引的創(chuàng)建和維護(hù)非常昂貴,因?yàn)镚IN索引對(duì)每組三個(gè)連續(xù)字符(三元組)進(jìn)行索引,以便能夠查找任何匹配模式。而且GIN索引也可以應(yīng)用于前后匹配的組合使用,這種情況下效率也很高。

加載pg_trgm插件后,在o_comment列創(chuàng)建GIN索引,重新進(jìn)行查詢,時(shí)間由8901ms提升到3939ms,效率得到了提升,同時(shí)我們也可以看到GIN索引也可以進(jìn)行‘%prefix%prefix%’的查詢,效率也是很明顯的提升。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-755320.html

tpch10=# create extension if not exists pg_trgm;
CREATE EXTENSION
tpch10=# create index on orders using gin (o_comment gin_trgm_ops);
CREATE INDEX
tpch10=# analyze orders;
ANALYZE

tpch10=# explain analyze select * from orders where o_comment like '%request%';
                                                                     QUERY PLAN
----------------------------------------------------------------------------------------------------------------------------------------------------
 Bitmap Heap Scan on orders  (cost=29361.97..330044.89 rows=3181674 width=107) (actual time=1727.012..3828.371 rows=2318296 loops=1)
   Recheck Cond: ((o_comment)::text ~~ '%request%'::text)
   Rows Removed by Index Recheck: 2088
   Heap Blocks: exact=260892
   ->  Bitmap Index Scan on orders_o_comment_idx1  (cost=0.00..28566.55 rows=3181674 width=0) (actual time=1604.355..1604.355 rows=2320384 loops=1)
         Index Cond: ((o_comment)::text ~~ '%request%'::text)
 Planning Time: 0.473 ms
 Execution Time: 3939.557 ms
(8 rows)


tpch10=# explain analyze select * from orders where o_comment like '%special%request%';
                                                                   QUERY PLAN
-------------------------------------------------------------------------------------------------------------------------------------------------
 Bitmap Heap Scan on orders  (cost=1478.19..236736.24 rows=151508 width=107) (actual time=1794.711..2266.371 rows=168338 loops=1)
   Recheck Cond: ((o_comment)::text ~~ '%special%request%'::text)
   Rows Removed by Index Recheck: 80603
   Heap Blocks: exact=161167
   ->  Bitmap Index Scan on orders_o_comment_idx1  (cost=0.00..1440.31 rows=151508 width=0) (actual time=1731.915..1731.915 rows=248941 loops=1)
         Index Cond: ((o_comment)::text ~~ '%special%request%'::text)
 Planning Time: 0.501 ms
 Execution Time: 2278.128 ms
(8 rows)

到了這里,關(guān)于POSTGRESQL(PG) 性能優(yōu)化之like全文檢索優(yōu)化的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來(lái)自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 全文檢索-Elasticsearch-進(jìn)階檢索

    全文檢索-Elasticsearch-進(jìn)階檢索

    本文記錄谷粒商城高級(jí)篇的 Elasticsearch 進(jìn)階檢索部分,續(xù)上之前記錄的 Elasticsearch入門篇。 ES 支持兩種基本方式檢索 : 一個(gè)是通過使用 REST request URI 發(fā)送搜索參數(shù)(uri + 檢索參數(shù)) 另一個(gè)是通過使用 REST request body 來(lái)發(fā)送它們(uri + 請(qǐng)求體) 請(qǐng)求體中寫查詢條件,語(yǔ)法: 示例

    2024年02月03日
    瀏覽(28)
  • 【全文檢索】sqlite-fts4和pgsql的全文檢索對(duì)比

    【全文檢索】sqlite-fts4和pgsql的全文檢索對(duì)比

    因?yàn)槭茿ndroid項(xiàng)目,老系統(tǒng)中的全文檢索是采用sqlite自帶的fts4,然而后續(xù)由于地圖要素全部轉(zhuǎn)為線上,全文檢索也需要同步在線查詢,所以將整個(gè)全文檢索的功能遷移到pgsql中。目前這塊功能基本結(jié)束,這里來(lái)對(duì)兩種全文檢索方案做一個(gè)對(duì)比總結(jié)。 相比與fts5,fts4的好處是原生

    2024年02月05日
    瀏覽(17)
  • 全文檢索-Es-初步檢索(三)

    全文檢索-Es-初步檢索(三)

    #為jmeter返回的結(jié)果 jmeter測(cè)試結(jié)果 請(qǐng)求頭 http請(qǐng)求 put 返回結(jié)果 再次發(fā)送請(qǐng)求 post不帶/帶id保存 不帶id 結(jié)果 二次請(qǐng)求結(jié)果 帶id保存 結(jié)果 二次請(qǐng)求結(jié)果 結(jié)論 發(fā)送請(qǐng)求 查詢-查看結(jié)果樹 增加判斷,確定是否修改 結(jié)果 查看修改是否成功 結(jié)果 更新文檔 post/put帶_update的請(qǐng)求(會(huì)比

    2024年02月14日
    瀏覽(29)
  • Elasticsearch 全文檢索 分詞檢索-Elasticsearch文章四

    Elasticsearch 全文檢索 分詞檢索-Elasticsearch文章四

    https://www.elastic.co/guide/en/enterprise-search/current/start.html https://www.elastic.co/guide/en/elasticsearch/reference/7.17/query-dsl-match-query.html Full text Query中,我們只需要把如下的那么多點(diǎn)分為3大類,你的體系能力會(huì)大大提升 很多api都可以查得到,我們只要大概知道有支持哪些功能 Elasticsearch 執(zhí)行

    2024年02月14日
    瀏覽(24)
  • MySQL 全文檢索

    MySQL 全文檢索

    不是所有的數(shù)據(jù)表都支持全文檢索 MySQL支持多種底層數(shù)據(jù)庫(kù)引擎,但是并非所有的引擎支持全文檢索?,目前最常用引擎是是MyISAM和InnoDB;前者支持全文檢索,后者不支持。 操作符 含義 + 必須有 - 必須不包含 包含對(duì)應(yīng)的排名靠前 包含對(duì)應(yīng)的排名靠后 ~ 取反()

    2024年04月15日
    瀏覽(24)
  • MySQL 中文全文檢索

    創(chuàng)建索引(MySQL 5.7.6后全文件索引可用WITH PARSER ngram,針對(duì)中文,日文,韓文) 查詢方法 注意 只能在類型為CHAR、VARCHAR或者TEXT的字段上創(chuàng)建全文索引。 全文索引只支持InnoDB和MyISAM引擎。 MATCH()函數(shù)使用的字段名,必須要與創(chuàng)建全文索引時(shí)指定的字段名一致。 多個(gè)字段索引,

    2024年02月12日
    瀏覽(25)
  • MySQL中文全文檢索

    常規(guī)數(shù)據(jù)庫(kù)搜索都是用 like 語(yǔ)句,但是like 語(yǔ)句是不能利用索引的,查詢效率極其低下。這也就是為什么很多功能都只提供標(biāo)題搜索的原因,因?yàn)槿绻阉鲀?nèi)容,幾萬(wàn)數(shù)據(jù)就跑不動(dòng)了。 Mysql 全文索引是專門為了解決模糊查詢提供的,可以對(duì)整篇文章預(yù)先按照詞進(jìn)行索引,搜索

    2024年02月14日
    瀏覽(16)
  • mysql全文檢索使用

    mysql全文檢索使用

    數(shù)據(jù)庫(kù)數(shù)據(jù)量10萬(wàn)左右,使用like \\\'%test%\\\'要耗費(fèi)30秒左右,放棄該辦法 使用mysql的全文檢索 第一步:建立索引 首先修改一下設(shè)置: my.ini中ngram_token_size = 1 可以通過?? ?show variables like \\\'%token%\\\';來(lái)查看 接下來(lái)建立索引:alter ?table 表名 add fulltext titlefull (字段名) with parser ngram; 第二步

    2024年02月12日
    瀏覽(22)
  • 實(shí)現(xiàn)全文檢索的方法

    實(shí)現(xiàn)網(wǎng)站全文檢索功能,可以采取多種方法,從簡(jiǎn)單的基于數(shù)據(jù)庫(kù)的搜索到使用專門的全文檢索系統(tǒng)。以下是一些常見的實(shí)現(xiàn)全文檢索的方法: 1. **數(shù)據(jù)庫(kù)全文索引**: ? ?如果你的網(wǎng)站后端使用的是關(guān)系型數(shù)據(jù)庫(kù)(如MySQL),大多數(shù)數(shù)據(jù)庫(kù)管理系統(tǒng)都提供了全文索引的功能。

    2024年04月26日
    瀏覽(18)
  • MySQL——全文檢索

    MySQL——全文檢索

    不是所有的數(shù)據(jù)表都支持全文檢索 MySQL支持多種底層數(shù)據(jù)庫(kù)引擎,但是并非所有的引擎支持全文檢索?,目前最常用引擎是是MyISAM和InnoDB;前者支持全文檢索,后者不支持。 表productnotes : 1. 查詢包含 rabbit 的行,并按照相關(guān)性排序 ?2.顯示每一條的相關(guān)性值 3.有heavy 但是沒有

    2024年04月15日
    瀏覽(30)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包