亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

PostgreSQL模糊匹配走索引的操作

 更新時(shí)間:2021年01月26日 08:41:11   作者:JackGo_73  
這篇文章主要介紹了PostgreSQL模糊匹配走索引的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

場(chǎng)景 lower(name) like 'pf%'

create table users (id int primary key, name varchar(255));
Create or replace function random_string(length integer) returns text as
$$
declare
 chars text[] := '{0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z}';
 result text := '';
 i integer := 0;
begin
 if length < 0 then
 raise exception 'Given length cannot be less than 0';
 end if;
 for i in 1..length loop
 result := result || chars[1+random()*(array_length(chars, 1)-1)];
 end loop;
 return result;
end;
$$ language plpgsql;
insert into users values(generate_series(1,50000), random_string(15));

普通bt:不走索引

pg_trgm模塊提供函數(shù)和操作符測(cè)定字母數(shù)字文本基于三元模型匹配的相似性,還有支持快速搜索相似字符串的索引操作符類。三元模型是一組從一個(gè)字符串中獲得的三個(gè)連續(xù)的字符。我們可以通過(guò)計(jì)數(shù)兩個(gè)字符串共享的三元模型的數(shù)量來(lái)測(cè)量它們的相似性。這個(gè)簡(jiǎn)單的想法證明在測(cè)量許多自然語(yǔ)言詞匯的相似性時(shí)是非常有效的。

CREATE INDEX users_idx0 ON users (name);

全字匹配查詢(走索引)

explain select * from users where name='pfDNQVmhqDrF1EY';
        QUERY PLAN
-------------------------------------------------------------------------
 Index Scan using users_idx0 on users (cost=0.29..8.31 rows=1 width=20)
 Index Cond: ((name)::text = 'pfDNQVmhqDrF1EY'::text)
(2 rows)

加函數(shù)全字匹配(不走索引)

explain select * from users where lower(name)='pfDNQVmhqDrF1EY';
      QUERY PLAN
-----------------------------------------------------------
 Seq Scan on users (cost=0.00..1069.00 rows=250 width=20)
 Filter: (lower((name)::text) = 'pfDNQVmhqDrF1EY'::text)
(2 rows)

模糊匹配(不走索引)

explain select * from users where name like 'pf%';
      QUERY PLAN
--------------------------------------------------------
 Seq Scan on users (cost=0.00..944.00 rows=5 width=20)
 Filter: ((name)::text ~~ 'pf%'::text)
explain select * from users where name like 'pf_';
      QUERY PLAN
--------------------------------------------------------
 Seq Scan on users (cost=0.00..944.00 rows=5 width=20)
 Filter: ((name)::text ~~ 'pf_'::text)

字段帶函數(shù)的bt索引:函數(shù)走索引

drop index users_idx0;
CREATE INDEX users_dex1 ON users (lower(name));

加函數(shù)全字匹配(走索引)

explain select * from users where lower(name)='pfDNQVmhqDrF1EY';
        QUERY PLAN
---------------------------------------------------------------------------
 Bitmap Heap Scan on users (cost=6.23..324.34 rows=250 width=20)
 Recheck Cond: (lower((name)::text) = 'pfDNQVmhqDrF1EY'::text)
 -> Bitmap Index Scan on users_dex1 (cost=0.00..6.17 rows=250 width=0)
   Index Cond: (lower((name)::text) = 'pfDNQVmhqDrF1EY'::text)
(4 rows)

模糊匹配(不走索引)

explain select * from users where lower(name) like 'pf%';
      QUERY PLAN
-----------------------------------------------------------
 Seq Scan on users (cost=0.00..1069.00 rows=250 width=20)
 Filter: (lower((name)::text) ~~ 'pf%'::text)
(2 rows)

聲明操作符類的bt索引:like走索引

定義索引的同時(shí)可以為索引的每個(gè)字段聲明一個(gè)操作符類。

CREATE INDEX name ON table (column opclass [sort options] [, …]);

這個(gè)操作符類指明該索引用于該字段時(shí)要使用的操作符。

CREATE INDEX users_dex2 ON users (lower(name) varchar_pattern_ops);

模糊匹配(走索引)

