{"id":285,"date":"2026-01-26T02:43:03","date_gmt":"2026-01-26T02:43:03","guid":{"rendered":"https:\/\/blog.gpst.net.cn:4008\/?p=285"},"modified":"2026-01-28T06:24:14","modified_gmt":"2026-01-28T06:24:14","slug":"clickhouse%e8%bf%90%e7%bb%b4-%e4%bc%98%e5%8c%96%e6%9f%a5%e8%af%a2%e6%80%a7%e8%83%bd","status":"publish","type":"post","link":"https:\/\/opshub.com.cn\/?p=285","title":{"rendered":"clickhouse\u8fd0\u7ef4\u2014\u2014\u4f18\u5316\u67e5\u8be2\u6027\u80fd"},"content":{"rendered":"\n<p>\u5728 ClickHouse \u4e2d\uff0c\u4f18\u5316\u67e5\u8be2\u6027\u80fd\u7684\u914d\u7f6e\u8bbe\u7f6e\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u7cfb\u7edf\u7684\u54cd\u5e94\u901f\u5ea6\u548c\u8d44\u6e90\u5229\u7528\u7387\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u914d\u7f6e\u4f18\u5316\u9009\u9879\uff1a<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u67e5\u8be2\u7ea7\u522b\u8bbe\u7f6e<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>max_threads<\/strong>\uff1a\u63a7\u5236\u67e5\u8be2\u7684\u6700\u5927\u5e76\u53d1\u7ebf\u7a0b\u6570\u3002\u9ed8\u8ba4\u901a\u5e38\u4e3a\u670d\u52a1\u5668 CPU \u6838\u5fc3\u6570\u3002\u8bbe\u7f6e\u66f4\u9ad8\u7684\u5e76\u53d1\u6570\u53ef\u80fd\u4f1a\u589e\u52a0\u67e5\u8be2\u901f\u5ea6\uff0c\u4f46\u9700\u8981\u6839\u636e\u786c\u4ef6\u8d44\u6e90\u8fdb\u884c\u5408\u7406\u8c03\u6574\u3002sqlSET max_threads = 8;<\/li>\n\n\n\n<li><strong>max_block_size<\/strong>\uff1a\u8bbe\u7f6e\u67e5\u8be2\u8fd4\u56de\u7684\u6570\u636e\u5757\u5927\u5c0f\u3002\u8f83\u5927\u7684\u5757\u53ef\u4ee5\u51cf\u5c11\u7f51\u7edc\u4f20\u8f93\u7684\u5f00\u9500\uff0c\u4f46\u4f1a\u589e\u52a0\u5185\u5b58\u5360\u7528\u3002sqlSET max_block_size = 65536;<\/li>\n\n\n\n<li><strong>max_memory_usage<\/strong>\uff1a\u9650\u5236\u5355\u4e2a\u67e5\u8be2\u7684\u6700\u5927\u5185\u5b58\u4f7f\u7528\u91cf\u3002\u9002\u5f53\u7684\u9650\u5236\u53ef\u4ee5\u9632\u6b62\u67e5\u8be2\u5185\u5b58\u6ea2\u51fa\u5bfc\u81f4\u670d\u52a1\u5d29\u6e83\u3002sqlSET max_memory_usage = &#8217;10G&#8217;;<\/li>\n\n\n\n<li><strong>max_bytes_before_external_group_by<\/strong>\u548c<strong>max_bytes_before_external_sort<\/strong>\uff1a\u5728 GROUP BY \u6216 ORDER BY \u64cd\u4f5c\u4e2d\uff0c\u5f53\u6570\u636e\u91cf\u8fbe\u5230\u6b64\u9650\u5236\u65f6\uff0cClickHouse \u4f1a\u5c06\u90e8\u5206\u6570\u636e\u5b58\u50a8\u5728\u78c1\u76d8\u4e0a\uff0c\u4ece\u800c\u8282\u7701\u5185\u5b58\u3002sqlSET max_bytes_before_external_group_by = &#8216;1G&#8217;;<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u5206\u533a\u548c\u6392\u5e8f\u914d\u7f6e<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>index_granularity<\/strong>\uff1a\u8bbe\u7f6e\u7d22\u5f15\u7c92\u5ea6\uff0c\u8868\u793a\u6bcf\u9694\u591a\u5c11\u884c\u8fdb\u884c\u4e00\u6b21\u7d22\u5f15\u3002\u8f83\u5c0f\u7684\u7c92\u5ea6\u53ef\u4ee5\u52a0\u5feb\u67e5\u8be2\u901f\u5ea6\uff0c\u4f46\u4f1a\u589e\u52a0\u5b58\u50a8\u7a7a\u95f4\u3002sqlCREATE TABLE example_table ( id UInt32, date Date ) ENGINE = MergeTree() PARTITION BY toYYYYMM(date) ORDER BY (date, id) SETTINGS index_granularity = 8192;<\/li>\n\n\n\n<li><strong>merge_tree_max_rows_to_use_cache<\/strong>\uff1a\u8bbe\u7f6e\u67e5\u8be2\u65f6\u80fd\u7f13\u5b58\u7684\u6700\u5927\u884c\u6570\u3002\u589e\u5927\u6b64\u503c\u53ef\u4ee5\u63d0\u5347\u67e5\u8be2\u901f\u5ea6\uff0c\u4f46\u4f1a\u589e\u52a0\u5185\u5b58\u5360\u7528\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">3. \u538b\u7f29\u8bbe\u7f6e<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>min_compress_block_size<\/strong>\u548c<strong>max_compress_block_size<\/strong>\uff1a\u63a7\u5236\u6570\u636e\u5757\u7684\u538b\u7f29\u5927\u5c0f\u3002\u5927\u7684\u6570\u636e\u5757\u53ef\u4ee5\u63d0\u5347\u538b\u7f29\u7387\uff0c\u51cf\u5c11 I\/O \u6d88\u8017\u3002xml&lt;min_compress_block_size&gt;1024&lt;\/min_compress_block_size&gt; &lt;max_compress_block_size&gt;8192&lt;\/max_compress_block_size&gt;<\/li>\n\n\n\n<li><strong>enable_mixed_granularity_parts<\/strong>\uff1a\u542f\u7528\u6df7\u5408\u7c92\u5ea6\uff0c\u53ef\u4ee5\u51cf\u5c11\u9ad8\u57fa\u6570\u6570\u636e\u7684\u7d22\u5f15\u5927\u5c0f\uff0c\u8282\u7701\u5b58\u50a8\u7a7a\u95f4\u3002xml&lt;enable_mixed_granularity_parts&gt;true&lt;\/enable_mixed_granularity_parts&gt;<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">4. \u7f13\u5b58\u548c\u5185\u5b58\u7ba1\u7406<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>mark_cache_size<\/strong>\uff1a\u6807\u8bb0\u7f13\u5b58\u7684\u5927\u5c0f\uff0c\u7528\u4e8e\u7f13\u5b58\u6570\u636e\u5206\u7247\u7684\u6807\u8bb0\u3002\u8f83\u5927\u7684\u7f13\u5b58\u53ef\u4ee5\u51cf\u5c11 I\/O\uff0c\u63d0\u5347\u67e5\u8be2\u6027\u80fd\u3002xml&lt;mark_cache_size&gt;5368709120&lt;\/mark_cache_size&gt; &lt;!&#8211; 5GB &#8211;&gt;<\/li>\n\n\n\n<li><strong>max_open_files<\/strong>\uff1a\u8bbe\u7f6e\u5355\u4e2a\u67e5\u8be2\u5141\u8bb8\u6253\u5f00\u7684\u6700\u5927\u6587\u4ef6\u6570\u91cf\u3002\u589e\u5927\u6b64\u8bbe\u7f6e\u5728\u591a\u5206\u533a\u67e5\u8be2\u65f6\u6548\u679c\u660e\u663e\uff0c\u4f46\u5e94\u786e\u4fdd\u64cd\u4f5c\u7cfb\u7edf\u7684\u6587\u4ef6\u63cf\u8ff0\u7b26\u8bbe\u7f6e\u8db3\u591f\u9ad8\u3002xml&lt;max_open_files&gt;500&lt;\/max_open_files&gt;<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">5. \u5408\u5e76\u4e0e TTL \u8bbe\u7f6e<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>merge_with_ttl_timeout<\/strong>\uff1a\u5408\u5e76\u8fc7\u671f\u6570\u636e\u7684\u8d85\u65f6\u65f6\u95f4\u3002\u5b9a\u671f\u5408\u5e76\u6570\u636e\u51cf\u5c11\u78c1\u76d8\u788e\u7247\uff0c\u6709\u52a9\u4e8e\u4f18\u5316\u67e5\u8be2\u6027\u80fd\u3002xml&lt;merge_with_ttl_timeout&gt;3600&lt;\/merge_with_ttl_timeout&gt; &lt;!&#8211; \u6bcf\u5c0f\u65f6 &#8211;&gt;<\/li>\n\n\n\n<li><strong>background_pool_size<\/strong>\uff1a\u8bbe\u7f6e\u540e\u53f0\u4efb\u52a1\u7ebf\u7a0b\u6c60\u5927\u5c0f\uff0c\u9002\u7528\u4e8eMergeTree\u8868\u7684\u5206\u6bb5\u5408\u5e76\u7b49\u540e\u53f0\u4efb\u52a1\u3002xml&lt;background_pool_size&gt;16&lt;\/background_pool_size&gt;<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">6. \u7f51\u7edc\u8bbe\u7f6e<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>max_concurrent_queries<\/strong>\uff1a\u63a7\u5236\u7cfb\u7edf\u6700\u5927\u5e76\u53d1\u67e5\u8be2\u6570\uff0c\u907f\u514d\u8fc7\u591a\u5e76\u53d1\u5bfc\u81f4\u8d44\u6e90\u8017\u5c3d\u3002xml&lt;max_concurrent_queries&gt;100&lt;\/max_concurrent_queries&gt;<\/li>\n\n\n\n<li><strong>tcp_keep_alive_timeout<\/strong>\uff1a\u8bbe\u7f6e TCP \u957f\u8fde\u63a5\u7684\u8d85\u65f6\u65f6\u95f4\u3002\u53ef\u4ee5\u6839\u636e\u7f51\u7edc\u60c5\u51b5\u8c03\u6574\uff0c\u51cf\u5c11\u4e0d\u5fc5\u8981\u7684\u8fde\u63a5\u6d88\u8017\u3002xml&lt;tcp_keep_alive_timeout&gt;60&lt;\/tcp_keep_alive_timeout&gt;<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">7. EXPLAIN \u548c\u8c03\u8bd5\u5de5\u5177<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u4f7f\u7528EXPLAIN\u67e5\u770b\u67e5\u8be2\u8ba1\u5212\uff0c\u5224\u65ad\u7d22\u5f15\u548c\u6392\u5e8f\u952e\u7684\u4f7f\u7528\u60c5\u51b5\uff0c\u53d1\u73b0\u67e5\u8be2\u4e2d\u7684\u6f5c\u5728\u6027\u80fd\u74f6\u9888\u3002sqlEXPLAIN SELECT * FROM example_table WHERE date = &#8216;2023-01-01&#8217;;<\/li>\n<\/ul>\n\n\n\n<p>\u914d\u7f6e\u5b8c\u6210\u540e\uff0c\u5efa\u8bae\u6839\u636e\u4e1a\u52a1\u9700\u6c42\u5b9a\u671f\u76d1\u63a7\u5e76\u8c03\u4f18\u67e5\u8be2\uff0c\u4ee5\u786e\u4fdd ClickHouse \u7cfb\u7edf\u7a33\u5b9a\u8fd0\u884c\u5e76\u7ef4\u6301\u826f\u597d\u7684\u67e5\u8be2\u6027\u80fd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 ClickHouse \u4e2d\uff0c\u4f18\u5316\u67e5\u8be2\u6027\u80fd\u7684\u914d\u7f6e\u8bbe\u7f6e\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u7cfb\u7edf\u7684\u54cd\u5e94\u901f\u5ea6\u548c\u8d44\u6e90\u5229\u7528\u7387\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u914d\u7f6e\u4f18\u5316\u9009\u9879\uff1a 1. \u67e5\u8be2\u7ea7\u522b\u8bbe\u7f6e 2. \u5206\u533a\u548c\u6392\u5e8f\u914d\u7f6e 3. \u538b\u7f29\u8bbe\u7f6e 4. \u7f13\u5b58\u548c\u5185\u5b58\u7ba1\u7406 5. \u5408\u5e76\u4e0e TTL \u8bbe\u7f6e 6. \u7f51\u7edc\u8bbe\u7f6e 7. EXPLAIN \u548c\u8c03\u8bd5\u5de5\u5177 \u914d\u7f6e\u5b8c\u6210\u540e\uff0c\u5efa\u8bae\u6839\u636e\u4e1a\u52a1\u9700\u6c42\u5b9a\u671f\u76d1\u63a7\u5e76\u8c03\u4f18\u67e5\u8be2\uff0c\u4ee5\u786e\u4fdd ClickHouse \u7cfb\u7edf\u7a33\u5b9a\u8fd0\u884c\u5e76\u7ef4\u6301\u826f\u597d\u7684\u67e5\u8be2\u6027\u80fd\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-285","post","type-post","status-publish","format-standard","hentry","category-7"],"_links":{"self":[{"href":"https:\/\/opshub.com.cn\/index.php?rest_route=\/wp\/v2\/posts\/285","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/opshub.com.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/opshub.com.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/opshub.com.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/opshub.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=285"}],"version-history":[{"count":2,"href":"https:\/\/opshub.com.cn\/index.php?rest_route=\/wp\/v2\/posts\/285\/revisions"}],"predecessor-version":[{"id":632,"href":"https:\/\/opshub.com.cn\/index.php?rest_route=\/wp\/v2\/posts\/285\/revisions\/632"}],"wp:attachment":[{"href":"https:\/\/opshub.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=285"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/opshub.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=285"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/opshub.com.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=285"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}