• Skip to main content
  • Skip to primary sidebar

学習記録

pg_trgmでDB全文検索を早くする

2020年8月31日 by 河副 太智 Leave a Comment

ローカルの場合

psqlにログインして以下を実行するだけで準備完了

1
CREATE EXTENSION pg_trgm;

インデックスの作成

1
2
3
CREATE INDEX (任意のインデックスの名前) ON (テーブル) USING gist(項目 gist_trgm_ops);
 
#例CREATE INDEX eu_index ON test_eu USING GIST (eng_discription gist_trgm_ops);

インデックスを作成したテーブルを検索した際のパフォーマンスを調べる

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#インデックス作成前のパフォーマンスを調べる
explain select * from test_eu where eng_discription ~ '.*remote controller.*' ORDER BY image_amount DESC;
QUERY PLAN
#結果
-------------------------------------------------------------------------------------
Gather Merge (cost=155601.10..155609.74 rows=74 width=1295)
Workers Planned: 2
-> Sort (cost=154601.08..154601.17 rows=37 width=1295)
Sort Key: image_amount DESC
-> Parallel Seq Scan on test_eu (cost=0.00..154600.11 rows=37 width=1295)
Filter: (eng_discription ~ '.*remote controller.*'::text)
#インデックス作成後の結果
--------------------------------------------------------------------------------
Sort (cost=462.64..462.86 rows=88 width=1295)
Sort Key: image_amount DESC
-> Bitmap Heap Scan on test_eu (cost=113.09..459.80 rows=88 width=1295)
Recheck Cond: (eng_discription ~ '.*remote controller.*'::text)
-> Bitmap Index Scan on eu_index (cost=0.00..113.07 rows=88 width=0)
Index Cond: (eng_discription ~ '.*remote controller.*'::text)

スコアの見方

1
Seq Scan on w_user (cost=0.00..178.50 rows=50 width=161)

0.00がはじめのデータ取得で178.50が最後のデータ取得の時間

 

AWSの場合

そのままだとエラーになる

1
2
rulings=# CREATE EXTENSION pg_trgm;
ERROR:  機能拡張の制御ファイル"/usr/pgsql-12/share/extension/pg_trgm.control"をオープンできませんでした: No such file or directory

contribをインストール

1
yum -y install postgresql12-contrib

再度以下を実行するだけで準備完了

1
CREATE EXTENSION pg_trgm;

CREATE EXTENSIONが出ればOK

 

このエントリーをはてなブックマークに追加

Tweet
[`yahoo` not found]
このエントリーを Google ブックマーク に追加
LinkedIn にシェア
LINEで送る


Filed Under: PosgreSQL

Reader Interactions

コメントを残す コメントをキャンセル

メールアドレスが公開されることはありません。 ※ が付いている欄は必須項目です

Primary Sidebar

カテゴリー

  • AWS
  • Bootstrap
  • Dash
  • Django
  • flask
  • GIT(sourcetree)
  • Plotly/Dash
  • VPS
  • その他tool
  • ブログ
  • プログラミング
    • Bokeh
    • css
    • HoloViews
    • Jupyter
    • Numpy
    • Pandas
    • PosgreSQL
    • Python 基本
    • python3
      • webアプリ
    • python3解説
    • scikit-learn
    • scipy
    • vps
    • Wordpress
    • グラフ
    • コマンド
    • スクレイピング
    • チートシート
    • データクレンジング
    • ブロックチェーン
    • 作成実績
    • 時系列分析
    • 機械学習
      • 分析手法
      • 教師有り
    • 異常値検知
    • 自然言語処理
  • 一太郎
  • 数学
    • sympy
      • 対数関数(log)
      • 累乗根(n乗根)
    • 暗号学

Copyright © 2025 · Genesis Sample on Genesis Framework · WordPress · Log in