explain select * from users where lower(name) like 'pf%';
            QUERY PLAN
------------------------------------------------------------------------------------------------------
 Bitmap Heap Scan on users (cost=4.82..144.00 rows=5 width=20)
 Filter: (lower((name)::text) ~~ 'pf%'::text)
 -> Bitmap Index Scan on users_dex2 (cost=0.00..4.82 rows=53 width=0)
   Index Cond: ((lower((name)::text) ~>=~ 'pf'::text) AND (lower((name)::text) ~<~ 'pg'::text))
(4 rows)

場(chǎng)景2 name like '%pf%'

Create or replace function random_string(length integer) returns text as
$$
declare
 chars text[] := '{0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z}';
 result text := '';
 i integer := 0;
begin
 if length < 0 then
 raise exception 'Given length cannot be less than 0';
 end if;
 for i in 1..length loop
 result := result || chars[1+random()*(array_length(chars, 1)-1)];
 end loop;
 return result;
end;
$$ language plpgsql;
create table users (id int primary key, name varchar(255));
insert into users values(generate_series(1,50000), random_string(15));

聲明操作符bt:不走索引

CREATE INDEX idx_name ON users USING btree (lower(name) varchar_pattern_ops);
explain (analyze true,format yaml, verbose true, buffers true) select * from users where lower(name) like '%pf%';\
      QUERY PLAN
-----------------------------------------------------------
 - Plan:             +
  Node Type: "Seq Scan"        +
  Parallel Aware: false        +
  Relation Name: "users"        +
  Schema: "public"          +
  Alias: "users"          +
  Startup Cost: 0.00         +
  Total Cost: 1069.00         +
  Plan Rows: 5           +
  Plan Width: 20          +
  Actual Startup Time: 0.320       +
  Actual Total Time: 86.841       +
  Actual Rows: 710          +
  Actual Loops: 1          +
  Output:            +
  - "id"            +
  - "name"           +
  Filter: "(lower((users.name)::text) ~~ '%pf%'::text)"+
  Rows Removed by Filter: 49290      +
  Shared Hit Blocks: 319        +
  Shared Read Blocks: 0        +
  Shared Dirtied Blocks: 0        +
  Shared Written Blocks: 0        +
  Local Hit Blocks: 0         +
  Local Read Blocks: 0         +
  Local Dirtied Blocks: 0        +
  Local Written Blocks: 0        +
  Temp Read Blocks: 0         +
  Temp Written Blocks: 0        +
 Planning Time: 0.188         +
 Triggers:            +
 Execution Time: 86.975

聲明pg_trgm操作符bt:可以走索引

CREATE EXTENSION pg_trgm;
CREATE INDEX idx_users_name_trgm_gist ON users USING gist (name gist_trgm_ops);
explain (analyze true, verbose true, buffers true) select * from users where name like '%pf%';
                QUERY PLAN
------------------------------------------------------------------------------------------------------------------------------------------
 Bitmap Heap Scan on public.users (cost=32.19..371.08 rows=505 width=20) (actual time=19.314..53.132 rows=193 loops=1)
 Output: id, name
 Recheck Cond: ((users.name)::text ~~ '%pf%'::text)
 Rows Removed by Index Recheck: 49807
 Heap Blocks: exact=319
 Buffers: shared hit=972
 -> Bitmap Index Scan on idx_users_name_trgm_gist (cost=0.00..32.06 rows=505 width=0) (actual time=19.175..19.175 rows=50000 loops=1)
   Index Cond: ((users.name)::text ~~ '%pf%'::text)
   Buffers: shared hit=653
 Planning time: 0.188 ms
 Execution time: 53.231 ms
(11 rows)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。

