update some content

This commit is contained in:
2021-10-28 14:13:06 +08:00
parent 97eb780aa4
commit 4f99455d8f
242 changed files with 1826 additions and 2616 deletions

View File

@@ -13,7 +13,7 @@
"articleSection" : "post",
"name" : "[筆記] 測試 postgresql 的pg_prewarm 對效能的影響 \/ test pg_prewarm in postgresql 11",
"headline" : "[筆記] 測試 postgresql 的pg_prewarm 對效能的影響 \/ test pg_prewarm in postgresql 11",
"description" : "\x3cp\x3e老闆提到想要把新系統的 postgresql 資料庫都撈到記憶體裡面\x3c\/p\x3e\n\x3cp\x3e但是否決了我提出的ramdisk 作法(因為當機的話,資料就沒了)\x3c\/p\x3e\n\x3cp\x3e在找資料的時候發現了這個postgresql 的 pg_prewarm extension\x3c\/p\x3e\n\x3cp\x3e好像有點意思就來測試看看吧\x3c\/p\x3e\n\x3cp\x3e只是目前還不知道該怎麼解讀測試的數據就是了\x26hellip;\x3c\/p\x3e\n\x3cp\x3e幹林北真的不是 DBA 啦 =.=\x3c\/p\x3e",
"description" : "\u003cp\u003e老闆提到想要把新系統的 postgresql 資料庫都撈到記憶體裡面\u003c\/p\u003e\n\u003cp\u003e但是否決了我提出的ramdisk 作法(因為當機的話,資料就沒了)\u003c\/p\u003e\n\u003cp\u003e在找資料的時候發現了這個postgresql 的 pg_prewarm extension\u003c\/p\u003e\n\u003cp\u003e好像有點意思就來測試看看吧\u003c\/p\u003e\n\u003cp\u003e只是目前還不知道該怎麼解讀測試的數據就是了\u0026hellip;\u003c\/p\u003e\n\u003cp\u003e幹林北真的不是 DBA 啦 =.=\u003c\/p\u003e",
"inLanguage" : "en",
"author" : "Eric Chang",
"creator" : "Eric Chang",
@@ -21,8 +21,8 @@
"accountablePerson" : "Eric Chang",
"copyrightHolder" : "Eric Chang",
"copyrightYear" : "2019",
"datePublished": "2019-12-20 14:31:42 \x2b0800 CST",
"dateModified" : "2019-12-20 14:31:42 \x2b0800 CST",
"datePublished": "2019-12-20 14:31:42 \u002b0800 CST",
"dateModified" : "2019-12-20 14:31:42 \u002b0800 CST",
"url" : "https:\/\/h.cowbay.org\/post\/test-pg_prewarm\/",
"wordCount" : "1314",
"image" : "https://h.cowbay.orghttps://h.cowbay.org/images/post-default-9.jpg"",
@@ -45,9 +45,9 @@
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.3.1/css/all.css" integrity="sha384-mzrmE5qonljUremFsqc01SB46JvROS7bZs3IO2EmfFsd15uHvIt+Y8vEf7N7fWAU" crossorigin="anonymous">
<link href="https://h.cowbay.org/css/style.css?v=1632901489" rel="stylesheet" id="theme-stylesheet" type='text/css' media='all'>
<link href="https://h.cowbay.org/css/style.css?v=1634607506" rel="stylesheet" id="theme-stylesheet" type='text/css' media='all'>
<link href="https://h.cowbay.org/css/custom.css?v=1632901489" rel="stylesheet" type='text/css' media='all'>
<link href="https://h.cowbay.org/css/custom.css?v=1634607506" rel="stylesheet" type='text/css' media='all'>
<link rel="shortcut icon" href="https://h.cowbay.org/img/favicon.ico" type="image/x-icon">
<link rel="icon" href="https://h.cowbay.org/img/favicon.ico" type="image/x-icon">
@@ -314,7 +314,7 @@ if (!doNotTrack) {
<h4 id="修改-postgresqlconf">修改 postgresql.conf</h4>
<p>編輯postgresql.conf開啟平行處理以及設定可用記憶體容量</p>
<p>這台測試機的環境是一台三代i7 , 24G RAM , 240G SSD安裝debian 10(buster)</p>
<pre><code># load libiriaes
<pre tabindex="0"><code># load libiriaes
# 其實這次不會用到pg_stat_statements ,不過出於習慣,還是加入開機自動載入吧
shared_preload_libraries = 'pg_stat_statements'
@@ -340,17 +340,17 @@ max_parallel_workers = 8
</code></pre><p>重新啟動postgresql ,準備開始測試囉!</p>
<p>轉換成 postgres 身份後,進入 psql</p>
<h4 id="建立測試資料庫">建立測試資料庫</h4>
<pre><code>postgres=# create database test;
<pre tabindex="0"><code>postgres=# create database test;
CREATE DATABASE
postgres=#
</code></pre><h4 id="連接測試資料庫建立pg_prewarm-extension">連接測試資料庫、建立pg_prewarm extension</h4>
<pre><code>postgres=# \c test ;
<pre tabindex="0"><code>postgres=# \c test ;
You are now connected to database &quot;test&quot; as user &quot;postgres&quot;.
test=# CREATE EXTENSION pg_prewarm;
CREATE EXTENSION
test=#
</code></pre><h4 id="建立測試資料表塞入500萬筆資料">建立測試資料表塞入500萬筆資料</h4>
<pre><code>test=# \timing
<pre tabindex="0"><code>test=# \timing
Timing is on.
test=# CREATE TABLE test_tbl AS
SELECT floor(random() * (9923123) + 1)::int FROM generate_series(1, 5000000) AS id;
@@ -359,10 +359,10 @@ Time: 2940.602 ms (00:02.941)
test=#
</code></pre><h4 id="檢查看看剛剛建立的table-用了多少空間">檢查看看剛剛建立的table 用了多少空間</h4>
<p>哎呀,看起來用得不多啊</p>
<pre><code>test=# SELECT pg_size_pretty(pg_relation_size('test_tbl'));
<pre tabindex="0"><code>test=# SELECT pg_size_pretty(pg_relation_size('test_tbl'));
173 MB
</code></pre><p><strong>玩大一點,塞個一億筆資料好了</strong></p>
<pre><code>test=# drop table test_tbl;
<pre tabindex="0"><code>test=# drop table test_tbl;
Time: 0.361 ms
test=# CREATE TABLE test_tbl AS
SELECT floor(random() * (99343) + 1)::int FROM generate_series(1, 100000000) AS id;
@@ -377,7 +377,7 @@ test=#
</code></pre><p>現在資料庫長到3457MB了</p>
<p>先來執行一些初步的取得基本數據</p>
<pre><code>test=# explain (analyze,buffers) select count(*) from test_tbl;
<pre tabindex="0"><code>test=# explain (analyze,buffers) select count(*) from test_tbl;
QUERY PLAN
------------------------------------------------------------------------------------------------------------------------
Finalize Aggregate (cost=755978.52..755978.53 rows=1 width=8) (actual time=3331.917..3331.918 rows=1 loops=1)
@@ -398,7 +398,7 @@ test=#
</code></pre><p>可以看到打中buffer 的部份其實很少,只有 160 大部分都是讀進去buffer (442318)</p>
<p>來看看 buffer 的使用狀況</p>
<pre><code>test=# CREATE EXTENSION pg_buffercache;
<pre tabindex="0"><code>test=# CREATE EXTENSION pg_buffercache;
CREATE EXTENSION
test=# select c.relname,pg_size_pretty(count(*) * 8192) as buffered,
test-# round(100.0 * count(*) / (
@@ -427,7 +427,7 @@ Time: 148.719 ms
test=#
</code></pre><p>可以看到這個 test_tbl 只有0.5% 被撈到shared_buffers 裡面</p>
<p>接下來就把這個table全部推到shared_buffers 裡面去</p>
<pre><code>test=# select pg_prewarm('test_tbl','buffer');
<pre tabindex="0"><code>test=# select pg_prewarm('test_tbl','buffer');
pg_prewarm
------------
442478
@@ -436,7 +436,7 @@ test=#
Time: 1938.043 ms (00:01.938)
test=#
</code></pre><p>然後再來看一次shared_buffers的使用狀況</p>
<pre><code>test=# select c.relname,pg_size_pretty(count(*) * 8192) as buffered,
<pre tabindex="0"><code>test=# select c.relname,pg_size_pretty(count(*) * 8192) as buffered,
round(100.0 * count(*) / (
select setting from pg_settings
where name='shared_buffers')::integer,1)
@@ -466,7 +466,7 @@ test=#
<p><strong>buffer_percent 表示這個表格佔用多少shared_buffers 的比例</strong></p>
<p><strong>percent_of_relation 表示這個表格有多少比例被載入 shared_buffers</strong></p>
<p>再來跑一次explain看看狀況</p>
<pre><code>test=# explain (analyze,buffers) select count(*) from test_tbl;
<pre tabindex="0"><code>test=# explain (analyze,buffers) select count(*) from test_tbl;
Time: 3551.785 ms (00:03.552)
Finalize Aggregate (cost=755978.52..755978.53 rows=1 width=8) (actual time=3427.286..3427.287 rows=1 loops=1)
Buffers: shared hit=442478
@@ -486,7 +486,7 @@ Time: 3551.785 ms (00:03.552)
<p>看樣子表格還是太小,所以沒有完全發揮?那再來把表格加大!</p>
<p>先重開一次 postgresql 清除buffer</p>
<p>然後重新建立表格</p>
<pre><code>test=# drop table test_tbl;
<pre tabindex="0"><code>test=# drop table test_tbl;
DROP TABLE
Time: 297.493 ms
test=# CREATE TABLE test_tbl AS
@@ -495,7 +495,7 @@ SELECT 300000000
Time: 290660.607 ms (04:50.661)
test=#
</code></pre><p>一樣,看看用了多少容量</p>
<pre><code>test=# SELECT pg_size_pretty(pg_relation_size('test_tbl'));
<pre tabindex="0"><code>test=# SELECT pg_size_pretty(pg_relation_size('test_tbl'));
pg_size_pretty
----------------
10 GB
@@ -505,7 +505,7 @@ Time: 0.474 ms
test=#
</code></pre><p>哇哈哈用了10G ,這次還不撐爆你!</p>
<p>跑explain 看看狀況</p>
<pre><code>test=# explain (analyze,buffers) select count(*) from test_tbl;
<pre tabindex="0"><code>test=# explain (analyze,buffers) select count(*) from test_tbl;
Time: 22909.065 ms (00:22.909)
QUERY PLAN
@@ -526,7 +526,7 @@ Time: 22909.065 ms (00:22.909)
</code></pre><p>看一下現在 shared_buffers 使用狀況</p>
<p>可以看到這個 test_tbl 幾乎沒被放入 shared_buffers 中</p>
<pre><code>test=# select c.relname,pg_size_pretty(count(*) * 8192) as buffered,
<pre tabindex="0"><code>test=# select c.relname,pg_size_pretty(count(*) * 8192) as buffered,
round(100.0 * count(*) / (
select setting from pg_settings
where name='shared_buffers')::integer,1)
@@ -552,7 +552,7 @@ group by c.oid,c.relname order by 3 desc limit 10;
Time: 163.936 ms
test=#
</code></pre><p>強制把test_tbl 全部塞進 shared_buffers</p>
<pre><code>test=# select pg_prewarm('test_tbl','buffer');
<pre tabindex="0"><code>test=# select pg_prewarm('test_tbl','buffer');
pg_prewarm
------------
1327434
@@ -561,7 +561,7 @@ test=#
Time: 7472.805 ms (00:07.473)
test=#
</code></pre><p>確認一下test_tbl 有沒有被整個塞進去</p>
<pre><code>test=# select c.relname,pg_size_pretty(count(*) * 8192) as buffered,
<pre tabindex="0"><code>test=# select c.relname,pg_size_pretty(count(*) * 8192) as buffered,
round(100.0 * count(*) / (
select setting from pg_settings
where name='shared_buffers')::integer,1)
@@ -587,7 +587,7 @@ group by c.oid,c.relname order by 3 desc limit 10;
Time: 4985.366 ms (00:04.985)
test=#
</code></pre><p>GOOD let&rsquo;s do explain again !</p>
<pre><code>test=# explain (analyze,buffers) select count(*) from test_tbl;
<pre tabindex="0"><code>test=# explain (analyze,buffers) select count(*) from test_tbl;
Time: 11451.188 ms (00:11.451)
QUERY PLAN
--------------------------------------------------------------------------------------------------------------------
@@ -896,7 +896,7 @@ title="pinterest icon"></i>
</div>
<script src="https://h.cowbay.org/js/jquery.min.js"></script>
<script src="https://h.cowbay.org/js/jquerymigrate.js"></script>
<script src="https://h.cowbay.org/js/production.min.js?v=1632901489"></script>
<script src="https://h.cowbay.org/js/production.min.js?v=1634607506"></script>
</body>
</html>