{"id":16752,"date":"2024-12-22T10:24:49","date_gmt":"2024-12-22T02:24:49","guid":{"rendered":"https:\/\/www.orczhou.com\/?p=16752"},"modified":"2025-12-12T15:50:32","modified_gmt":"2025-12-12T07:50:32","slug":"using-polardb-parallel-query","status":"publish","type":"post","link":"https:\/\/www.orczhou.com\/index.php\/2024\/12\/using-polardb-parallel-query\/","title":{"rendered":"\u4f7f\u7528PolarDB\u7684\u5e76\u884c\u67e5\u8be2\u4f18\u5316\u5927\u8d5bSQL"},"content":{"rendered":"\n\n\n\n<p>\u5728\u6628\u5929\u8fdb\u884c\u7684SQL\u7f16\u7a0b\u5927\u8d5b\u4e2d\uff0c\u6240\u6709 MySQL \u9009\u624b\u7684\u6210\u7ee9\u90fd\u6ca1\u6709\u8fdb\u5165\u516b\u5f3a\u7684\u3002\u4e2a\u4eba\u4e5f\u5bf9\u8fd9\u4e2a\u95ee\u9898\u6bd4\u8f83\u611f\u5174\u8da3\uff0c\u7ecf\u8fc7\u521d\u6b65\u5206\u6790\uff0c\u91cd\u8981\u7684\u539f\u56e0\u5728\u4e8e MySQL \u5b9e\u73b0\u4e2d\u6ca1\u6709\u6bd4\u8f83\u597d\u7684\u5e76\u884c\u52a0\u901f\u7684\u80fd\u529b\u3002\u800c\u5728 MySQL \u7684\u884d\u751f\u7248\u672c\u4e2d\uff0c\u5012\u662f\u6709\u51e0\u4e2a\u7248\u672c\u63d0\u4f9b\u4e86\u5e76\u884c\u6267\u884c\u7684\u80fd\u529b\u3002\u5305\u62ec\u4e86 <a href=\"https:\/\/help.aliyun.com\/zh\/polardb\/polardb-for-mysql\/user-guide\/elastic-parallel-query\/\">PolarDB \u7684 Elastic Parallel Query<\/a><sup>[2]<\/sup>\u3001<a href=\"https:\/\/docs.aws.amazon.com\/AmazonRDS\/latest\/AuroraUserGuide\/aurora-mysql-parallel-query.html\">Amazon Aurora \u7684 Parallel query<\/a><sup>[3]<\/sup> \u3002\u6240\u4ee5\uff0c\u4e5f\u6253\u7b97\u9a8c\u8bc1\u4e00\u4e0b\uff0c\u5982\u679c\u52a0\u4e0a\u8fd9\u4e9b\u5e76\u884c\u80fd\u529b\uff0c\u662f\u5426\u80fd\u591f\u66f4\u5feb\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u7ed3\u679c\u7efc\u8ff0<\/h3>\n\n\n\n<p>PolarDB MySQL \u8fd0\u884c\u4e86\u4e0e MySQL \u201c\u51e0\u4e4e\u201d\uff08\u4ec5\u6dfb\u52a0Hint\u5f00\u542f\u5e76\u884c\uff09\u76f8\u540c\u7684SQL\uff08<a href=\"https:\/\/github.com\/orczhou\/myscripts\/blob\/main\/game_ticket\/game_ticket.polardb.sql\">\u53c2\u8003<\/a>\uff09\u8fd0\u884c\u6700\u5feb\u4e3a\uff1a3.821 s\u3002\u76f8\u6bd4\u5728\u540c\u4e00\u4e2a\u96c6\u7fa4\uff0c\u4e0d\u5f00\u542f\u5e76\u884c\u7684\u65f6\u95f4\u662f 6.647s\uff0c\u901f\u5ea6\u63d0\u5347\u4e86 <strong>42.5%<\/strong> \u3002\u6b64\u5916\u4e5f\u6d4b\u8bd5\u4e86Aurora\u7684\u76f8\u540c\u7684\u89c4\u683c\uff0c\u51e0\u7ecf\u8c03\u8bd5\u4f9d\u65e7\u65e0\u6cd5\u4f7f\u7528\u5176\u5e76\u884c\u80fd\u529b\u3002<\/p>\n\n\n\n<p>\u5728\u76f8\u540c\u7684SQL\u5b9e\u73b0\u4e0b\uff0c<span style=\"text-decoration: underline;\">PolarDB MySQL \u53ef\u80fd\u662f\u6240\u6709 MySQL \u7248\u672c\u4e2d\u6027\u80fd\u6700\u597d\u7684<\/span>\u3002\u5982\u679c\uff0c\u611f\u89c9\u8fd8\u6709\u4ec0\u4e48\u7248\u672c\u53ef\u80fd\u6709\u66f4\u597d\u7684\u6027\u80fd\uff0c\u6b22\u8fce\u7559\u8a00\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u6570\u636e\u4e0eSQL<\/h3>\n\n\n\n<p>\u8fd9\u6b21\u662f\u5c1d\u8bd5\u4f7f\u7528 MySQL \u9ad8\u6027\u80fd\u7684\u5b8c\u6210\u201c\u7b2c\u4e8c\u6b21SQL\u7f16\u7a0b\u5927\u8d5b\u201d\u7684\u8fdb\u9636\u6311\u6218\u3002\u5b8c\u6574\u7684\u9898\u76ee\u63cf\u8ff0\u53ef\u4ee5\u53c2\u8003\uff1a<a href=\"https:\/\/www.ninedata.cloud\/sql_train2024\">\u8d5b\u9898\u8bf4\u660e<\/a><sup>[4]<\/sup>\u3002\u8fd9\u91cc\u5b9e\u73b0\u7684 PolarDB MySQL \u7248\u672c SQL \u53c2\u8003\uff1a<a href=\"https:\/\/github.com\/orczhou\/sql-game-2nd\/blob\/main\/gt.polardb.sql\" target=\"_blank\" rel=\"noreferrer noopener\">gt.polardb.sql@GitHub<\/a><sup>[1]<\/sup>\uff08\u6216\u53c2\u8003\u672c\u6587\u7ed3\u5c3e\u90e8\u5206\uff09\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">PolarDB\u7684\u89c4\u683c\u9009\u62e9<\/h3>\n\n\n\n<p>\u8fd9\u91cc\u9009\u62e9\u4e0e\u8bd5\u9898\u7c7b\u4f3c\u76844c8g\u89c4\u683c\uff0c\u8be6\u7ec6\u53c2\u6570\u5982\u4e0b\uff1a<\/p>\n\n\n\n<div class=\"wp-block-columns is-layout-flex wp-container-core-columns-is-layout-28f84493 wp-block-columns-is-layout-flex\">\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<p>\u4e3b\u8981\u53c2\u6570\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>CPU\u67b6\u6784\uff1ax86<\/li>\n\n\n\n<li>\u4ea7\u54c1\u7248\u672c\uff1a\u4f01\u4e1a\u7248<\/li>\n\n\n\n<li>\u5c0f\u7248\u672c\u53f7\uff1a8.0.2\uff08\u4e0e MySQL 8.0.18 \u5b8c\u5168\u517c\u5bb9\uff09<\/li>\n\n\n\n<li>IMCI\u53ea\u8bfb\u8282\u70b9\u4e2a\u6570\uff1a0<\/li>\n\n\n\n<li>\u521d\u59cb\u53ea\u8bfb\u8282\u70b9\u4e2a\u6570\uff1a0<\/li>\n\n\n\n<li>\u521d\u59cb\u8bfb\u5199\u8282\u70b9\u4e2a\u6570\uff1a1<\/li>\n\n\n\n<li>\u8282\u70b9\u89c4\u683c\uff1a4 \u6838 8GB\uff08\u901a\u7528\uff09<\/li>\n\n\n\n<li>\u5b58\u50a8\u7c7b\u578b\uff1aPSL5<\/li>\n<\/ul>\n<\/div>\n\n\n\n<div class=\"wp-block-column is-layout-flow wp-block-column-is-layout-flow\">\n<figure class=\"wp-block-image size-full is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"596\" height=\"880\" src=\"https:\/\/www.orczhou.com\/wp-content\/uploads\/2024\/12\/image-68.png\" alt=\"\" class=\"wp-image-16817\" style=\"width:296px;height:auto\" srcset=\"https:\/\/www.orczhou.com\/wp-content\/uploads\/2024\/12\/image-68.png 596w, https:\/\/www.orczhou.com\/wp-content\/uploads\/2024\/12\/image-68-203x300.png 203w\" sizes=\"auto, (max-width: 596px) 100vw, 596px\" \/><\/figure>\n<\/div>\n<\/div>\n\n\n\n<h3 class=\"wp-block-heading\">\u5728 PolarDB \u4e0a\u5e76\u884c\u6267\u884c<\/h3>\n\n\n\n<p>PolarDB \u7684\u5e76\u884c\u6267\u884c\u53ef\u4ee5\u4f7f\u7528 Hint \u8f83\u4e3a\u65b9\u4fbf\u7684\u5f00\u542f\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">SELECT\n  \/*+PARALLEL(8)*\/\n...<\/code><\/pre>\n\n\n\n<p>\u53ef\u4ee5\u901a\u8fc7\u6267\u884c\u8ba1\u5212\u89c2\u5bdf\uff0c\u5b9e\u9645\u662f\u5426\u4f7f\u7528\u4e86\u5e76\u884c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">| -&gt; Gather (merge sort; slice: 1; workers: 8)  (cost=2024861688.70 rows=1995124000) (actual time=1774.761..2159.495 rows=1000000 loops=1)\n    -&gt; Sort: &lt;temporary&gt;.p_id  (cost=1705198767.38 rows=249390500) (actual time=480.641,781.971,239.446..503.108,818.048,250.387 rows=125000,220305,60043 loops=1,1,1)\n        -&gt; Stream results (actual time=1.631,2.232,1.396..380.381,630.231,168.213 rows=125000,220305,60043 loops=1,1,1)\n            -&gt; Left hash join (t_01.a_s = p_01.a_s), (t_01.d_s = p_01.d_s), extra conditions: ((p_01.seq &gt;= ((t_01.p_seat_to - t_01.seat_count) + 1)) and (p_01.seq &lt;= t_01.p_seat_to))  (cost=25015432.30 rows=249390500) (actual time=1.621,2.220,1.387..200.048,332.885,86.841 rows=125000,220305,60043 loops=1,1,1)\n                -&gt; Parallel table scan on p_01, with parallel partitions: 8 (actual time=0.002,0.003,0.002..48.149,74.839,20.489 rows=125000,220305,60043 loops=1,1,1)\n                    -&gt; Materialize (shared access, partitions: 8, partition_keys: a_s,) (actual time=0.001,0.002,0.001..24.240,34.098,9.331 rows=125000,220305,60043 loops=1,1,1)\n                        -&gt; Gather (slice: 1; workers: 8)  (cost=1146187.18 rows=997560) (actual time=158.204..362.897 rows=1000000 loops=1)\n                            -&gt; Window aggregate  (cost=1090064.43 rows=124695) (actual time=160.066,167.128,157.433..296.715,314.531,278.945 rows=125000,149658,102922 loops=1,1,1)\n                                -&gt; Repartition (hash keys: passenger.departure_station, passenger.arrival_station; merge sort; slice: 2; workers: 8)  (cost=514229.38 rows=124695) (actual time=160.059,167.121,157.424..223.566,236.036,217.548 rows=125000,149658,102922 loops=1,1,1)\n                                    -&gt; Sort: passenger.departure_station, passenger.arrival_station  (cost=12554.66 rows=124695) (actual time=152.998,157.330,149.335..172.035,180.041,168.003 rows=125000,132932,111478 loops=1,1,1)\n                                        -&gt; Parallel table scan on passenger, with parallel partitions: 745 (actual time=0.052,0.058,0.046..57.559,65.907,54.511 rows=125000,132932,111478 loops=1,1,1)\n                -&gt; Hash\n                    -&gt; Table scan on t_01\n                        -&gt; Materialize (shared access) (actual time=0.002,0.003,0.001..0.744,0.935,0.666 rows=2000,2000,2000 loops=1,1,1)\n                            -&gt; Gather (slice: 1; workers: 8)  (cost=3416.33 rows=2000) (actual time=6.884..7.646 rows=2000 loops=1)\n                                -&gt; Window aggregate with buffering  (cost=3293.83 rows=250) (actual time=1.955,2.233,1.724..3.692,3.760,3.633 rows=250,292,196 loops=1,1,1)\n                                    -&gt; Repartition (hash keys: t_include_no_seat.d_s, t_include_no_seat.a_s; slice: 2; workers: 1)  (cost=3161.31 rows=250) (actual time=1.930,2.215,1.691..2.759,2.922,2.589 rows=250,292,196 loops=1,1,1)\n                                        -&gt; Sort: t_include_no_seat.d_s, t_include_no_seat.a_s, t_include_no_seat.if_no_seat, t_include_no_seat.t_id (actual time=1.383,1.383,1.383..2.070,2.070,2.070 rows=2000,2000,2000 loops=1,1,1)\n                                            -&gt; Table scan on t_include_no_seat (actual time=1.381,1.381,1.381..1.685,1.685,1.685 rows=2000,2000,2000 loops=1,1,1)\n                                                -&gt; Materialize with deduplication (shared access) (actual time=0.001,0.001,0.001..0.295,0.295,0.295 rows=2000,2000,2000 loops=1,1,1)\n                                                    -&gt; Table scan on &lt;union temporary&gt;  (cost=2.50 rows=0) (actual time=0.001..0.290 rows=2000 loops=1)\n                                                        -&gt; Union materialize with deduplication  (actual time=2.330..3.003 rows=2000 loops=1)\n                                                            -&gt; Table scan on train  (cost=101.25 rows=1000) (actual time=0.050..0.380 rows=1000 loops=1)\n                                                            -&gt; Table scan on train  (cost=101.25 rows=1000) (actual time=0.023..0.348 rows=1000 loops=1)\n |<\/code><\/pre>\n\n\n\n<p>\u8fd9\u91cc\u7684\u8bf8\u5982<code>Gather (merge sort; slice: 1; workers: 8)<\/code>\u7b49\u5185\u5bb9\uff0c\u663e\u793a\u5bf9\u5e94\u7684\u90e8\u5206\u4f1a\u901a\u8fc7\u591a\u7ebf\u7a0b\u5e76\u884c\u6267\u884c\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u6267\u884c\u65f6\u95f4\u7edf\u8ba1<\/h3>\n\n\n\n<p>\u8fd9\u91cc\u8fd0\u884c\u4e86\u8be5 SQL \u4e09\u6b21\u7684\u7ed3\u679c\u7edf\u8ba1\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">real\t0m3.863s\nuser\t0m0.379s\nsys\t0m0.100s\n\nreal\t0m3.917s\nuser\t0m0.414s\nsys\t0m0.123s\n\nreal\t0m3.821s\nuser\t0m0.422s\nsys\t0m0.128s<\/code><\/pre>\n\n\n\n<p>\u8bf4\u660e\uff1a\u8fd9\u91cc\uff0c\u56e0\u4e3aPolarDB\u662f\u8fd0\u884c\u5728\u4e91\u7aef\uff0c\u6545\u4ec5\u9700\u5173\u6ce8\u8fd9\u91cc\u7684 <code>real<\/code> \u90e8\u5206\u7684\u65f6\u95f4\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u4e0d\u5f00\u542f\u5e76\u884c\u65f6 PolarDB \u7684\u6027\u80fd<\/h3>\n\n\n\n<p>\u8be5\u7ec4\u6570\u636e\u53ef\u7528\u5bf9\u6bd4\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">real\t0m6.743s\nuser\t0m0.394s\nsys\t0m0.120s\n\nreal\t0m6.647s\nuser\t0m0.393s\nsys\t0m0.120s\n\nreal\t0m6.665s\nuser\t0m0.407s\nsys\t0m0.125s<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u5e76\u884c\u6267\u884c\u7684\u4e00\u4e9b\u72b6\u6001\u53c2\u6570<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">mysql&gt; show global status like '%pq_%';\n+-------------------------------------+-------+\n| Variable_name                       | Value |\n+-------------------------------------+-------+\n| PQ_fallback_one_worker              | 0     |\n| PQ_local_workers_created            | 297   |\n| PQ_migrant_workers_created          | 0     |\n| PQ_net_exchange_fail_connect        | 0     |\n| PQ_refused_over_computing_resource  | 0     |\n| PQ_refused_over_max_queuing_time    | 0     |\n| PQ_refused_over_total_workers       | 0     |\n| PQ_remote_workers_created           | 0     |\n| PQ_running_local_workers            | 0     |\n| PQ_running_migrant_workers          | 0     |\n| PQ_running_remote_workers           | 0     |\n| PQ_sched_adative_resource_dec_count | 0     |\n| PQ_sched_adative_resource_inc_count | 0     |\n+-------------------------------------+-------+<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u4f7f\u7528 Aurora \u7684\u5e76\u884c\u6267\u884c<\/h3>\n\n\n\n<p>\u8fd9\u91cc\u4e5f\u5c1d\u8bd5\u4f7f\u7528 Aurora \u7684\u5e76\u884c\u67e5\u8be2\u8fdb\u884c\u4f18\u5316\uff0c\u4f46\u662f\u5e76\u6ca1\u6709\u6210\u529f\u3002Aurora \u7684\u5e76\u884c\u6267\u884c\u5e76\u6ca1\u6709 Hint \u53ef\u4ee5\u63a7\u5236\uff0c\u800c\u662f\u4f18\u5316\u5668\u6839\u636e\u9700\u8981\u9009\u62e9\u4f7f\u7528\u3002\u5728\u672c\u6b21\u6d4b\u8bd5\u4e2d\uff0c\u5728\u4e00\u4e2a 4c32gb \u7684Aurora\u5b9e\u4f8b\u4e0a\uff0c\u51e0\u7ecf\u5c1d\u8bd5\uff0c\u90fd\u672a\u80fd\u5b9e\u73b0\u5e76\u884c\u3002\u6545\u672a\u6210\u529f\u6d4b\u8bd5\u3002\u5728\u975e\u5e76\u884c\u65f6\uff0cAurora\u7684\u6267\u884c\u65f6\u95f4\u4e3a 6.921s\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u5f00\u542fAurora\u5e76\u884c\u6267\u884c<\/h4>\n\n\n\n<p>\u8981\u4f7f\u7528 Aurora \u7684\u5e76\u884c\u6267\u884c\u80fd\u529b\uff0c\u9700\u8981\u5148\u521b\u5efa\u6700\u65b0\u7248\u672c\u7684Aurora\uff0c\u5728\u9009\u62e9\u7684\u53c2\u6570\u7ec4\uff08parameter group\uff09\u65f6\uff0c\u8be5\u53c2\u6570\u7ec4\u9700\u8981\u6253\u5f00 <code>aurora_parallel_query <\/code>\u53c2\u6570\u3002\u5728\u5b9e\u4f8b\u521b\u5efa\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u5982\u4e0b\u547d\u4ee4\u67e5\u770b\u5e76\u884c\u67e5\u8be2\u662f\u5426\u6253\u5f00\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"sql\" class=\"language-sql\">mysql&gt; show global variables like '%aurora_parallel_query%';\n+-----------------------+-------+\n| Variable_name         | Value |\n+-----------------------+-------+\n| aurora_parallel_query | ON    |\n+-----------------------+-------+<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Aurora\u4e0a\u7684\u6267\u884c\u65f6\u95f4<\/h4>\n\n\n\n<p>\u8fd9\u91cc\u8bb0\u5f55\u76f8\u5173SQL\u7684\u6267\u884c\u65f6\u95f4\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code class=\"\">real\t0m6.921s\nuser\t0m1.022s\nsys\t0m0.076s\n[ec2-user@xterm-256color- delete_me]$ time mysql --local-infile=true -hpq-testing.cluster-cjzowaj9vqpd.ap-northeast-1.rds.amazonaws.com -ub_admin -p-f7HNhmp_frX game_ticket &lt; aurora.sql &gt; aurora.ret\nmysql: [Warning] Using a password on the command line interface can be insecure.\n\nreal\t0m6.955s\nuser\t0m1.012s\nsys\t0m0.076s\n[ec2-user@xterm-256color- delete_me]$ time mysql --local-infile=true -hpq-testing.cluster-cjzowaj9vqpd.ap-northeast-1.rds.amazonaws.com -ub_admin -p-f7HNhmp_frX game_ticket &lt; aurora.sql &gt; aurora.ret\nmysql: [Warning] Using a password on the command line interface can be insecure.\n\nreal\t0m7.154s\nuser\t0m0.970s\nsys\t0m0.138s<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">\u6700\u540e<\/h3>\n\n\n\n<p>PolarDB MySQL \u5728\u5e76\u884c\u6267\u884c\u5f00\u542f\u7684\u60c5\u51b5\u4e0b\u6027\u80fd\u63d0\u5347\u4e8642.5%\uff0c\u6700\u7ec8\u6267\u884c\u65f6\u95f4\u4e3a 3.821 s\u3002\u6709\u53ef\u80fd\u662f\u6240\u6709 MySQL \u517c\u5bb9\u7684\u53d1\u578b\u7248\u672c\u4e2d\u6027\u80fd\u6700\u5feb\u7684\u3002<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">gt.polardb.sql<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code lang=\"sql\" class=\"language-sql\">-- explain analyze\nWITH\n  t_no_seat_virtual AS (\n    select\n      train_id as t_id,\n      departure_station as d_s,\n      arrival_station as a_s,\n      seat_count,\n      seat_count*0.1 as seat_count_no_seat\n    from train\n  ),\n  t_include_no_seat AS (\n    select t_id,d_s ,a_s ,seat_count, 0 as if_no_seat\n    from t_no_seat_virtual\n    union\n    select t_id,d_s ,a_s ,seat_count_no_seat, 1 as if_no_seat\n    from t_no_seat_virtual\n  )\nSELECT\n  \/*+PARALLEL(8)*\/\n  p_01.p_id,         -- output 01\n  p_01.d_s,          -- output 02\n  p_01.a_s,          -- output 03\n  t_01.t_id as t_id, -- output 04\n  IF(\n      if_no_seat,\n      \"\" ,\n      ceil((p_01.seq-t_01.p_seat_to + t_01.seat_count)\/100)\n  ) as t_carr_id, -- output 05\n\n  CASE IF( !isnull(t_01.t_id) and if_no_seat,-1,ceil((( p_01.seq-t_01.p_seat_to + t_01.seat_count )%100)%5))\n    WHEN 1  THEN CONCAT( ceil((( p_01.seq-t_01.p_seat_to + t_01.seat_count )%100)\/5) ,\"A\")\n    WHEN 2  THEN CONCAT( ceil((( p_01.seq-t_01.p_seat_to + t_01.seat_count )%100)\/5) ,\"B\")\n    WHEN 3  THEN CONCAT( ceil((( p_01.seq-t_01.p_seat_to + t_01.seat_count )%100)\/5) ,\"C\")\n    WHEN 4  THEN CONCAT( ceil((( p_01.seq-t_01.p_seat_to + t_01.seat_count )%100)\/5) ,\"E\")\n    WHEN 0  THEN CONCAT( IF( (p_01.seq-t_01.p_seat_to + t_01.seat_count)%100 = 0, \"20\" ,ceil((( p_01.seq-t_01.p_seat_to + t_01.seat_count )%100)\/5)) ,\"F\")\n    WHEN -1 THEN \"\u65e0\u5ea7\"\n    ELSE NULL\n  END as seat_index   -- output 06\nFROM\n  (\n    select\n      \/*+PARALLEL(8)*\/\n      ROW_NUMBER() over(PARTITION BY departure_station,arrival_station) as seq ,\n      passenger_id as p_id,\n      departure_station as d_s,\n      arrival_station as a_s\n    from\n    passenger\n  ) as p_01\n\n  LEFT JOIN\n\n  (\n    select\n      \/*+PARALLEL(8)*\/\n      seat_count,\n      sum(seat_count)\n        over (\n               PARTITION BY d_s,a_s\n               ORDER BY     if_no_seat,t_id\n             ) as p_seat_to ,\n      t_id,\n      d_s ,\n      a_s ,\n      if_no_seat\n    from\n    t_include_no_seat\n  ) t_01\n\n  ON\n        p_01.seq &gt;= p_seat_to-seat_count + 1\n    and p_01.seq &lt;= p_seat_to\n    and p_01.d_s =  t_01.d_s\n    and p_01.a_s =  t_01.a_s\nORDER BY p_01.p_id<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u53c2\u8003\u94fe\u63a5<\/h3>\n\n\n\n<ul class=\"wp-block-list is-style-no-disc\">\n<li>[1] <a href=\"https:\/\/github.com\/orczhou\/sql-game-2nd\/blob\/main\/gt.polardb.sql\">https:\/\/github.com\/orczhou\/sql-game-2nd\/blob\/main\/gt.polardb.sql<\/a> <\/li>\n\n\n\n<li>[2] <a href=\"https:\/\/help.aliyun.com\/zh\/polardb\/polardb-for-mysql\/user-guide\/elastic-parallel-query\/\">https:\/\/help.aliyun.com\/zh\/polardb\/polardb-for-mysql\/user-guide\/elastic-parallel-query\/<\/a><\/li>\n\n\n\n<li>[3] <a href=\"https:\/\/docs.aws.amazon.com\/AmazonRDS\/latest\/AuroraUserGuide\/aurora-mysql-parallel-query.html\">https:\/\/docs.aws.amazon.com\/AmazonRDS\/latest\/AuroraUserGuide\/aurora-mysql-parallel-query.html<\/a><\/li>\n\n\n\n<li>[4] <a href=\"https:\/\/www.ninedata.cloud\/sql_train2024\">https:\/\/www.ninedata.cloud\/sql_train2024<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u6628\u5929\u8fdb\u884c\u7684SQL\u7f16\u7a0b\u5927\u8d5b\u4e2d\uff0c\u6240\u6709 MySQL \u9009\u624b\u7684\u6210\u7ee9\u90fd\u6ca1\u6709\u8fdb\u5165\u516b\u5f3a\u7684\u3002\u4e2a\u4eba\u4e5f\u5bf9\u8fd9\u4e2a\u95ee\u9898\u6bd4\u8f83\u611f\u5174\u8da3\uff0c\u7ecf\u8fc7\u521d\u6b65\u5206\u6790\uff0c\u91cd\u8981\u7684\u539f\u56e0\u5728\u4e8e MySQL \u5b9e\u73b0\u4e2d\u6ca1\u6709\u6bd4\u8f83\u597d\u7684\u5e76\u884c\u52a0\u901f\u7684\u80fd\u529b\u3002\u800c\u5728 MySQL \u7684\u884d\u751f\u7248\u672c\u4e2d\uff0c\u5012\u662f\u6709\u51e0\u4e2a\u7248\u672c\u63d0\u4f9b\u4e86\u5e76\u884c\u6267\u884c\u7684\u80fd\u529b\u3002\u5305\u62ec\u4e86 PolarDB \u7684 Elastic Parallel Query[2]\u3001Amazon Aurora \u7684 Parallel query[3] \u3002\u6240\u4ee5\uff0c\u4e5f\u6253\u7b97\u9a8c\u8bc1\u4e00\u4e0b\uff0c\u5982\u679c\u52a0\u4e0a\u8fd9\u4e9b\u5e76\u884c\u80fd\u529b\uff0c\u662f\u5426\u80fd\u591f\u66f4\u5feb\u3002 \u7ed3\u679c\u7efc\u8ff0 PolarDB MySQL \u8fd0\u884c\u4e86\u4e0e MySQL \u201c\u51e0\u4e4e\u201d\uff08\u4ec5\u6dfb\u52a0Hint\u5f00\u542f\u5e76\u884c\uff09\u76f8\u540c\u7684SQL\uff08\u53c2\u8003\uff09\u8fd0\u884c\u6700\u5feb\u4e3a\uff1a3.821 s\u3002\u76f8\u6bd4\u5728\u540c\u4e00\u4e2a\u96c6\u7fa4\uff0c\u4e0d\u5f00\u542f\u5e76\u884c\u7684\u65f6\u95f4\u662f 6.647s\uff0c\u901f\u5ea6\u63d0\u5347\u4e86 42.5% \u3002\u6b64\u5916\u4e5f\u6d4b\u8bd5\u4e86Aurora\u7684\u76f8\u540c\u7684\u89c4\u683c\uff0c\u51e0\u7ecf\u8c03\u8bd5\u4f9d\u65e7\u65e0\u6cd5\u4f7f\u7528\u5176\u5e76\u884c\u80fd\u529b\u3002 \u5728\u76f8\u540c\u7684SQL\u5b9e\u73b0\u4e0b\uff0cPolarDB MySQL \u53ef\u80fd\u662f\u6240\u6709 MySQL \u7248\u672c\u4e2d\u6027\u80fd\u6700\u597d\u7684\u3002\u5982\u679c\uff0c\u611f\u89c9\u8fd8\u6709\u4ec0\u4e48\u7248\u672c\u53ef\u80fd\u6709\u66f4\u597d\u7684\u6027\u80fd\uff0c\u6b22\u8fce\u7559\u8a00\u3002 \u6570\u636e\u4e0eSQL \u8fd9\u6b21\u662f\u5c1d\u8bd5\u4f7f\u7528 MySQL \u9ad8\u6027\u80fd\u7684\u5b8c\u6210\u201c\u7b2c\u4e8c\u6b21SQL\u7f16\u7a0b\u5927\u8d5b\u201d\u7684\u8fdb\u9636\u6311\u6218\u3002\u5b8c\u6574\u7684\u9898\u76ee\u63cf\u8ff0\u53ef\u4ee5\u53c2\u8003\uff1a\u8d5b\u9898\u8bf4\u660e[4]\u3002\u8fd9\u91cc\u5b9e\u73b0\u7684 PolarDB MySQL \u7248\u672c SQL \u53c2\u8003\uff1agt.polardb.sql@GitHub[1]\uff08\u6216\u53c2\u8003\u672c\u6587\u7ed3\u5c3e\u90e8\u5206\uff09\u3002 PolarDB\u7684\u89c4\u683c\u9009\u62e9 \u8fd9\u91cc\u9009\u62e9\u4e0e\u8bd5\u9898\u7c7b\u4f3c\u76844c8g\u89c4\u683c\uff0c\u8be6\u7ec6\u53c2\u6570\u5982\u4e0b\uff1a \u4e3b\u8981\u53c2\u6570\uff1a \u5728 PolarDB \u4e0a\u5e76\u884c\u6267\u884c PolarDB \u7684\u5e76\u884c\u6267\u884c\u53ef\u4ee5\u4f7f\u7528 Hint \u8f83\u4e3a\u65b9\u4fbf\u7684\u5f00\u542f\uff1a \u53ef\u4ee5\u901a\u8fc7\u6267\u884c\u8ba1\u5212\u89c2\u5bdf\uff0c\u5b9e\u9645\u662f\u5426\u4f7f\u7528\u4e86\u5e76\u884c\uff1a \u8fd9\u91cc\u7684\u8bf8\u5982Gather (merge sort; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"wp-custom-template-a-1440-px-width-template","format":"standard","meta":{"_eb_attr":"","inline_featured_image":false,"_tocer_settings":[],"footnotes":""},"categories":[6],"tags":[],"class_list":["post-16752","post","type-post","status-publish","format-standard","hentry","category-mysql"],"_links":{"self":[{"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/posts\/16752","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/comments?post=16752"}],"version-history":[{"count":25,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/posts\/16752\/revisions"}],"predecessor-version":[{"id":21300,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/posts\/16752\/revisions\/21300"}],"wp:attachment":[{"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/media?parent=16752"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/categories?post=16752"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.orczhou.com\/index.php\/wp-json\/wp\/v2\/tags?post=16752"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}