相關(guān)文章

  • 如何將postgresql數(shù)據(jù)庫(kù)表內(nèi)數(shù)據(jù)導(dǎo)出為excel格式(推薦)

    如何將postgresql數(shù)據(jù)庫(kù)表內(nèi)數(shù)據(jù)導(dǎo)出為excel格式(推薦)

    這篇文章主要介紹了如何將postgresql數(shù)據(jù)庫(kù)表內(nèi)數(shù)據(jù)導(dǎo)出為excel格式(推薦),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-03-03
  • postgresql行轉(zhuǎn)列與列轉(zhuǎn)行圖文教程

    postgresql行轉(zhuǎn)列與列轉(zhuǎn)行圖文教程

    PostgreSQL是一種開(kāi)源的關(guān)系型數(shù)據(jù)庫(kù),它提供了多種管理工具來(lái)操作數(shù)據(jù)庫(kù),下面這篇文章主要給大家介紹了關(guān)于postgresql行轉(zhuǎn)列與列轉(zhuǎn)行的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-06-06
  • PostgreSQL字符切割:substring函數(shù)的用法說(shuō)明

    PostgreSQL字符切割:substring函數(shù)的用法說(shuō)明

    這篇文章主要介紹了PostgreSQL字符切割:substring函數(shù)的用法說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-02-02
  • PostgreSQL數(shù)據(jù)庫(kù)視圖及子查詢使用操作

    PostgreSQL數(shù)據(jù)庫(kù)視圖及子查詢使用操作

    這篇文章主要為大家介紹了PostgreSQL數(shù)據(jù)庫(kù)視圖及子查詢的使用操作,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪
    2022-04-04
  • PostgreSQL出現(xiàn)死鎖該如何解決

    PostgreSQL出現(xiàn)死鎖該如何解決

    昨天在對(duì)一張表執(zhí)行一條update語(yǔ)句的時(shí)候,沒(méi)有修改成功,直接終止執(zhí)行,就瘋狂點(diǎn)擊執(zhí)行,執(zhí)行了很多次這條語(yǔ)句導(dǎo)致了表被死鎖了,這篇文章主要給大家介紹了關(guān)于PostgreSQL出現(xiàn)死鎖該如何解決的相關(guān)資料,需要的朋友可以參考下
    2022-05-05
  • postgresql~*符號(hào)的含義及用法說(shuō)明

    postgresql~*符號(hào)的含義及用法說(shuō)明

    這篇文章主要介紹了postgresql~*符號(hào)的含義及用法說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-01-01
  • 在PostgreSQL中實(shí)現(xiàn)數(shù)據(jù)的自動(dòng)清理和過(guò)期清理

    在PostgreSQL中實(shí)現(xiàn)數(shù)據(jù)的自動(dòng)清理和過(guò)期清理

    在 PostgreSQL 中,可以通過(guò)多種方式實(shí)現(xiàn)數(shù)據(jù)的自動(dòng)清理和過(guò)期處理,以確保數(shù)據(jù)庫(kù)不會(huì)因?yàn)榇鎯?chǔ)過(guò)多過(guò)時(shí)或不再需要的數(shù)據(jù)而導(dǎo)致性能下降和存儲(chǔ)空間浪費(fèi),本文給大家介紹了一些常見(jiàn)的方法及詳細(xì)示例,需要的朋友可以參考下
    2024-07-07
  • SQLCipher數(shù)據(jù)遷移到PostgreSql詳細(xì)教程

    SQLCipher數(shù)據(jù)遷移到PostgreSql詳細(xì)教程

    這篇文章主要介紹了SQLCipher數(shù)據(jù)遷移到PostgreSql詳細(xì)教程,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2025-09-09
  • Postgresql排序與limit組合場(chǎng)景性能極限優(yōu)化詳解

    Postgresql排序與limit組合場(chǎng)景性能極限優(yōu)化詳解

    這篇文章主要介紹了Postgresql排序與limit組合場(chǎng)景性能極限優(yōu)化詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-12-12
  • PostgreSQL 備份與恢復(fù)實(shí)戰(zhàn)操作pg_dump / pg_restore 全方位指南

    PostgreSQL 備份與恢復(fù)實(shí)戰(zhàn)操作pg_dump / pg_restore 

    本文將帶你深入掌握 PostgreSQL 最核心的備份恢復(fù)工具 —— pg_dump 和 pg_restore,涵蓋邏輯備份、物理備份、增量備份、自動(dòng)化腳本等企業(yè)級(jí)實(shí)踐,感興趣的朋友跟隨小編一起看看吧
    2025-10-10

最新評(píng)論