Successfully reported this slideshow.
Your SlideShare is downloading. ×

MySQLとPostgreSQLの基本的なパラメータ比較

Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad

Check these out next

1 of 11 Ad

MySQLとPostgreSQLの基本的なパラメータ比較

Download to read offline

MySQLとPostgreSQL運用で必要になる基本的なサーバーパラメータの確認方法や設定方法等。

MySQLとPostgreSQL運用で必要になる基本的なサーバーパラメータの確認方法や設定方法等。

Advertisement
Advertisement

More Related Content

Slideshows for you (20)

Similar to MySQLとPostgreSQLの基本的なパラメータ比較 (20)

Advertisement

More from Shinya Sugiyama (19)

Recently uploaded (20)

Advertisement

MySQLとPostgreSQLの基本的なパラメータ比較

  1. 1. Compared Version MySQL PostgreSQL root@localhost [mysql]> select @@version,now(); +-----------+---------------------+ | @@version | now() | +-----------+---------------------+ | 8.0.18 | 2019-11-04 01:50:06 | +-----------+---------------------+ 1 row in set (0.00 sec) postgres=# select version(); version -------------------------------------- PostgreSQL 12.0 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-39), 64-bit (1 行) PostgreSQL 12 Release date: 2019-10-03 https://www.postgresql.org/docs/12/release-12.html MySQL 8.0.18 Release date: 2019-10-14 https://dev.mysql.com/doc/relnotes/mysql/8.0/en/news-8-0-18.html
  2. 2. Server Parameters MySQL PostgreSQL [mysql]> show global variables like 'innodb_buffer%'; +-------------------------------------+----------------+ | Variable_name | Value | +-------------------------------------+----------------+ | innodb_buffer_pool_chunk_size | 134217728 | | innodb_buffer_pool_dump_at_shutdown | ON | | innodb_buffer_pool_dump_now | OFF | | innodb_buffer_pool_dump_pct | 25 | | innodb_buffer_pool_filename | ib_buffer_pool | | innodb_buffer_pool_in_core_file | ON | | innodb_buffer_pool_instances | 1 | | innodb_buffer_pool_load_abort | OFF | | innodb_buffer_pool_load_at_startup | ON | | innodb_buffer_pool_load_now | OFF | | innodb_buffer_pool_size | 268435456 | +-------------------------------------+----------------+ 11 rows in set (0.00 sec) postgres=# select name,setting,unit,context from pg_settings where name like '%buffer%'; name | setting | unit | context ----------------+---------+------+------------ shared_buffers | 16384 | 8kB | postmaster temp_buffers | 1024 | 8kB | user wal_buffers | 512 | 8kB | postmaster (3 行) postgres=# show shared_buffers; shared_buffers ---------------- 128MB (1 行) postgres=# show all; MySQLのパラメータはSHOWコマンドを利用して参照する事が可能です。 その他、performance_schemaから確認する事も可能です。 PostgreSQLに設定されている値は、pg_settings Viewや、 SHOWコマンドで確認する事が可能。 pg_settingsはサーバの実行時パラメータへのアクセスを提供します。
  3. 3. MySQL: performance_schemaでの確認 MySQL root@localhost [performance_schema]> select * from variables_info where VARIABLE_NAME like 'innodb_buffer%' ; +-------------------------------------+-----------------+---------------+-----------+---------------------+----------+----------+----------+ | VARIABLE_NAME | VARIABLE_SOURCE | VARIABLE_PATH | MIN_VALUE | MAX_VALUE | SET_TIME | SET_USER | SET_HOST | +-------------------------------------+-----------------+---------------+-----------+---------------------+----------+----------+----------+ | innodb_buffer_pool_chunk_size | COMPILED | | 1048576 | 144115188075855871 | NULL | NULL | NULL | | innodb_buffer_pool_dump_at_shutdown | COMPILED | | 0 | 0 | NULL | NULL | NULL | | innodb_buffer_pool_dump_now | COMPILED | | 0 | 0 | NULL | NULL | NULL | | innodb_buffer_pool_dump_pct | COMPILED | | 1 | 100 | NULL | NULL | NULL | | innodb_buffer_pool_filename | COMPILED | | 0 | 0 | NULL | NULL | NULL | | innodb_buffer_pool_in_core_file | COMPILED | | 0 | 0 | NULL | NULL | NULL | | innodb_buffer_pool_instances | COMPILED | | 0 | 64 | NULL | NULL | NULL | | innodb_buffer_pool_load_abort | COMPILED | | 0 | 0 | NULL | NULL | NULL | | innodb_buffer_pool_load_at_startup | COMPILED | | 0 | 0 | NULL | NULL | NULL | | innodb_buffer_pool_load_now | COMPILED | | 0 | 0 | NULL | NULL | NULL | | innodb_buffer_pool_size | GLOBAL | /etc/my.cnf | 5242880 | 9223372036854775807 | NULL | NULL | NULL | +-------------------------------------+-----------------+---------------+-----------+---------------------+----------+----------+----------+ 11 rows in set (0.00 sec) どこから設定を読み込んでいるか? 設定可能な最小値と最大値
  4. 4. PostgreSQL: pg_settingsとパラメータ概要 PostgreSQL postgres=# select name,setting,unit,context,category,short_desc from pg_settings where name like '%buffer%'; name | setting | unit | context | category | short_desc ----------------+---------+------+------------+-------------------------+----------------------------------------------------------------------------- shared_buffers | 16384 | 8kB | postmaster | 使用リソース/メモリ | サーバで使用される共有メモリのバッファ数を設定。 temp_buffers | 1024 | 8kB | user | 使用リソース/メモリ | 各セッションで使用される一時バッファの最大数を設定。 wal_buffers | 512 | 8kB | postmaster | 先行書き込みログ / 設定 | 共有メモリ内に割り当てられた、WALデータ用のディスクページバッファ数を設定。 (3 行) postgres=# 説明 contextでどのようなタイプの パラメータか確認する事が可能。
  5. 5. Global and Local Variables MySQL PostgreSQL root@localhost [sys]> set session sort_buffer_size = 262144 * 2; Query OK, 0 rows affected (0.00 sec) root@localhost [sys]> show global variables like 'sort_buffer%'; +------------------+--------+ | Variable_name | Value | +------------------+--------+ | sort_buffer_size | 262144 | +------------------+--------+ root@localhost [sys]> show session variables like 'sort_buffer%'; +------------------+--------+ | Variable_name | Value | +------------------+--------+ | sort_buffer_size | 524288 | +------------------+--------+ root@localhost [mysql]> select @@global.sort_buffer_size,@@session.sort_buffer_size; +---------------------------+----------------------------+ | @@global.sort_buffer_size | @@session.sort_buffer_size | +---------------------------+----------------------------+ | 262144 | 524288 | +---------------------------+----------------------------+ postgres=# select name,setting,unit,context,category from pg_settings where name like 'work_mem'; name | setting | unit | context | category ----------+---------+------+---------+--------------------- work_mem | 4096 | kB | user | 使用リソース/メモリ (1 行) postgres=# set work_mem = '16MB'; SET postgres=# select name,setting,unit,context,category from pg_settings where name like 'work_mem'; name | setting | unit | context | category ----------+---------+------+---------+--------------------- work_mem | 16384 | kB | user | 使用リソース/メモリ (1 行) postgres=# root@localhost [mysql]> select @@local.sort_buffer_size; root@localhost [mysql]> set @@session.sort_buffer_size = 262144 * 2; セッションシステム変数を変更すると、セッションが終了するまでその値は有効になります。 別のクライアントは影響を受けません。 グローバルシステム変数を変更すると、その値は全ての新しい接続に反映されます。 現在接続中のクライアントのセッション変数には影響を与えません SET GLOBAL ステートメントを発行するクライアントのセッション変数にも影響を与えません。 SETコマンド: SETコマンドで設定出来る値、UserまたはSuperuserパラメター他の セッションには影響しない。SET LOCALの場合、発行したトランザクション内に限定される。 contextがsuperuserの項目はSuperユーザーのみ変更する事が可能。 sighupはPostgreSQLプロセスがSIGHUPシグナルを受け取ったタイミングで、 設定リロードして反映させる事が可能。(pg_ctl reload, pg_reload_conf関数を利用) SELECT pg_reload_conf(); サーバー全体の値 特定セッションの値 特定セッションのみ影響
  6. 6. PostgreSQL:サーバー設定の反映 PostgreSQL -bash-4.2$ cat postgresql.conf | grep log_line log_line_prefix = '%m [%p] ' # special values: -bash-4.2$ psql -c "select name,setting,unit,context,category from pg_settings where name like 'log_line_prefix';" name | setting | unit | context | category -----------------+----------+------+---------+--------------------------------- log_line_prefix | %m [%p] | | sighup | レポートとログ出力 / ログの内容 (1 行) -bash-4.2$ vi postgresql.conf -bash-4.2$ cat postgresql.conf | grep log_line log_line_prefix = '[%t][%p][%c-%l][%x][%e]%q(%u, %d, %r, %a)' #log_line_prefix = '%m [%p] ' # special values: -bash-4.2$ psql -c "select pg_reload_conf();" pg_reload_conf ---------------- t (1 行) -bash-4.2$ psql -c "select name,setting,unit,context,category from pg_settings where name like 'log_line_prefix';" name | setting | unit | context | category -----------------+-------------------------------------------+------+---------+--------------------------------- log_line_prefix | [%t][%p][%c-%l][%x][%e]%q(%u, %d, %r, %a) | | sighup | レポートとログ出力 / ログの内容 (1 行) contextは、sighupで再起動不要な為、 pg_reload_conf()で設定を反映
  7. 7. 補足:パラメータの動的設定変更 MySQL PostgreSQL 参照: MySQL 8.0 Reference Manual -bash-4.2$ psql postgres -c "select context,count(*) from pg_settings group by context" context | count -------------------+------- postmaster | 55 superuser-backend | 4 user | 122 internal | 17 backend | 2 sighup | 79 superuser | 35 (7 行) -bash-4.2$ 多くのサーバーシステム変数は、動的にSET GLOBALまたは SET SESSIONを使用すると 実行時に設定可能です。 https://dev.mysql.com/doc/refman/8.0/en/dynamic-system-variables.html https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html sql> set session sort_buffer_size = 262144 * 2; sql> set global sort_buffer_size = 262144 * 2; [performance_schema]> select * from global_variables where VARIABLE_NAME like 'sort%'; +------------------+----------------+ | VARIABLE_NAME | VARIABLE_VALUE | +------------------+----------------+ | sort_buffer_size | 524288 | +------------------+----------------+ 1 row in set (0.01 sec) SETコマンド SETコマンドで設定出来る値、UserまたはSuperuserパラメター他の セッションには影響しない。SET LOCALの場合、発行したトランザクション内に限定される。 contextがsuperuserの項目はSuperユーザーのみ変更する事が可能。 sighupはPostgreSQLプロセスがSIGHUPシグナルを受け取ったタイミングで、 設定リロードして反映させる事が可能。(pg_ctl reload, pg_reload_conf関数を利用) SELECT pg_reload_conf(); postmaster: サーバ起動時にのみ適用可能、何かを変更するためにはサーバを再起動が必要。 sighup: サーバを再起動することなくpostgresql.conf内を変更することで行うことができます。 internal: これらの設定は直接変更できません。 これらは内部で決定された値を反映するものです。 backend: サーバを再起動することなくpostgresql.conf内を変更することで行うことができます。 superuser: セッションの中でSETコマンドを使用することで設定可能。(スーパーユーザのみ) user: postgresql.conf、または、セッションの中でSETコマンドを使用することで設定可能。 以下の値は再起動不要 sighup, backend superuser, user
  8. 8. パラメーターの永続化 MySQL (以下、 or の方法で永続化) PostgreSQL (以下、 or の方法で永続化) ❶ パラメータファイルを直接編集し永続化 (my.cnf) ❷ PERSISTオプションを付けて設定を反映 -bash-4.2$ mysql -u root -p -e "set persist max_connections=128;" Enter password: -bash-4.2$ cat mysqld-auto.cnf | jq <SNIP> "max_connections": { "Value": "128", "Metadata": { "Timestamp": 1579957659893227, "User": "root", "Host": "localhost" } [performance_schema]> select * from variables_info where VARIABLE_NAME like 'max_connections'G *************************** 1. row *************************** VARIABLE_NAME: max_connections VARIABLE_SOURCE: PERSISTED VARIABLE_PATH: /usr/local/mysql-8.0.18-linux-glibc2.12-x86_64/data/mysqld-auto.cnf MIN_VALUE: 1 MAX_VALUE: 100000 SET_TIME: 2020-01-25 13:07:39.893227 SET_USER: root SET_HOST: localhost ❶ パラメータファイルを直接編集し永続化 (postgresql.conf) ❷ ALTERコマンドでパラメータを永続化  -bash-4.2$ cat postgresql.conf | grep shared_buffers shared_buffers = 128MB # min 128kB -bash-4.2$ psql postgres -c "ALTER SYSTEM SET shared_buffers = '256MB'" ALTER SYSTEM -bash-4.2$ cat postgresql.conf | grep shared_buffers shared_buffers = 128MB # min 128kB -bash-4.2$ cat postgresql.auto.conf # Do not edit this file manually! # It will be overwritten by the ALTER SYSTEM command. shared_buffers = '256MB' -bash-4.2$ psql postgres -c "select name,setting,unit,context,category from pg_settings where name like 'shared_buffers';" name | setting | unit | context | category ----------------+---------+------+------------+--------------------- shared_buffers | 16384 | 8kB | postmaster | 使用リソース/メモリ -bash-4.2$ psql postgres -c "select name,setting,unit,context,category from pg_settings where name like 'shared_buffers';" name | setting | unit | context | category ----------------+---------+------+------------+--------------------- shared_buffers | 32768 | 8kB | postmaster | 使用リソース/メモリ グローバルシステム変数を永続的に設定するには、オプションファイルに設定する必要があります。 set persistコマンドでコマンドで実行した設定を"mysqld-auto.cnf”に書き込んで永続化します。 このファイルはオプションファイルの後に読み込まれるのでこちらの設定が反映されます。 https://dev.mysql.com/doc/refman/8.0/en/server-system-variables.html DynamicがYESになっているパッラメータが動的に設定変更可能なパラメター postgresql.auto.confはpostgresql.conf が読み込まれるときはいつでも自動的に読み込まれ、同 じように設定が反映されpostgresql.auto.confはpostgresql.confの設定を上書きします。 https://www.postgresql.jp/document/11/html/config-setting.html setコマンドにpersistオプションを付け ると、設定と同時にmysqld-auto.cnfに 追加され永続化される。 ALTER SYSTEMコマンドで設定す ると、postgresql.auto.conf に追加され永続化される
  9. 9. パラメータ変更の判断 MySQL PostgreSQL root@localhost [mysql]> show global status like 'sort%'; +-------------------+-------+ | Variable_name | Value | +-------------------+-------+ | Sort_merge_passes | 16 | | Sort_range | 0 | | Sort_rows | 11711 | | Sort_scan | 315 | +-------------------+-------+ root@localhost [mysql]> show session status like 'sort%'; +-------------------+-------+ | Variable_name | Value | +-------------------+-------+ | Sort_merge_passes | 0 | | Sort_range | 0 | | Sort_rows | 41 | | Sort_scan | 2 | +-------------------+-------+ root@localhost [sys]> select * from metrics where variable_name like 'sort%'; +-------------------+----------------+---------------+---------+ | Variable_name | Variable_value | Type | Enabled | +-------------------+----------------+---------------+---------+ | sort_merge_passes | 16 | Global Status | YES | | sort_range | 0 | Global Status | YES | | sort_rows | 13037 | Global Status | YES | | sort_scan | 320 | Global Status | YES | +-------------------+----------------+---------------+---------+ postgres=# select * from pg_stat_database limit 1; -[ RECORD 1 ]---------+------------------------------ datid | 0 datname | numbackends | 0 xact_commit | 0 xact_rollback | 0 blks_read | 38 blks_hit | 8130 tup_returned | 3835 tup_fetched | 1716 tup_inserted | 0 tup_updated | 0 tup_deleted | 0 conflicts | 0 temp_files | 0 temp_bytes | 0 deadlocks | 0 checksum_failures | checksum_last_failure | blk_read_time | 0 blk_write_time | 0 stats_reset | 2020-01-25 07:33:10.717705+09 postgres=# select datname,round(blks_hit*100/(blks_hit+blks_read), 2) AS cache_hit_ratio from pg_stat_database WHERE blks_read > 0; datname | cache_hit_ratio ----------+----------------- | 99.00 postgres | 98.00 SHOW STATUSやSYSスキーマをモニタリングして、MySQLの状態に応じて適宜パフォーマンスチューニ ングしていくと良いでしょう。また、パフォーマンス以外にも運用途中で設定変更しなければいけ 事も多いかと思いますので、サービスの再起動の有無等を適宜確認すると判断しやすいと思います。 pg_stat_* という名称のテーブル/ビューから、稼動統計情報を取得すれば、適宜パラメータを チューニングする事が可能。
  10. 10. 参考 MySQL https://dev.mysql.com/doc/refman/8.0/en/server-option-variable-reference.html https://dev.mysql.com/doc/refman/8.0/en/dynamic-system-variables.html https://gihyo.jp/dev/serial/01/mysql-road-construction-news/0094 https://www.slideshare.net/ShinyaSugiyama/mysql80-sys PostgreSQL https://www.postgresql.jp/document/11/html/view-pg-settings.html https://lets.postgresql.jp/documents/technical/statistics/2

×