{"id":96410,"date":"2024-10-20T09:11:17","date_gmt":"2024-10-20T01:11:17","guid":{"rendered":"https:\/\/server.hk\/cnblog\/96410\/"},"modified":"2024-10-20T09:11:17","modified_gmt":"2024-10-20T01:11:17","slug":"pg%e6%95%b8%e6%93%9a%e5%ba%ab%e5%85%a7%e5%ad%98%e5%91%8a%e8%ad%a6%e4%ba%86%e6%80%8e%e9%ba%bc%e5%88%86%e6%9e%90","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/96410\/","title":{"rendered":"PG\u6578\u64da\u5eab\u5167\u5b58\u544a\u8b66\u4e86\u600e\u9ebc\u5206\u6790"},"content":{"rendered":"<h1 id=\"pg%e6%95%b8%e6%93%9a%e5%ba%ab%e5%85%a7%e5%ad%98%e5%91%8a%e8%ad%a6%e4%ba%86%e6%80%8e%e9%ba%bc%e5%88%86%e6%9e%90-rusEkBZmAq\">PG\u6578\u64da\u5eab\u5167\u5b58\u544a\u8b66\u4e86\u600e\u9ebc\u5206\u6790<\/h1>\n<p>\u5728\u4f7f\u7528PostgreSQL\uff08PG\uff09\u6578\u64da\u5eab\u7684\u904e\u7a0b\u4e2d\uff0c\u5167\u5b58\u544a\u8b66\u662f\u4e00\u500b\u5e38\u898b\u7684\u554f\u984c\u3002\u7576\u6578\u64da\u5eab\u7684\u5167\u5b58\u4f7f\u7528\u91cf\u8d85\u904e\u9810\u8a2d\u7684\u95be\u503c\u6642\uff0c\u7cfb\u7d71\u6703\u767c\u51fa\u544a\u8b66\uff0c\u9019\u53ef\u80fd\u6703\u5f71\u97ff\u6578\u64da\u5eab\u7684\u6027\u80fd\u548c\u7a69\u5b9a\u6027\u3002\u56e0\u6b64\uff0c\u4e86\u89e3\u5982\u4f55\u5206\u6790\u548c\u89e3\u6c7a\u9019\u4e9b\u544a\u8b66\u662f\u975e\u5e38\u91cd\u8981\u7684\u3002<\/p>\n<h2 id=\"%e5%85%a7%e5%ad%98%e5%91%8a%e8%ad%a6%e7%9a%84%e5%8e%9f%e5%9b%a0-rusEkBZmAq\">\u5167\u5b58\u544a\u8b66\u7684\u539f\u56e0<\/h2>\n<p>\u5167\u5b58\u544a\u8b66\u901a\u5e38\u7531\u4ee5\u4e0b\u5e7e\u500b\u539f\u56e0\u5f15\u8d77\uff1a<\/p>\n<ul>\n<li><strong>\u67e5\u8a62\u6027\u80fd\u4e0d\u4f73\uff1a<\/strong>\u67d0\u4e9b\u67e5\u8a62\u53ef\u80fd\u6703\u6d88\u8017\u904e\u591a\u7684\u5167\u5b58\uff0c\u7279\u5225\u662f\u7576\u5b83\u5011\u6d89\u53ca\u5927\u91cf\u6578\u64da\u6642\u3002<\/li>\n<li><strong>\u914d\u7f6e\u4e0d\u7576\uff1a<\/strong>PostgreSQL\u7684\u5167\u5b58\u914d\u7f6e\u53c3\u6578\uff08\u5982work_mem\u3001shared_buffers\u7b49\uff09\u5982\u679c\u8a2d\u7f6e\u4e0d\u7576\uff0c\u53ef\u80fd\u6703\u5c0e\u81f4\u5167\u5b58\u4f7f\u7528\u904e\u9ad8\u3002<\/li>\n<li><strong>\u4e26\u767c\u9023\u63a5\u904e\u591a\uff1a<\/strong>\u7576\u540c\u6642\u9023\u63a5\u7684\u7528\u6236\u6578\u91cf\u904e\u591a\u6642\uff0c\u5167\u5b58\u9700\u6c42\u6703\u6025\u5287\u589e\u52a0\u3002<\/li>\n<li><strong>\u5167\u5b58\u6cc4\u6f0f\uff1a<\/strong>\u67d0\u4e9b\u61c9\u7528\u7a0b\u5e8f\u6216\u64f4\u5c55\u53ef\u80fd\u5b58\u5728\u5167\u5b58\u6cc4\u6f0f\u7684\u554f\u984c\uff0c\u5c0e\u81f4\u5167\u5b58\u4f7f\u7528\u4e0d\u65b7\u589e\u52a0\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%a6%82%e4%bd%95%e5%88%86%e6%9e%90%e5%85%a7%e5%ad%98%e5%91%8a%e8%ad%a6-rusEkBZmAq\">\u5982\u4f55\u5206\u6790\u5167\u5b58\u544a\u8b66<\/h2>\n<p>\u7576\u6536\u5230\u5167\u5b58\u544a\u8b66\u6642\uff0c\u53ef\u4ee5\u6309\u7167\u4ee5\u4e0b\u6b65\u9a5f\u9032\u884c\u5206\u6790\uff1a<\/p>\n<h3 id=\"1-%e6%aa%a2%e6%9f%a5%e5%85%a7%e5%ad%98%e4%bd%bf%e7%94%a8%e6%83%85%e6%b3%81-rusEkBZmAq\">1. \u6aa2\u67e5\u5167\u5b58\u4f7f\u7528\u60c5\u6cc1<\/h3>\n<p>\u4f7f\u7528\u4ee5\u4e0bSQL\u67e5\u8a62\u4f86\u6aa2\u67e5\u7576\u524d\u7684\u5167\u5b58\u4f7f\u7528\u60c5\u6cc1\uff1a<\/p>\n<pre><code>SELECT * FROM pg_stat_activity;<\/code><\/pre>\n<p>\u9019\u500b\u67e5\u8a62\u5c07\u986f\u793a\u7576\u524d\u6240\u6709\u6d3b\u52d5\u7684\u9023\u63a5\u53ca\u5176\u72c0\u614b\uff0c\u5e6b\u52a9\u4f60\u8b58\u5225\u54ea\u4e9b\u67e5\u8a62\u53ef\u80fd\u6b63\u5728\u6d88\u8017\u904e\u591a\u7684\u5167\u5b58\u3002<\/p>\n<h3 id=\"2-%e5%88%86%e6%9e%90%e6%9f%a5%e8%a9%a2%e6%80%a7%e8%83%bd-rusEkBZmAq\">2. \u5206\u6790\u67e5\u8a62\u6027\u80fd<\/h3>\n<p>\u4f7f\u7528EXPLAIN\u547d\u4ee4\u4f86\u5206\u6790\u6027\u80fd\u8f03\u5dee\u7684\u67e5\u8a62\uff1a<\/p>\n<pre><code>EXPLAIN ANALYZE SELECT * FROM your_table WHERE condition;<\/code><\/pre>\n<p>\u9019\u5c07\u986f\u793a\u67e5\u8a62\u7684\u57f7\u884c\u8a08\u5283\uff0c\u5e6b\u52a9\u4f60\u627e\u51fa\u6027\u80fd\u74f6\u9838\u3002<\/p>\n<h3 id=\"3-%e6%aa%a2%e6%9f%a5%e9%85%8d%e7%bd%ae%e5%8f%83%e6%95%b8-rusEkBZmAq\">3. \u6aa2\u67e5\u914d\u7f6e\u53c3\u6578<\/h3>\n<p>\u6aa2\u67e5PostgreSQL\u7684\u5167\u5b58\u914d\u7f6e\u53c3\u6578\uff0c\u7279\u5225\u662f\u4ee5\u4e0b\u5e7e\u500b\uff1a<\/p>\n<ul>\n<li><strong>shared_buffers\uff1a<\/strong>\u9019\u662fPostgreSQL\u7528\u65bc\u7de9\u5b58\u6578\u64da\u7684\u5167\u5b58\u91cf\uff0c\u901a\u5e38\u5efa\u8b70\u8a2d\u7f6e\u70ba\u7cfb\u7d71\u5167\u5b58\u768425%\u3002<\/li>\n<li><strong>work_mem\uff1a<\/strong>\u9019\u662f\u6bcf\u500b\u67e5\u8a62\u4f7f\u7528\u7684\u5167\u5b58\u91cf\uff0c\u6839\u64da\u67e5\u8a62\u7684\u8907\u96dc\u6027\u9032\u884c\u8abf\u6574\u3002<\/li>\n<li><strong>maintenance_work_mem\uff1a<\/strong>\u9019\u662f\u7528\u65bc\u7dad\u8b77\u64cd\u4f5c\uff08\u5982VACUUM\uff09\u7684\u5167\u5b58\u91cf\u3002<\/li>\n<\/ul>\n<p>\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u67e5\u8a62\u4f86\u6aa2\u67e5\u7576\u524d\u7684\u914d\u7f6e\u53c3\u6578\uff1a<\/p>\n<pre><code>SHOW shared_buffers; \nSHOW work_mem; \nSHOW maintenance_work_mem;<\/code><\/pre>\n<h3 id=\"4-%e7%9b%a3%e6%8e%a7%e4%b8%a6%e7%99%bc%e9%80%a3%e6%8e%a5-rusEkBZmAq\">4. \u76e3\u63a7\u4e26\u767c\u9023\u63a5<\/h3>\n<p>\u4f7f\u7528\u4ee5\u4e0b\u67e5\u8a62\u4f86\u6aa2\u67e5\u7576\u524d\u7684\u4e26\u767c\u9023\u63a5\u6578\u91cf\uff1a<\/p>\n<pre><code>SELECT count(*) FROM pg_stat_activity;<\/code><\/pre>\n<p>\u5982\u679c\u4e26\u767c\u9023\u63a5\u6578\u91cf\u904e\u9ad8\uff0c\u8003\u616e\u589e\u52a0\u9023\u63a5\u6c60\u7684\u5927\u5c0f\u6216\u512a\u5316\u61c9\u7528\u7a0b\u5e8f\u7684\u9023\u63a5\u7ba1\u7406\u3002<\/p>\n<h3 id=\"5-%e6%aa%a2%e6%9f%a5%e5%85%a7%e5%ad%98%e6%b3%84%e6%bc%8f-rusEkBZmAq\">5. \u6aa2\u67e5\u5167\u5b58\u6cc4\u6f0f<\/h3>\n<p>\u5982\u679c\u61f7\u7591\u5b58\u5728\u5167\u5b58\u6cc4\u6f0f\uff0c\u53ef\u4ee5\u4f7f\u7528\u5de5\u5177\u5982pgBadger\u4f86\u5206\u6790PostgreSQL\u7684\u65e5\u8a8c\uff0c\u627e\u51fa\u53ef\u80fd\u7684\u554f\u984c\u3002<\/p>\n<h2 id=\"%e8%a7%a3%e6%b1%ba%e5%85%a7%e5%ad%98%e5%91%8a%e8%ad%a6%e7%9a%84%e6%96%b9%e6%b3%95-rusEkBZmAq\">\u89e3\u6c7a\u5167\u5b58\u544a\u8b66\u7684\u65b9\u6cd5<\/h2>\n<p>\u6839\u64da\u5206\u6790\u7d50\u679c\uff0c\u53ef\u4ee5\u63a1\u53d6\u4ee5\u4e0b\u63aa\u65bd\u4f86\u89e3\u6c7a\u5167\u5b58\u544a\u8b66\uff1a<\/p>\n<ul>\n<li>\u512a\u5316\u6027\u80fd\u8f03\u5dee\u7684\u67e5\u8a62\uff0c\u6e1b\u5c11\u5167\u5b58\u6d88\u8017\u3002<\/li>\n<li>\u8abf\u6574PostgreSQL\u7684\u5167\u5b58\u914d\u7f6e\u53c3\u6578\uff0c\u4ee5\u9069\u61c9\u5be6\u969b\u9700\u6c42\u3002<\/li>\n<li>\u9650\u5236\u4e26\u767c\u9023\u63a5\u6578\u91cf\uff0c\u6216\u4f7f\u7528\u9023\u63a5\u6c60\u6280\u8853\u4f86\u7ba1\u7406\u9023\u63a5\u3002<\/li>\n<li>\u5b9a\u671f\u6aa2\u67e5\u548c\u7dad\u8b77\u6578\u64da\u5eab\uff0c\u9632\u6b62\u5167\u5b58\u6cc4\u6f0f\u554f\u984c\u7684\u767c\u751f\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-rusEkBZmAq\">\u7e3d\u7d50<\/h2>\n<p>\u5167\u5b58\u544a\u8b66\u662fPostgreSQL\u6578\u64da\u5eab\u7ba1\u7406\u4e2d\u4e00\u500b\u91cd\u8981\u7684\u554f\u984c\uff0c\u901a\u904e\u5408\u7406\u7684\u5206\u6790\u548c\u8abf\u6574\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u89e3\u6c7a\u9019\u4e9b\u554f\u984c\uff0c\u78ba\u4fdd\u6578\u64da\u5eab\u7684\u7a69\u5b9a\u6027\u548c\u6027\u80fd\u3002\u5982\u679c\u60a8\u9700\u8981\u9032\u4e00\u6b65\u7684\u652f\u6301\u6216\u89e3\u6c7a\u65b9\u6848\uff0c\u8003\u616e\u4f7f\u7528<a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a>\u6216<a href=\"https:\/\/server.hk\">\u9999\u6e2f\u4f3a\u670d\u5668<\/a>\u4f86\u63d0\u5347\u60a8\u7684\u6578\u64da\u5eab\u6027\u80fd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e86\u89e3PG\u6578\u64da\u5eab\u5167\u5b58\u544a\u8b66\u7684\u5206\u6790\u65b9\u6cd5\uff0c\u638c\u63e1\u6027\u80fd\u512a\u5316\u6280\u5de7\uff0c\u78ba\u4fdd\u7cfb\u7d71\u7a69\u5b9a\u904b\u884c\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-96410","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96410","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=96410"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96410\/revisions"}],"predecessor-version":[{"id":96411,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/96410\/revisions\/96411"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=96410"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=96410"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=96410"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}