29回勉強会資料「PostgreSQLのリカバリ超入門」
See also http://www.interdb.jp/pgsql (Coming soon!)
初心者向け。PostgreSQLのWAL、CHECKPOINT、 オンラインバックアップの仕組み解説。
これを見たら、次は→ http://www.slideshare.net/satock/29shikumi-backup
2017/9/7 db tech showcase Tokyo 2017(JPOUG in 15 minutes)にて発表した内容です。
SQL大量発行に伴う処理遅延は、ミッションクリティカルシステムでありがちな性能問題のひとつです。
SQLをまとめて発行したり、処理の多重度を上げることができれば高速化可能です。ですが・・・
AP設計に起因する性能問題のため、開発工程の終盤においては対処が難しいことが多々あります。
そのような状況において、どのような改善手段があるのか、Oracleを例に解説します。
Oracle Databaseの既存バージョンの10gや11gOracle Zero Data Loss Recovery Applianceの登場で、ますます重要な機能となってきたOracle Recovery Managerについて、OTN人気連載シリーズ「しばちょう先生の試して納得!DBAへの道」の執筆者が語ります。RMANバックアップの運用例から、高速増分バックアップの内部動作とチューニング方法まで、出し惜しみなく解説します。
2017/9/7 db tech showcase Tokyo 2017(JPOUG in 15 minutes)にて発表した内容です。
SQL大量発行に伴う処理遅延は、ミッションクリティカルシステムでありがちな性能問題のひとつです。
SQLをまとめて発行したり、処理の多重度を上げることができれば高速化可能です。ですが・・・
AP設計に起因する性能問題のため、開発工程の終盤においては対処が難しいことが多々あります。
そのような状況において、どのような改善手段があるのか、Oracleを例に解説します。
Oracle Databaseの既存バージョンの10gや11gOracle Zero Data Loss Recovery Applianceの登場で、ますます重要な機能となってきたOracle Recovery Managerについて、OTN人気連載シリーズ「しばちょう先生の試して納得!DBAへの道」の執筆者が語ります。RMANバックアップの運用例から、高速増分バックアップの内部動作とチューニング方法まで、出し惜しみなく解説します。
This document summarizes new features and improvements in MySQL 8.0. Key highlights include utf8mb4 becoming the default character set to support Unicode 9.0, performance improvements for utf8mb4 of up to 1800%, continued enhancements to JSON support including new functions, expanded GIS functionality including spatial reference system support, and new functions for working with UUIDs and bitwise operations. It also provides a brief history of MySQL and outlines performance improvements seen in benchmarks between MySQL versions.
This document summarizes benchmarks of MySQL multi-thread slave performance using different numbers of slave_parallel_workers threads on Oracle Cloud MySQL instances. It finds that using 16 threads provides the best performance, processing queries faster and keeping the slave less behind the master compared to lower thread counts. CPU usage is also lower with 16 threads even though more threads are used.
6. Explain (SQL Execution Plan)
root@localhost [mysql]> explain select id from memo where id = 1;
+----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+-------------+
| 1 | SIMPLE | memo | NULL | const | PRIMARY | PRIMARY | 4 | const | 1 | 100.00 | Using index |
+----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+-------------+
1 row in set, 1 warning (0.00 sec)
postgresql=# explain select id from memo where id = 1;
QUERY PLAN
-----------------------------------------------------------------------------
Index Only Scan using idx_memo_id on memo (cost=0.28..8.29 rows=1 width=4)
Index Cond: (id = 1)
MySQL, PostgreSQLのANALYZEによる統計情報は厳密なものではなくランダムなサンプリングを行った結果であり、
実際の処理は実際のデータ分布、インデックス、プラットフォーム等、その他の要因にも依存します。
https://www.postgresql.jp/document/11/html/using-explain.html
https://dev.mysql.com/doc/refman/8.0/en/using-explain.html
7. Basic Expain
MySQL
root@localhost [confirm]> explain select id from memo where id = 1;
+----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+-------------+
| 1 | SIMPLE | memo | NULL | const | PRIMARY | PRIMARY | 4 | const | 1 | 100.00 | Using index |
+----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+-------------+
1 row in set, 1 warning (0.00 sec)
root@localhost [confirm]> show warnings;
+-------+------+--------------------------------------------------------------------+
| Level | Code | Message |
+-------+------+--------------------------------------------------------------------+
| Note | 1003 | /* select#1 */ select '1' AS `id` from `confirm`.`memo` where true |
+-------+------+--------------------------------------------------------------------+
1 row in set (0.00 sec)
root@localhost [confirm]> explain for connection 38017;
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
| 1 | SIMPLE | memo | NULL | ALL | NULL | NULL | NULL | NULL | 10 | 100.00 | NULL |
+----+-------------+-------+------------+------+---------------+------+---------+------+------+----------+-------+
1 row in set (0.00 sec)
EXPLAIN (DESC)は、SELECT、DELETE、INSERT、REPLACE、およびUPDATEステートメントで機能します。MySQL 8.0.19以降では、
TABLEステートメントでも機能します。explainで実行プランを確認したのちに、show warningsを実行する事で、実際にMySQLの
オプティマイザが書換え最適化したSQLを確認可能です。また、explain for connectionで特定の接続の実行プランを確認する事も可能です。
8. Basic Expain
PostgreSQL
app=# explain select id from memo where id = 1;
QUERY PLAN
-----------------------------------------------------------------------------
Index Only Scan using idx_memo_id on memo (cost=0.28..8.29 rows=1 width=4)
Index Cond: (id = 1)
(2 行)
app=# explain analyze select id from memo where id = 1;
QUERY PLAN
-----------------------------------------------------------------------------------------------------------------------
Index Only Scan using idx_memo_id on memo (cost=0.28..8.29 rows=1 width=4) (actual time=0.011..0.012 rows=1 loops=1)
Index Cond: (id = 1)
Heap Fetches: 1
Planning Time: 0.046 ms
Execution Time: 0.021 ms
(5 行)
EXPLAINはSELECT,INSERT、UPDATE、DELETE、CREATE TABLE AS、EXECUTで機能します。
(cost=0.28..8.29 rows=1 width=4)
- 初期処理の推定コスト。 出力段階が開始できるようになる前に消費される時間。
- 全体推定コスト。 これは計画ノードが実行完了である、つまりすべての利用可能な行を受け取ることを前提として示される。
- 計画ノードが出力する行の推定数。rowsの値は、計画ノードによって処理あるいはスキャンされた行数を表しておらず、ノードによって発行された行数を表す。
- この計画ノードが出力する行の(バイト単位での)推定平均幅。
9. Expain Options
MySQL
root@localhost [confirm]> explain analyze select * from memo where id = 1G
*************************** 1. row ***************************
EXPLAIN: -> Rows fetched before execution (actual time=0.000..0.000 rows=1 loops=1)
1 row in set (0.00 sec)
root@localhost [confirm]> explain analyze select * from memo where id >= 1 and id < 10 G
*************************** 1. row ***************************
EXPLAIN: -> Filter: ((memo.id >= 1) and (memo.id < 10)) (cost=2.06 rows=9) (actual time=0.016..0.027 rows=9 loops=1)
-> Index range scan on memo using PRIMARY (cost=2.06 rows=9) (actual time=0.014..0.023 rows=9 loops=1)
1 row in set (0.00 sec)
root@localhost [confirm]> explain analyze update memo set data = 'analyze テスト' where id = 1G
*************************** 1. row ***************************
EXPLAIN: <not executable by iterator executor>
1 row in set (0.00 sec)
参考: https://dev.mysql.com/doc/refman/8.0/en/explain.html
MySQL 8.0.18はEXPLAIN ANALYZEを導入しています。EXPLAIN ANALYZEは、SELECTステートメントだけでなく、
複数テーブルのUPDATEおよびDELETEステートメントでも使用できます。 MySQL 8.0.19からは、TABLEステートメントでも使用できます。
WL#4168: Implement EXPLAIN ANALYZE
10. Expain Options
PostgreSQL
app=# explain (analyze on, buffers on, verbose on) select id from memo where id = 1;
QUERY PLAN
------------------------------------------------------------------------------------------------------------------------------
Index Only Scan using idx_memo_id on public.memo (cost=0.28..8.29 rows=1 width=4) (actual time=0.027..0.029 rows=1 loops=1)
Output: id
Index Cond: (memo.id = 1)
Heap Fetches: 1
Buffers: shared hit=3
Planning Time: 0.084 ms
Execution Time: 0.051 ms
(7 行)
app=# explain verbose select * from memo where id = 1;
QUERY PLAN
--------------------------------------------------------------------------------
Index Scan using idx_memo_id on public.memo (cost=0.28..8.29 rows=1 width=25)
Output: id, data, data2
Index Cond: (memo.id = 1)
(3 行)
参考: https://www.postgresql.jp/document/11/html/sql-explain.html
解析でEXPLAINの出力をプログラムに渡すことを考えているのであれば、代わりに機械読み取りが容易な出力書式(XML、JSON、YAML)のいずれかを使用する事も可能です。
指定
取得 確認可能
11. Expain Output Format
MySQL
参考: https://dev.mysql.com/doc/refman/8.0/en/explain.html
[mysql]> explain format=TRADITIONAL select id from memo where id >= 1 and id < 10G
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: memo
partitions: NULL
type: range
possible_keys: PRIMARY
key: PRIMARY
key_len: 4
ref: NULL
rows: 9
filtered: 100.00
Extra: Using where; Using index
1 row in set, 1 warning (0.00 sec)
root@localhost [confirm]>
[mysql]> explain format=TREE select id from memo where id >= 1 and id < 10G
*************************** 1. row ***************************
EXPLAIN: -> Filter: ((memo.id >= 1) and (memo.id < 10)) (cost=2.06 rows=9)
-> Index range scan on memo using PRIMARY (cost=2.06 rows=9)
1 row in set (0.01 sec)
[mysql]> explain format=JSON select id from memo where id = 1G
*************************** 1. row ***************************
EXPLAIN: {
"query_block": {
"select_id": 1,
"cost_info": {
"query_cost": "1.00"
},
"table": {
"table_name": "memo",
<SNIP>
"using_index": true,
"cost_info": {
"read_cost": "0.00",
"eval_cost": "0.10",
"prefix_cost": "0.00",
"data_read_per_join": "16"
},
"used_columns": [
"id"
]
}
}
1 row in set, 1 warning (0.00 sec)
詳細
見積
12. Expain Output Format
PostgreSQL
参考: https://www.postgresql.jp/document/11/html/sql-explain.html
解析でEXPLAINの出力をプログラムに渡すことを考えているのであれば、代わりに機械読み取りが容易な出力書式(XML、JSON、YAML)のいずれかを使用する事も可能です。
app=# explain (FORMAT YAML) select id
app=# from memo where id = 1;
QUERY PLAN
----------------------------------
- Plan: +
Node Type: "Index Only Scan"+
Parallel Aware: false +
Scan Direction: "Forward" +
Index Name: "idx_memo_id" +
Relation Name: "memo" +
Alias: "memo" +
Startup Cost: 0.28 +
Total Cost: 8.29 +
Plan Rows: 1 +
Plan Width: 4 +
Index Cond: "(id = 1)"
(1 行)
app=# explain (FORMAT JSON) select id
app=# from memo where id = 1;
QUERY PLAN
---------------------------------------
[ +
{ +
"Plan": { +
"Node Type": "Index Only Scan",+
"Parallel Aware": false, +
"Scan Direction": "Forward", +
"Index Name": "idx_memo_id", +
"Relation Name": "memo", +
"Alias": "memo", +
"Startup Cost": 0.28, +
"Total Cost": 8.29, +
"Plan Rows": 1, +
"Plan Width": 4, +
"Index Cond": "(id = 1)" +
} +
} +
]
(1 行)
app=# explain (FORMAT XML) select id
app-# from memo where id = 1;
QUERY PLAN
----------------------------------------------------------
<explain xmlns="http://www.postgresql.org/2009/explain">+
<Query> +
<Plan> +
<Node-Type>Index Only Scan</Node-Type> +
<Parallel-Aware>false</Parallel-Aware> +
<Scan-Direction>Forward</Scan-Direction> +
<Index-Name>idx_memo_id</Index-Name> +
<Relation-Name>memo</Relation-Name> +
<Alias>memo</Alias> +
<Startup-Cost>0.28</Startup-Cost> +
<Total-Cost>8.29</Total-Cost> +
<Plan-Rows>1</Plan-Rows> +
<Plan-Width>4</Plan-Width> +
<Index-Cond>(id = 1)</Index-Cond> +
</Plan> +
</Query> +
</explain>
(1 行)
17. Need to Know
PostgreSQL (INSERT, UPDATE, DELETE) - 使用 場合 実際 実行
app=# explain analyze delete from memo where id = 1000;
QUERY PLAN
------------------------------------------------------------------------------------------------------------------------
Delete on memo (cost=0.28..8.62 rows=2 width=6) (actual time=0.080..0.080 rows=0 loops=1)
-> Index Scan using idx_memo_id on memo (cost=0.28..8.62 rows=2 width=6) (actual time=0.020..0.023 rows=2 loops=1)
Index Cond: (id = 1000)
Planning Time: 0.086 ms
Execution Time: 0.111 ms
(5 行)
app=# select * from memo where id = 1000;
id | data | data2
----+------+-------
(0 行)
app=# explain (analyze on, buffers on) delete from memo where id = 112;
QUERY PLAN
------------------------------------------------------------------------------------------------------------------------
Delete on memo (cost=0.28..8.29 rows=1 width=6) (actual time=0.027..0.027 rows=0 loops=1)
Buffers: shared hit=4 dirtied=1
-> Index Scan using idx_memo_id on memo (cost=0.28..8.29 rows=1 width=6) (actual time=0.008..0.008 rows=1 loops=1)
Index Cond: (id = 112)
Buffers: shared hit=3
Planning Time: 0.077 ms
Execution Time: 0.082 ms
(7 行)
app=# select id from memo where id = 112;
id
----
(0 行)
使用 場合 文 実際 実行 忘
返 出力 表示 文 伴 副作用 通常通 発生
文 対
影響 与 実行 場合 以下 方法 使用
参照
※ 実際 更新