{"id":19043,"date":"2026-07-09T18:07:06","date_gmt":"2026-07-09T10:07:06","guid":{"rendered":"https:\/\/kbwp.transwarp.cn\/?p=19043"},"modified":"2026-07-09T18:11:42","modified_gmt":"2026-07-09T10:11:42","slug":"%e5%ad%97%e5%85%b8%e8%a1%a8partitions_v%e6%9f%a5%e8%af%a2%e6%80%a7%e8%83%bd%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"https:\/\/kbwp.transwarp.cn\/?p=19043","title":{"rendered":"\u5b57\u5178\u8868partitions_v\u67e5\u8be2\u6027\u80fd\u95ee\u9898"},"content":{"rendered":"<h3>\u95ee\u9898\u80cc\u666f<\/h3>\n<hr \/>\n<p>\u5ba2\u6237\u9700\u8981\u6839\u636e\u6570\u636e\u5e93\u540d\u3001\u8868\u540d\u67e5\u8be2\u76ee\u6807\u8868\u6240\u6709\u5206\u533a\u540d\u79f0\u4e0e\u5206\u533a\u8303\u56f4\uff0c\u539f\u59cb SQL \u4f7f\u7528\u7cfb\u7edf\u5185\u7f6e\u89c6\u56fe<code>partitions_v<\/code>\u3001<code>range_partitions_v<\/code>\u5b9e\u73b0\u903b\u8f91\uff0c\u5b8c\u6574\u8bed\u53e5\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-sql\">SELECT\npartition_name AS name,\nNULL AS createTimeSeconds,\npartition_range AS expression\nFROM (\n    SELECT\n    partition_name,\n    NULL AS partition_range\n    FROM metastore_inceptor1.partitions_v\n    WHERE database_name = LOWER('mdwuser')\n    AND table_name = LOWER('t_dws_s04_cust_atl_trd_aggr')\n    UNION ALL\n    SELECT\n    partition_name,\n    partition_range\n    FROM metastore_inceptor1.range_partitions_v\n    WHERE database_name = LOWER('mdwuser')\n    AND table_name = LOWER('t_dws_s04_cust_atl_trd_aggr')\n) t\nORDER BY partition_name ;<\/code><\/pre>\n<h3>\u73b0\u8c61\u63cf\u8ff0<\/h3>\n<hr \/>\n<p>\u6267\u884c\u8be5 SQL \u5355\u6b21\u8017\u65f6\u7ea6 29~32 \u79d2\uff0c\u63a5\u53e3\u54cd\u5e94\u8d85\u65f6\uff0c\u5e76\u53d1\u573a\u666f\u4e0b\u6781\u6613\u62d6\u6162\u6574\u4e2a\u5143\u6570\u636e\u5e93\u3002<\/p>\n<h3>\u539f\u56e0\u5206\u6790<\/h3>\n<hr \/>\n<p><strong>\u6838\u5fc3\u74f6\u9888\uff1a\u5185\u7f6e\u89c6\u56fe\u5f3a\u5236\u5168\u91cf\u626b\u63cf\u5343\u4e07\u884c\u5b57\u5178\u8868<\/strong><br \/>\n\u7cfb\u7edf\u81ea\u5e26\u89c6\u56fe<code>partitions_v<\/code>\u3001<code>range_partitions_v<\/code>\u521b\u5efa\u65f6\u4f7f\u7528\u9ed8\u8ba4<code>ALGORITHM=UNDEFINED<\/code>\u7b97\u6cd5\uff0c\u89c6\u56fe\u5185\u90e8\u5305\u542b\u5206\u533a\u805a\u5408\u903b\u8f91\uff08GROUP_CONCAT\u62fc\u63a5\u5206\u533a\u503c\uff09\uff0c\u6570\u636e\u5e93\u4f1a\u6267\u884c\u5148 <strong>\u5168\u8868\u626b\u63cf\u3001\u518d\u751f\u6210\u8d85\u5927\u4e34\u65f6\u8868<\/strong> \u7684\u4f4e\u6548\u903b\u8f91\uff1a<\/p>\n<ol>\n<li>\u65e0\u89c6\u5916\u5c42<code>database_name<\/code>\/<code>table_name<\/code>\u8fc7\u6ee4\u6761\u4ef6\uff0c\u5148\u5b8c\u6574\u626b\u63cf<code>PARTITION_KEY_VALS<\/code>\u5206\u533a\u503c\u5b57\u5178\u8868581 \u4e07\u6761\u5168\u91cf\u6570\u636e\uff1b<\/li>\n<li>\u5bf9 581 \u4e07\u884c\u6570\u636e\u5206\u7ec4\u3001\u62fc\u63a5\u5b57\u7b26\u4e32\uff0c\u751f\u6210 516 \u4e07\u884c\u4e34\u65f6\u6d3e\u751f\u8868\u5b58\u5165\u5185\u5b58 \/ \u78c1\u76d8\uff1b<\/li>\n<li>\u518d\u7528\u76ee\u6807\u8868\u4ec5 1383 \u6761\u5206\u533a\u6570\u636e\uff0c\u5faa\u73af\u5173\u8054\u5343\u4e07\u884c\u4e34\u65f6\u8868\u505a\u5339\u914d\uff1b<\/li>\n<li>99% \u626b\u63cf\u3001\u8ba1\u7b97\u7684\u6d77\u91cf\u6570\u636e\u6700\u7ec8\u5168\u90e8\u4e22\u5f03\uff0c\u4ec5\u4fdd\u7559\u76ee\u6807\u8868\u5c11\u91cf\u5206\u533a\uff0c\u4ea7\u751f\u5927\u91cf\u65e0\u6548 IO \u4e0e CPU \u6d88\u8017\u3002<\/li>\n<\/ol>\n<p><strong>\u6267\u884c\u8ba1\u5212\u4f50\u8bc1\uff08\u5173\u952e\u4fe1\u606f\u89e3\u8bfb\uff09<\/strong><\/p>\n<p>\u4e3b\u8981\u8017\u65f6\u5728 <code>union all<\/code> \u4e0a\u9762\u7684 <code>partitions_v<\/code> \u8868\uff0c\u6267\u884c\u8ba1\u5212\u89e3\u6790<\/p>\n<pre><code class=\"language-shell\">explain analyze\n    SELECT\n    partition_name,\n    NULL AS partition_range\n    FROM metastore_inceptor1.partitions_v\n    WHERE database_name = 'mdwuser'\n    AND table_name = 't_dws_s04_cust_atl_trd_aggr';\n\n-> Nested loop inner join  (cost=8592881703.04 rows=78119330050) (actual time=29131.746..29193.443 rows=1383 loops=1)\n    -> Nested loop inner join  (cost=780927953.03 rows=7809250566) (actual time=28965.008..29016.211 rows=1383 loops=1)\n        -> Index lookup on part using PARTITIONS_N49 (TBL_ID='392739'), with index condition: (part.TBL_ID is not null)  (cost=821.93 rows=1383) (actual time=0.719..38.873 rows=1383 loops=1)\n        -> Index lookup on partition_value using <auto_key0> (part_id=part.PART_ID)  (actual time=20.952..20.952 rows=1 loops=1383)\n            -> Materialize  (cost=1698795.60..1698795.60 rows=5646602) (actual time=28964.239..28964.239 rows=5161874 loops=1)\n                -> Group aggregate: group_concat(PARTITION_KEY_VALS.PART_KEY_VAL separator ',')  (cost=1134135.40 rows=5646602) (actual time=0.075..6504.620 rows=5161874 loops=1)\n                    -> Index scan on PARTITION_KEY_VALS using PRIMARY  (cost=569475.20 rows=5646602) (actual time=0.068..3259.636 rows=5811950 loops=1)\n    -> Filter: (partition_key.tab_id = '392739')  (cost=6312.68..1.50 rows=10) (actual time=0.127..0.128 rows=1 loops=1383)\n        -> Index lookup on partition_key using <auto_key0> (tab_id=part.TBL_ID)  (actual time=0.127..0.127 rows=1 loops=1383)\n            -> Materialize  (cost=7013.65..7013.65 rows=23298) (actual time=166.691..166.691 rows=23301 loops=1)\n                -> Group aggregate: group_concat(PARTITION_KEYS.PKEY_NAME order by PARTITION_KEYS.INTEGER_IDX ASC separator ',')  (cost=4683.85 rows=23298) (actual time=0.054..48.071 rows=23301 loops=1)\n                    -> Index scan on PARTITION_KEYS using PRIMARY  (cost=2354.05 rows=23298) (actual time=0.041..28.414 rows=23688 loops=1)<\/code><\/pre>\n<h3>\u4f18\u5316\u6574\u4f53\u601d\u8def<\/h3>\n<hr \/>\n<p>\u9488\u5bf9\u4e1a\u52a1\u4ec5\u6839\u636e\u5e93\u540d\u3001\u8868\u540d\u53d6\u5c11\u91cf\u5b57\u6bb5\u7684\u8f7b\u91cf\u5316\u67e5\u8be2\u573a\u666f\uff0c\u65b0\u5efa\u4e24\u5957\u4e13\u7528\u7cbe\u7b80\u89c6\u56fe\uff0c\u4ece\u5e95\u5c42\u5f7b\u5e95\u89e3\u51b3\u6162\u67e5\u8be2\u95ee\u9898\uff1a<\/p>\n<ol>\n<li>\u4fee\u6539\u89c6\u56fe\u7b97\u6cd5\uff1a\u4f7f\u7528ALGORITHM=MERGE\u5408\u5e76\u7b97\u6cd5\uff0c\u5916\u5c42WHERE\u8fc7\u6ee4\u6761\u4ef6\u53ef\u76f4\u63a5\u4e0b\u63a8\u5230\u5e95\u5c42\u5b57\u5178\u8868\uff0c\u907f\u514d\u5168\u8868\u626b\u63cf\uff1b<\/li>\n<li>\u7cbe\u7b80\u5173\u8054\u8868\uff1a\u79fb\u9664\u4e1a\u52a1\u4e0d\u9700\u8981\u7684\u5b57\u5178\u8868\u3001\u5b58\u50a8\u8868\uff0c\u51cf\u5c11 JOIN \u6b21\u6570\uff1b<\/li>\n<li>\u6309\u9700\u4fdd\u7559\u5b57\u6bb5\uff1a\u89c6\u56fe\u4ec5\u4fdd\u7559\u7b5b\u9009\u3001\u8fd4\u56de\u5fc5\u9700\u5b57\u6bb5\uff0c\u51cf\u5c11\u78c1\u76d8\u6570\u636e\u8bfb\u53d6\uff1b<\/li>\n<li>\u63d0\u524d\u8fc7\u6ee4\u6570\u636e\uff1a\u89c6\u56fe\u9a71\u52a8\u987a\u5e8f\u6539\u4e3aDBS(\u5e93)\u2192TBLS(\u8868)\u2192PARTITIONS(\u5206\u533a)\uff0c\u5148\u901a\u8fc7\u5e93\u540d\u8868\u540d\u8fc7\u6ee4\u7f29\u5c0f\u6570\u636e\u96c6\uff0c\u518d\u5173\u8054\u5206\u533a\u76f8\u5173\u8868\u3002<\/li>\n<\/ol>\n<h3>\u4f18\u5316\u843d\u5730\u6267\u884c\u811a\u672c<\/h3>\n<hr \/>\n<h4>\u6b65\u9aa4 1\uff1a\u521b\u5efa\u7cbe\u7b80\u89c6\u56fe 1 partitions_v_new_slim<\/h4>\n<p>\u9002\u914d UNION \u4e0a\u534a\u6bb5\u67e5\u8be2\uff0c\u4ec5\u8fd4\u56de\u5206\u533a\u540d\u79f0\u3001\u5e93\u540d\u3001\u8868\u540d\uff0c\u79fb\u9664\u5206\u533a\u952e\u5927\u8868\u5173\u8054\uff1a<\/p>\n<pre><code class=\"language-sql\">CREATE OR REPLACE ALGORITHM=MERGE DEFINER=<code>inceptoruser<\/code>@<code>%<\/code> SQL SECURITY DEFINER VIEW <code>partitions_v_new_slim<\/code> AS\nSELECT\n    p.PART_NAME AS partition_name,\n    t.TBL_NAME AS table_name,\n    d.NAME AS database_name\nFROM metastore_inceptor1.DBS d\nINNER JOIN metastore_inceptor1.TBLS t ON d.DB_ID = t.DB_ID\nINNER JOIN metastore_inceptor1.PARTITIONS p ON t.TBL_ID = p.TBL_ID;<\/code><\/pre>\n<h4>\u6b65\u9aa4 2\uff1a\u521b\u5efa\u7cbe\u7b80\u89c6\u56fe 2 range_partitions_v_slim<\/h4>\n<p>\u9002\u914d UNION \u4e0b\u534a\u6bb5\u67e5\u8be2\uff0c\u4ec5\u8fd4\u56de\u5206\u533a\u540d\u79f0\u3001\u5206\u533a\u8303\u56f4\u3001\u5e93\u540d\u3001\u8868\u540d\uff0c\u79fb\u9664 SDS\u3001\u5206\u533a\u952e\u5197\u4f59\u5173\u8054\uff1a<\/p>\n<pre><code class=\"language-sql\">CREATE OR REPLACE ALGORITHM=MERGE DEFINER=<code>inceptoruser<\/code>@<code>%<\/code> SQL SECURITY DEFINER VIEW <code>range_partitions_v_slim<\/code> AS\nSELECT\n    part.PART_NAME AS partition_name,\n    pr.range AS partition_range,\n    tbls.TBL_NAME AS table_name,\n    dbs.NAME AS database_name\nFROM metastore_inceptor1.DBS dbs\nINNER JOIN metastore_inceptor1.TBLS tbls\n    ON dbs.DB_ID = tbls.DB_ID\nINNER JOIN metastore_inceptor1.PARTITIONS part\n    ON tbls.TBL_ID = part.TBL_ID\n-- \u4ec5\u5173\u8054\u5206\u533a\u8303\u56f4\u8868\uff0c\u7528\u4e8e\u8bfb\u53d6partition_range\u5b57\u6bb5\nLEFT JOIN (\n    SELECT\n        part_ranges.PART_ID,\n        GROUP_CONCAT(CONCAT(part_ranges.LOW,'-',part_ranges.HIGH) SEPARATOR ',') AS range\n    FROM metastore_inceptor1.part_ranges\n    GROUP BY part_ranges.PART_ID\n) pr ON part.PART_ID = pr.part_id;<\/code><\/pre>\n<h4>\u6b65\u9aa4 3\uff1a\u66ff\u6362\u89c6\u56fe\u540e\u7684\u6700\u7ec8\u4e1a\u52a1 SQL<\/h4>\n<pre><code class=\"language-sql\">SELECT\npartition_name AS name,\nNULL AS createTimeSeconds,\npartition_range AS expression\nFROM (\n    SELECT\n    partition_name,\n    NULL AS partition_range\n    FROM metastore_inceptor1.partitions_v_new_slim\n    WHERE database_name = LOWER('mdwuser')\n    AND table_name = LOWER('t_dws_s04_cust_atl_trd_aggr')\n    UNION ALL\n    SELECT\n    partition_name,\n    partition_range\n    FROM metastore_inceptor1.range_partitions_v_slim\n    WHERE database_name = LOWER('mdwuser')\n    AND table_name = LOWER('t_dws_s04_cust_atl_trd_aggr')\n) t\nORDER BY partition_name ;<\/code><\/pre>\n<p>\u67e5\u8be2\u6548\u7387\u4ece 29~32 \u79d2 \u964d\u4f4e\u5230 1\u79d2\u4ee5\u5185\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u95ee\u9898\u80cc\u666f \u5ba2\u6237\u9700\u8981\u6839\u636e\u6570\u636e\u5e93\u540d\u3001\u8868\u540d\u67e5\u8be2\u76ee\u6807\u8868\u6240\u6709\u5206\u533a\u540d\u79f0\u4e0e\u5206\u533a\u8303\u56f4\uff0c\u539f\u59cb SQL \u4f7f\u7528\u7cfb\u7edf\u5185\u7f6e\u89c6\u56feparti ..<\/p>\n<div class=\"clear-fix\"><\/div>\n<p><a href=\"https:\/\/kbwp.transwarp.cn\/?p=19043\" title=\"read more...\">Read more<\/a><\/p>\n","protected":false},"author":12,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-19043","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"acf":[],"_links":{"self":[{"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=\/wp\/v2\/posts\/19043","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=\/wp\/v2\/users\/12"}],"replies":[{"embeddable":true,"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=19043"}],"version-history":[{"count":3,"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=\/wp\/v2\/posts\/19043\/revisions"}],"predecessor-version":[{"id":19046,"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=\/wp\/v2\/posts\/19043\/revisions\/19046"}],"wp:attachment":[{"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=19043"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=19043"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kbwp.transwarp.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=19043"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}