1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【悩めるpsql初心者へ...】ubuntu18→CentOS7でpostgresql13のヒント句を使った(泣)

Posted at

概要

ubuntuだとrpmが使えないため、うまくpg_hint_planが使えなかった。
そこでcentOS7を入れてrpm使ってインストールした。

記事ごとに割と言っていることがバラバラで本当に4日間ほどこのアンサーにたどり着くのに時間がかかった。就職以来一番時間をかけたエラーだと思う。

なんとか早く対象者にこの記事が見つかればと思う。

環境

(PostgreSQL) 13.5
centOS 7

OS切り替え(CentOS7をvagrantで立ち上げる場合)

$ vagrant init

vagrantファイルを変更し、CentOS7を仮想環境に入れる設定

Vagrant.configure("2") do |config|
  config.vm.box = "centos/7"
end
# 仮想環境立ち上げ。時間かかる
$ vagrant up
# 仮想環境接続できる
$ vagrant ssh

rpmをvagrantに転送

ここからインストールしたrpmをvagrant環境にコピーする

ゲストOSから下記を実行

# ホストOSからゲストOSにコピーするためにvagrant-scpプラグインをインストール
$ vagrant plugin install vagrant-scp
# コピー
$ vagrant scp pg_hint_plan13-1.3.7-1.el7.x86_64.rpm :/home/vagrant

postgres13をインストール

仮想環境のvagrantユーザーで

$ sudo yum -y install epel-release yum-utils
$ sudo yum-config-manager --enable pgdg13
$ sudo yum -y install postgresql13 postgresql13-server
$ sudo /usr/pgsql-13/bin/postgresql-13-setup initdb
$ sudo systemctl enable --now postgresql-13
# postgresが正常に動いているか確認
$ systemctl status postgresql-13
# postgresユーザーになる
$ sudo -i -u postgres
# DB操作できる
$ psql

rpmインストーラーを使ってpg_hint_planをインストール

$ exitでvagrantユーザーになって、sudo権限で下記を実行

$ sudo rpm --install pg_hint_plan13-1.3.7-1.el7.x86_64.rpm
# 確認のためにもう一度
$ sudo rpm --install pg_hint_plan13-1.3.7-1.el7.x86_64.rpm
	package pg_hint_plan13-1.3.7-1.el7.x86_64 is already installed

postgresqlに入って設定

$ psql
# db作成
postgres= create database test
# testデータベースを選択
postgres= \c test
postgres= LOAD 'pg_hint_plan';
LOAD

設定の更新

# だいぶ下の方にある。(Shift+gで一番下までひとっ飛び!!)
$ sudo vi /var/lib/pgsql/13/data/postgresql.conf
shared_preload_libraries = 'pg_hint_plan'
# 更新
$ sudo service postgresql-13 restart

確認

$ psql
postgres= \c test

postgres= 
/*+ SeqScan(users) */ 
EXPLAIN(ANALYZE,BUFFERS)
SELECT
count(*)
FROM
users;

tips

ヒント句は、

  • 統計情報を定期的に更新しない
  • データ分布が変わらない
  • 想定外の実行計画が選択された場合のコスト増大がすさまじい

の条件が揃った時は使うことを検討してもいいかもしれない

参考

How To Install PostgreSQL 12 on CentOS 7 / CentOS 8

pg_hint_planで実行計画を制御する

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?