{"id":10007007,"date":"2022-12-24T02:39:46","date_gmt":"2022-12-23T18:39:46","guid":{"rendered":"https:\/\/ityet.com\/?p=10007007"},"modified":"2022-12-24T02:39:46","modified_gmt":"2022-12-23T18:39:46","slug":"%e7%b4%a2%e5%bc%95%e9%80%89%e6%8b%a9%e5%ba%a6%e9%97%ae%e9%a2%98%e4%bc%98%e5%8c%96%e6%95%b4%e7%90%86","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10007007","title":{"rendered":"\u7d22\u5f15\u9009\u62e9\u5ea6\u95ee\u9898\u4f18\u5316\u6574\u7406"},"content":{"rendered":"<blockquote><p>\u4e4b\u524d\u5728\u641e\u5b9c\u642d\u5143\u6570\u636e\u5e95\u5c42\u7d22\u5f15\u4f18\u5316\u7684\u65f6\u5019\uff0c\u9488\u5bf9\u4e00\u4e9b\u67e5\u8be2\u65f6\u5feb\u65f6\u6162\uff0c\u4ee5\u53ca\u4e00\u4e9b\u7d22\u5f15\u9009\u62e9\u7684\u95ee\u9898\uff0c\u7814\u7a76\u8fc7\uff0c\u4e5f\u57fa\u4e8e\u770b\u8fc7\u7684\u4e00\u4e9b\u6848\u4f8b\u4ee5\u53ca\u81ea\u8eab\u5f52\u7eb3\u601d\u8003\uff0c\u4e0b\u9762\u6574\u7406\u5206\u4eab\u4e0b\uff1b<\/p><\/blockquote>\n<p>\u4e00\u822c\u6211\u4eec\u4e3a\u4e86\u52a0\u5feb\u67e5\u8be2\u901f\u5ea6\uff0c\u4f1a\u8bbe\u8ba1\u7d22\u5f15\uff0c\u5f53\u7136\u6709\u7d22\u5f15\u60c5\u51b5\u4e0b\uff0c\u5927\u591a\u662f\u4f1a\u547d\u4e2d\u53bb\u8d70\u7d22\u5f15\u67e5\u8be2\uff1b\u4f46\u662f\u5462:<\/p>\n<ol>\n<li>\u5b58\u50a8\u4f18\u5316\u5668\u53bb\u6267\u884c\uff0c\u5c31\u7b97\u52a0\u4e86\u7d22\u5f15\uff0c\u5728\u4e00\u5b9a\u65f6\u5019\u6709\u53ef\u80fd\u6ca1\u7528\u5230\u7d22\u5f15\uff0c\u901f\u5ea6\u4f1a\u66f4\u6162\u70b9\uff0c\u8fd9\u662f\u4e3a\u4ec0\u4e48\u4e0d\u7528\uff1f<\/li>\n<li>\u6709\u4e9b\u65f6\u5019\u540c\u4e00\u4e2a\u7528\u6237\u4e0d\u540c\u65f6\u95f4\u53bb\u8bf7\u6c42\uff0c\u4ea7\u751f\u76f8\u540cSQL\u8bed\u53e5\u53bb\u67e5\u8be2\u4e5f\u53ef\u80fd\u51fa\u73b0\u4e0d\u540c\u7684\u5feb\u6162\u6027\u80fd\uff0c\u8fd9\u53c8\u662f\u4e3a\u4ec0\u4e48\uff1f<\/li>\n<li>\u5c31\u7b97\u547d\u4e2d\u4e86\u7d22\u5f15\uff0c\u901f\u5ea6\u53ef\u80fd\u66f4\u6162\uff0c\u8fd9\u6700\u540e\u53c8\u662f\u4e3a\u4ec0\u4e48\uff1f<\/li>\n<\/ol>\n<p>\u5148\u4e3e\u51e0\u4e2a\u4e4b\u524d\u77e5\u9053\u7684\u4f8b\u5b50\u52a0\u4ee5\u5206\u6790\uff1b<br \/><a name=\"JkuhD\"><\/a><\/p>\n<h2>\u6848\u4f8b\u4e00<\/h2>\n<pre><code class=\"sql\">-- \u521b\u5efa\u6d4b\u8bd5\u8868\nCREATE TABLE `t` (\n  `id` int primary key auto_increment,\n  `a` int default null,\n  `b` int default null,\n  KEY `a` (`a`),\n  KEY `b` (`b`)\n) ENGINE=InnoDB;\n\n-- \u63d2\u516510w\u884c\u6d4b\u8bd5\u6570\u636e\ndelimiter ;;\ncreate procedure idata()\nbegin\n  declare i int;\n  set i=1;\n  while(i&lt;=100000)do\n    insert into t values(i, i, i);\n    set i=i+1;\n  end while;\nend;;\ndelimiter ;\ncall idata();<\/code><\/pre>\n<p><strong>mysql\u5168\u8868\u626b\u63cf<\/strong><br \/>explain select <em> from t where a between 10000 and 20000; <br \/><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043146825\" alt=\"\" title=\"\" loading=\"lazy\"><br \/>\u901a\u8fc7explain\u7684\u6267\u884c\u7ed3\u679c\u6211\u4eec\u53ef\u4ee5\u770b\u51fa\uff0c\u4e0a\u9762\u7684SQL\u8bed\u53e5\u5e76\u6ca1\u6709\u8d70\u6211\u4eec\u7684\u7d22\u5f15a\uff0c\u800c\u662f\u76f4\u63a5\u4f7f\u7528\u4e86\u5168\u8868\u626b\u63cf\u3002<br \/>&#8212; \u5f3a\u5236\u8d70\u7d22\u5f15a explain select <\/em> from t force index(a) where a between 10000 and 20000; <br \/><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043146826\" alt=\"\" title=\"\" loading=\"lazy\"><br \/>\u901a\u8fc7explain\u7684\u6267\u884c\u7ed3\u679c\u6211\u4eec\u53ef\u4ee5\u770b\u51fa\uff0c\u4e0a\u9762\u7684SQL\u8bed\u53e5\u6211\u4eec\u901a\u8fc7force index(a)\u4ee5\u540e\uff0c\u786e\u5b9e\u4f7f\u7528\u4e86\u7d22\u5f15\u3002<br \/>&#8212; \u5f00\u542f\u6162\u65e5\u5fd7 set global slow_query_log  = true; set long_query_time = 0; &#8212; \u5206\u522b\u6267\u884c\u4e0d\u8d70\u7d22\u5f15\u548c\u8d70\u7d22\u5f15\u7684SQL select <em> from t where a between 10000 and 20000; select <\/em> from t force index(a) where a between 10000 and 20000; <br \/><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043146827\" alt=\"image.png\" title=\"image.png\" loading=\"lazy\"><\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043146828\" alt=\"image.png\" title=\"image.png\" loading=\"lazy\"><br \/>\u53ef\u4ee5\u770b\u51fa\u8d70\u7d22\u5f15\u7684\u67e5\u8be2\u6bd4\u4e0d\u8d70\u7d22\u5f15\u7684\u67e5\u8be2\u5feb\u4e86\u5c06\u8fd110ms\u3002<br \/>\u4f46\u662f\u5b58\u50a8\u4f18\u5316\u5668\u9ed8\u8ba4\u6ca1\u8d70\u7d22\u5f15\u7684\u67e5\u8be2\uff0c\u867d\u7136\u52a0\u4e86\u7d22\u5f15<br \/><a name=\"YwjIL\"><\/a><\/p>\n<h2>\u6848\u4f8b\u4e8c<\/h2>\n<p>\u9488\u5bf9\u67d0\u4e2a\u5e73\u53f0\u6709\u5f20\u6d88\u606f\u53d1\u9001\u4ea4\u6d41\u7684\u8868\uff0c\u89c4\u6a21\u8fbe\u5230\u6570\u5343\u4e07\u884c\u7ea7\uff0cPG\u5b58\u50a8\uff1b\u6d88\u606f\u8868\u4e0a\u7684\u4e3b\u67e5\u8be2\u901a\u5e38\u6781\u5feb\uff0c\u4f46\u662f\u4e5f\u9047\u5230\u4e86\u4e00\u4e9b\u95f4\u6b47\u7684\u6162\u67e5\u8be2\u8d85\u65f6\u3002\u6162\u67e5\u8be2\u4e0d\u4f46\u5f71\u54cd\u4e86\u6d88\u606f\u529f\u80fd\u7684\u7528\u6237\u4f53\u9a8c\uff0c\u800c\u4e14\u52a0\u5927\u4e86\u6574\u4e2a\u7cfb\u7edf\u7684\u8d1f\u8377\uff0c\u62d6\u6162\u4e86\u5176\u4ed6\u529f\u80fd\u7684\u7528\u6237\u4f53\u9a8c\u3002<br \/>\u8fd9\u4e2a\u67e5\u8be2\u957f\u8fd9\u6837\uff1a<\/p>\n<pre><code class=\"sql\">SELECT messages.* FROM messages  WHERE messages.deleted_at IS NULL AND messages.namespace = ?  AND (        jsonb_extract_path_text(context, 'topic') IN (?, ?)        OR jsonb_extract_path_text(context, 'topic') LIKE ?      )  AND ( context @&gt; '{&quot;involved_parties&quot;:[{&quot;id&quot;:1,&quot;type&quot;:1}]}'::jsonb ) ORDER BY messages.created_at ASC<\/code><\/pre>\n<p>\u5728context\u4e0a\u6709\u4e24\u4e2a\u7d22\u5f15<\/p>\n<ol>\n<li>context\u5217\u4e0a\u7684GIN\u7d22\u5f15<\/li>\n<li>jsonb_extract_path_text(context, \u2018topic\u2019)\u8868\u8fbe\u5f0f\u4e0a\u7684BTREE\u8868\u8fbe\u5f0f\u7d22\u5f15<\/li>\n<\/ol>\n<p>\u770b\u4e0b\u4e0a\u9762\u8bed\u53e5\u5076\u5c14\u6162\u7684\u65f6\u5019QUERY PLAN\uff1a<\/p>\n<pre><code class=\"sql\">UERY PLAN\n------------------------------------------------------------------------------\n Sort  (cost=540.08..540.09 rows=3 width=915)\n   Sort Key: created_at\n   -&gt;  Bitmap Heap Scan on messages  (cost=536.03..540.06 rows=3 width=915)\n         Recheck Cond: (((jsonb_extract_path_text(context, VARIADIC '{topic}'::text[]) = ANY ('{?,?}'::text[])) OR (jsonb_extract_path_text(context, VARIADIC '{topic}'::text[]) ~~ '?%'::text)) AND (context @&gt; '{&quot;involved_parties&quot;: [{&quot;id&quot;: 1, &quot;type&quot;: 1}]}'::jsonb))\n         Filter: ((deleted_at IS NULL) AND ((namespace)::text = '?'::text) AND ((jsonb_extract_path_text(context, VARIADIC '{topic}'::text[]) = ANY ('{?,?}'::text[])) OR (jsonb_extract_path_text(context, VARIADIC '{topic}'::text[]) ~~ '?%'::text)))\n         -&gt;  BitmapAnd  (cost=536.03..536.03 rows=1 width=0)\n               -&gt;  BitmapOr  (cost=20.13..20.13 rows=249 width=0)\n                     -&gt;  Bitmap Index Scan on index_messages_on_topic_key_string  (cost=0.00..15.55 rows=249 width=0)\n                           Index Cond: (jsonb_extract_path_text(context, VARIADIC '{topic}'::text[]) = ANY ('{?,?}'::text[]))\n                     -&gt;  Bitmap Index Scan on index_messages_on_topic_key_string  (cost=0.00..4.57 rows=1 width=0)\n                           Index Cond: ((jsonb_extract_path_text(context, VARIADIC '{topic}'::text[]) ~&gt;=~ '?'::text) AND (jsonb_extract_path_text(context, VARIADIC '{topic}'::text[]) ~&lt;~ '?'::text))\n               -&gt;  Bitmap Index Scan on index_messages_on_context  (cost=0.00..515.65 rows=29820 width=0)\n                     Index Cond: (context @&gt; '{&quot;involved_parties&quot;: [{&quot;id&quot;: 1, &quot;type&quot;: 1}]}'::jsonb)\n(\u8fd9\u4e2a\u67e5\u8be2\u8ba1\u5212\u6765\u81eaEXPLAIN\uff0c\u7531\u4e8eEXPLAIN ANALYZE\u8d85\u65f6)<\/code><\/pre>\n<p>\u770b\u4e0b\u4e0a\u9762\u8bed\u53e5\u5feb\u7684\u65f6\u5019QUERY PLAN\uff1a<\/p>\n<pre><code class=\"sql\">QUERY PLAN\n------------------------------------------------------------------------------\n Sort  (cost=667.75..667.76 rows=3 width=911) (actual time=0.093..0.094 rows=7 loops=1)\n   Sort Key: created_at\n   Sort Method: quicksort  Memory: 35kB\n   -&gt;  Bitmap Heap Scan on messages  (cost=14.93..667.73 rows=3 width=911) (actual time=0.054..0.077 rows=7 loops=1)\n         Recheck Cond: ((jsonb_extract_path_text(context, VARIADIC '{topic}'::text[]) = ANY ('{?,?}'::text[])) OR (jsonb_extract_path_text(context, VARIADIC '{topic}'::text[]) ~~ '?%'::text))\n         Filter: ((deleted_at IS NULL) AND (context @&gt; '{&quot;involved_parties&quot;: [{&quot;id&quot;: 1, &quot;type&quot;: 1}]}'::jsonb) AND ((namespace)::text = '?'::text) AND ((jsonb_extract_path_text(context, VARIADIC '{topic}'::text[]) = ANY ('{?,?}'::text[])) OR (jsonb_extract_path_text(context, VARIADIC '{topic}'::text[]) ~~ '?%'::text)))\n         Heap Blocks: exact=7\n         -&gt;  BitmapOr  (cost=14.93..14.93 rows=163 width=0) (actual time=0.037..0.037 rows=0 loops=1)\n               -&gt;  Bitmap Index Scan on index_messages_on_topic_key_string  (cost=0.00..10.36 rows=163 width=0) (actual time=0.029..0.029 rows=4 loops=1)\n                     Index Cond: (jsonb_extract_path_text(context, VARIADIC '{topic}'::text[]) = ANY ('{?,?}'::text[]))\n               -&gt;  Bitmap Index Scan on index_messages_on_topic_key_string  (cost=0.00..4.57 rows=1 width=0) (actual time=0.007..0.007 rows=7 loops=1)\n                     Index Cond: ((jsonb_extract_path_text(context, VARIADIC '{topic}'::text[]) ~&gt;=~ '?'::text) AND (jsonb_extract_path_text(context, VARIADIC '{topic}'::text[]) ~&lt;~ '?'::text))\n Planning time: 0.313 ms\n Execution time: 0.138 ms<\/code><\/pre>\n<p>\u56fe\u4e2d\u6267\u884c\u8ba1\u5212\u53ef\u4ee5\u770b\u51fa\uff0c(context @&gt; &#8216;{&#8220;involved_parties&#8221;: [{&#8220;id&#8221;: 1, &#8220;type&#8221;: 1}]}&#8217;::jsonb) \u6ca1\u8d70\u7d22\u5f15\u66f4\u5feb\uff0c\u8d70\u4e86\u7d22\u5f15\u8fd8\u66f4\u6162\uff1b \u800c\u4e14\u540c\u4e00\u4e2aSQL\u67e5\u8be2\uff0c\u6709\u4e9b\u65f6\u5019\u8d70\u7d22\u5f15\uff0c\u6709\u4e9b\u65f6\u5019\u53c8\u4e0d\u8d70\uff1b<br \/><a name=\"q7Kys\"><\/a><\/p>\n<h2>\u6848\u4f8b\u5206\u6790<\/h2>\n<p>\u8fd9\u91cc\u9488\u5bf9\u6848\u4f8b\u4e8c\u8fdb\u884c\u5206\u6790: <br \/><strong>\u4e86\u89e3\u4e0b\u7d22\u5f15\u533a\u522b<\/strong><\/p>\n<blockquote><p>GIN\u662fPostgreSQL\u63d0\u4f9b\u7684\u4e00\u6b3e\u7528\u4e8e\u590d\u6742\u503c\u7684\u7d22\u5f15\u5f15\u64ce\uff0c\u4e00\u822c\u7528\u4e8e\u6570\u7ec4\u3001JSON\u6216\u6587\u672c\u7b49\u7684\u6570\u636e\u7ed3\u6784\u3002GIN\u7684\u8bbe\u8ba1\u7528\u9014\u662f\u7d22\u5f15\u90a3\u4e9b\u53ef\u5bf9\u5185\u90e8\u7ed3\u6784\u505a\u7ec6\u5206\u7684\u6570\u636e\uff0c\u8fd9\u6837\u5c31\u53ef\u4ee5\u67e5\u627e\u6570\u636e\u5185\u90e8\u7684\u5b50\u6570\u636e\u4e86\u3002BTREE\u662fPostgreSQL\u7684\u9ed8\u8ba4\u7d22\u5f15\u5f15\u64ce\uff0c\u80fd\u5bf9\u7b80\u5355\u503c\u505a\u76f8\u7b49\u6027\u6bd4\u8f83\u6216\u8303\u56f4\u67e5\u8be2\u3002\u8868\u8fbe\u5f0f\u7d22\u5f15\u662fPostgreSQL\u63d0\u4f9b\u7684\u4e00\u79cd\u5f3a\u529b\u7684\u7d22\u5f15\u7c7b\u578b\uff0c\u80fd\u5bf9\u4e00\u4e2a\u8868\u8fbe\u5f0f\uff08\u800c\u4e0d\u662f\u4e00\u4e2a\u5217\uff09\u505a\u7d22\u5f15\u3002JSONB\u7c7b\u578b\u4e00\u822c\u53ea\u80fd\u7528GIN\u8fd9\u6837\u7684\u7d22\u5f15\u5f15\u64ce\uff0c\u56e0\u4e3aBTREE\u53ea\u652f\u6301\u6807\u91cf\u7c7b\u578b\uff08\u53ef\u4ee5\u7406\u89e3\u4e3a\u201c\u6ca1\u6709\u5185\u90e8\u7ed3\u6784\u7684\u7b80\u5355\u503c\u7c7b\u578b\u201d\uff09\u3002\u56e0\u6b64\uff0ccontext\u5217\u4e0a\u7684jsonb_extract_path_text(context, \u2018topic\u2019)\u8868\u8fbe\u5f0f\u53ef\u4ee5\u7528BTREE\u7d22\u5f15\uff0c\u56e0\u4e3a\u5b83\u8fd4\u56de\u5b57\u7b26\u4e32\u7c7b\u578b\u3002\u4e0d\u540c\u4e8eBTREE\u7d22\u5f15\u7edf\u4e00\u800c\u4e00\u81f4\u7684\u8868\u793a\u683c\u5f0f\uff0cGIN\u7d22\u5f15\u7684\u5185\u5bb9\u53ef\u4ee5\u56e0\u6240\u7528\u6570\u636e\u7c7b\u578b\u548c\u64cd\u4f5c\u7b26\u7c7b\u578b\u7684\u4e0d\u540c\u800c\u6781\u4e3a\u4e0d\u540c\u3002\u800c\u4e14\u8003\u8651\u5230\u67e5\u8be2\u53c2\u6570\u7684\u9009\u62e9\u5ea6\u6709\u8f83\u9ad8\u7684\u591a\u6837\u6027\uff0cGIN\u7d22\u5f15\u66f4\u9002\u7528\u4e8e\u4e00\u4e9b\u7279\u5b9a\u7684\u67e5\u8be2\uff0c\u4e0d\u50cfBTREE\u7d22\u5f15\u5e7f\u6cdb\u9002\u7528\u4e8e\u76f8\u7b49\u6027\u6bd4\u8f83\u548c\u8303\u56f4\u67e5\u8be2\u3002<\/p><\/blockquote>\n<p><strong>\u9884\u5206\u6790<\/strong><br \/>\u4e00\u4e2a\u67e5\u8be2\u901a\u5e38\u4f1a\u5148\u505a\u7d22\u5f15\u626b\u63cf\u4ee5\u521d\u7b5b\uff0c\u518d\u5bf9\u7b5b\u9009\u540e\u7684\u8303\u56f4\u505a\u8868\u626b\u63cf\uff08\u4e00\u4e2a\u7279\u4f8b\u662f\uff0c\u5f53\u7d22\u5f15\u626b\u63cf\u8db3\u4ee5\u8986\u76d6\u6240\u9700\u7684\u6240\u6709\u6570\u636e\u5217\u65f6\uff0c\u5219\u65e0\u9700\u8868\u626b\u63cf\uff09\u3002\u4e3a\u4e86\u6700\u5927\u5316\u6027\u80fd\uff0c\u7d22\u5f15\u8981\u6709\u8f83\u597d\u7684\u9009\u62e9\u5ea6\u6765\u7f29\u5c0f\u8303\u56f4\uff0c\u4ee5\u51cf\u5c11\u751a\u81f3\u907f\u514d\u4e4b\u540e\u7684\u8868\u626b\u63cf\u3002\u6761\u4ef6context @&gt; \u2018{\u201cinvolved_parties\u201d:[{\u201cid\u201d:1,\u201dtype\u201d:1}]}\u2019::jsonb\u80fd\u4f7f\u7528context\u5217\u4e0a\u7684GIN\u7d22\u5f15\uff0c\u4f46\u662f\u8fd9\u5e76\u4e0d\u662f\u4e00\u4e2a\u597d\u9009\u62e9\uff0c\u56e0\u4e3a{\u201cid\u201d:1,\u201dtype\u201d:1}\u8fd9\u4e2a\u503c\u662f\u5b58\u5728\u4e8e\u5927\u591a\u6570\u884c\u4e2d\u7684\u4e00\u4e2a\u7279\u6b8a\u503c\uff08\u8fd9\u6570\u5b57\u5c31\u5f88\u7279\u6b8a\uff0c\u50cf\u7ba1\u7406\u5458\u7684\u53f7\u7801\uff09\u3002\u56e0\u6b64\uff0cGIN\u7d22\u5f15\u5bf9\u4e8e\u8fd9\u4e2a\u6761\u4ef6\u7684\u9009\u62e9\u5ea6\u5f88\u5dee\u3002\u5b9e\u9645\u4e0a\uff0c\u8fd9\u4e2a\u67e5\u8be2\u4e2d\u7684\u5176\u4ed6\u6761\u4ef6\u5df2\u80fd\u63d0\u4f9b\u5f88\u597d\u7684\u9009\u62e9\u5ea6\uff0c\u6240\u4ee5\u6c38\u8fdc\u4e0d\u9700\u8981\u4e3a\u8fd9\u4e2a\u6761\u4ef6\u4f7f\u7528\u7d22\u5f15\u3002<br \/><strong>\u9488\u5bf9\u5feb\u6162\u67e5\u8be2\u5206\u6790<\/strong><br \/>                             \u6162\u67e5\u8be2\u8def\u5f84                               \u5feb\u67e5\u8be2\u8def\u5f84<br \/><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043146829\" alt=\"image.png\" title=\"image.png\" loading=\"lazy\"><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/remote\/1460000043146830\" alt=\"image.png\" title=\"image.png\" loading=\"lazy\"><\/p>\n<p>\u5982\u56fe\u53ef\u89c1\uff0c\u8fd9\u4e2a\u6162\u67e5\u8be2\u8ba1\u5212\u6bd4\u5feb\u67e5\u8be2\u8ba1\u5212\u66f4\u590d\u6742\u3002\u5b83\u591a\u4e86\u4e00\u4e2a\u201dBitmapAnd\u201d\u548c\u4e00\u4e2a\u626b\u63cfindex 3\u7684\u201dBitmap Index Scan\u201d\u8282\u70b9\uff08index 3\u662fcontext\u5217\u4e0a\u7684GIN\u7d22\u5f15\uff09\u3002\u82e5index 3\u4f4e\u6548\u7387\uff0c\u603b\u4f53\u6027\u80fd\u5c31\u4f1a\u964d\u4f4e\u3002<br \/>\u5f53\u6210\u672c\u4f30\u8ba1\u51c6\u786e\u65f6\uff0c\u67e5\u8be2\u8ba1\u5212\u5668\u5de5\u4f5c\u5f97\u5f88\u597d\u3002\u4f46\u662fJSONB\u4e0a\u7684GIN\u7d22\u5f15\u7684\u6210\u672c\u4f30\u8ba1\u4e0d\u662f\u5f88\u51c6\u786e\u7684\u3002\u7531\u89c2\u6d4b\u53ef\u89c1\uff0c\u5b83\u8ba4\u4e3a\u8fd9\u4e2a\u7d22\u5f15\u7684\u9009\u62e9\u5ea6\u4e3a0.001\uff08\u8fd9\u662f\u4e00\u4e2a\u786c\u7f16\u7801\u7684\u56fa\u5b9a\u503c\uff09\uff0c\u4e5f\u5c31\u662f\u8bf4\u5b83\u5047\u8bbe\u4efb\u4f55\u76f8\u5173\u7684\u67e5\u8be2\u90fd\u4f1a\u9009\u62e9\u8868\u4e2d\u6240\u6709\u884c\u76840.1%\uff0c\u4f46\u5728\u6211\u4eec\u8fd9\u4e2a\u573a\u666f\u5b83\u5b9e\u9645\u4f1a\u9009\u62e990%\u7684\u884c\uff0c\u6240\u4ee5\u8fd9\u4e2a\u5047\u8bbe\u4e0d\u6210\u7acb\u3002\u9519\u8bef\u7684\u5047\u8bbe\u4f7f\u67e5\u8be2\u8ba1\u5212\u5668\u4f4e\u4f30\u4e86\u6162\u67e5\u8be2\u8ba1\u5212\u7684\u6210\u672c\u3002\u867d\u7136JSONB\u7c7b\u578b\u7684\u5217\u4e5f\u6709\u4e00\u4e9b\u7edf\u8ba1\u4fe1\u606f\uff0c\u4f46\u597d\u50cf\u6ca1\u6709\u8d77\u5230\u4f5c\u7528\u3002<br \/><a name=\"gSHSK\"><\/a><\/p>\n<h2>\u7ed3\u8bba<\/h2>\n<p>\u6240\u4ee5\u8bf4\u6709\u4e9b\u65f6\u547d\u4e2d\u7d22\u5f15\u4e0d\u4e00\u5b9a\u5c31\u5feb\uff0c\u800c\u4e14\u7d22\u5f15\u4f18\u5316\u5668\u4e5f\u4e0d\u4e00\u5b9a\u662f\u51c6\u786e\u7684\uff0c\u53ef\u80fd\u4f1a\u6267\u884c\u66f4\u6162\uff1b<br \/><a name=\"btFYL\"><\/a><\/p>\n<h2>\u4e00\u4e9b\u6709\u7528\u7684\u539f\u5219<\/h2>\n<p><a name=\"cnECv\"><\/a><\/p>\n<h3>\u539f\u52191: \u5c11\u5373\u662f\u591a<\/h3>\n<p><strong>\u7ba1\u7406\u597d\u7d22\u5f15<\/strong><br \/>\u66f4\u591a\u7684\u7d22\u5f15\u5e76\u4e0d\u610f\u5473\u7740\u66f4\u597d\u7684\u6027\u80fd\u3002\u4e8b\u5b9e\u4e0a\uff0c\u6bcf\u589e\u52a0\u4e00\u4e2a\u7d22\u5f15\u90fd\u4f1a\u964d\u4f4e\u5199\u64cd\u4f5c\u7684\u6027\u80fd\u3002\u5982\u679c\u67e5\u8be2\u8ba1\u5212\u5668\u9009\u62e9\u4e86\u4e0d\u9ad8\u6548\u7684\u7d22\u5f15\uff0c\u90a3\u4e48\u67e5\u8be2\u4ecd\u7136\u4f1a\u5f88\u6162\u3002<br \/>\u4e0d\u8981\u5806\u79ef\u7d22\u5f15\uff08\u4f8b\u5982\u6bcf\u4e00\u5217\u90fd\u5efa\u7d22\u5f15\u5c31\u662f\u4e0d\u53ef\u53d6\u7684\uff09\u3002\u8bd5\u7740\u5c3d\u53ef\u80fd\u5220\u9664\u4e00\u4e9b\u7d22\u5f15\u5427\u3002\u800c\u4e14\u6bcf\u6539\u52a8\u4e00\u4e2a\u7d22\u5f15\u90fd\u8981\u76d1\u63a7\u5176\u5bf9\u6027\u80fd\u7684\u5f71\u54cd\u3002<br \/><strong>\u4f18\u9009\u7b80\u5355\u7684\u6570\u636e\u5e93\u8bbe\u8ba1<\/strong><br \/>RDBMS\uff08\u5173\u7cfb\u578b\u6570\u636e\u5e93\u7cfb\u7edf\uff09\u4e2d\u7684\u6570\u636e\u4e00\u822c\u90fd\u5b9c\u7528\u8303\u5f0f\u5316\u8bbe\u8ba1\u3002JSON\u6216JSONB\u5219\u662fNoSQL\u98ce\u683c\u7684\u53cd\u8303\u5f0f\u5316\u8bbe\u8ba1\u3002<br \/>\u8303\u5f0f\u5316\u548c\u53cd\u8303\u5f0f\u5316\u54ea\u4e2a\u66f4\u597d\u5462\uff1f\u4ece\u4e1a\u52a1\u7684\u89d2\u5ea6\uff0c\u8981\u5177\u4f53\u60c5\u51b5\u5177\u4f53\u5206\u6790\u3002\u4eceRDBMS\u7684\u89d2\u5ea6\uff0c\u8303\u5f0f\u5316\u603b\u662f\u66f4\u7b80\u5355\u66f4\u597d\uff0c\u800c\u53cd\u8303\u5f0f\u5316\u5219\u53ef\u4ee5\u5728\u67d0\u4e9b\u60c5\u51b5\u4f5c\u4e3a\u8865\u5145\u3002<br \/>\u5efa\u8bae1\uff1a\u8003\u8651\u4eceDDD\uff08\u9886\u57df\u9a71\u52a8\u8bbe\u8ba1\uff09\u7684\u89d2\u5ea6\u6765\u8bbe\u8ba1\u6570\u636e\u6a21\u578b\u3002<\/p>\n<ul>\n<li>\u5b9e\u4f53\u603b\u662f\u53ef\u4ee5\u5efa\u6a21\u4e3a\u8868\uff0c\u503c\u5bf9\u8c61\u603b\u662f\u53ef\u4ee5\u5d4c\u5165\u4fdd\u5b58\u5728\u5b9e\u4f53\u4e2d\uff08\u800c\u6709\u65f6\u4e3a\u4e86\u6027\u80fd\uff0c\u5927\u578b\u503c\u5bf9\u8c61\u4e5f\u53ef\u4ee5\u5efa\u6a21\u4e3a\u8868\uff09\u3002<\/li>\n<li>\u67d0\u4e2a\u5173\u8054\u7684\u76ee\u6807\u5b9e\u4f53\u82e5\u4e3a\u805a\u5408\u6839\uff0c\u5c31\u4e00\u5b9a\u4e0d\u80fd\u5d4c\u5165\u4fdd\u5b58\u5728\u522b\u5904\uff08\u800c\u8981\u81ea\u6210\u4e00\u8868\uff09\u3002\u4f46\u5982\u679c\u5173\u8054\u7684\u76ee\u6807\u5b9e\u4f53\u4e0d\u662f\u805a\u5408\u6839\uff0c\u5e76\u4e14\u5173\u8054\u7684\u6e90\u5b9e\u4f53\u662f\u81ea\u5305\u542b\u7684\u805a\u5408\u6839\uff0c\u90a3\u4e48\u76ee\u6807\u5b9e\u4f53\u5c31\u53ef\u4ee5\u88ab\u5d4c\u5165\u4fdd\u5b58\u3002<\/li>\n<\/ul>\n<p>\u5efa\u8bae2: \u73b0\u4ee3RDBMS\u4e2d\u7684\u53ef\u7a7a\u5217(nullable column)\u5f88\u9ad8\u6548\uff0c\u4e0d\u7528\u8fc7\u4e8e\u62c5\u5fc3\u6027\u80fd\uff0c\u5982\u679c\u591a\u4e2a\u53ef\u7a7a\u5217\u662f\u5bf9\u4e8e\u53ef\u9009\u5c5e\u6027(optional attribute)\u6700\u7b80\u660e\u7684\u5efa\u6a21\u65b9\u5f0f\uff0c\u5c31\u4e0d\u8981\u72b9\u8c6b\u4e86\uff0c\u66f4\u522b\u628aJSONB\u5f53\u4f5c\u5bf9\u53ef\u7a7a\u5217\u7684\u201c\u4f18\u5316\u201d\u65b9\u5f0f\u3002<br \/><a name=\"TCQn3\"><\/a><\/p>\n<h3>\u539f\u52192: \u7edf\u8ba1\u4fe1\u606f\u8981\u51c6\u786e<\/h3>\n<p>PostgreSQL\u7ef4\u62a4\u6bcf\u4e00\u5f20\u8868\u7684\u7edf\u8ba1\u4fe1\u606f\uff0c\u5305\u62ec\u800c\u4e0d\u9650\u4e8e\u5143\u7ec4\u6570(tuple number)\uff0c\u9875\u6570(page number)\uff0c\u6700\u5e38\u89c1\u7684\u503c(most common values)\uff0c\u67f1\u72b6\u56fe\u754c\u9650(histogram bounds)\u548c\u4e0d\u540c\u503c\u7684\u4e2a\u6570(number of distinct values, \u53ef\u80fd\u76f8\u5f53\u4e8e\u96c6\u7684\u57fa\u6570set cardinality)\u3002\u6709\u4e00\u4e9b\u7edf\u8ba1\u4fe1\u606f\u662f\u91c7\u6837\u5f97\u5230\u7684\u800c\u4e0d\u591f\u51c6\u786e\u3002\u67e5\u8be2\u8ba1\u5212\u5668\u4f1a\u5bf9\u67e5\u8be2\u751f\u6210\u591a\u4e2a\u53ef\u80fd\u7684\u8ba1\u5212\uff0c\u6839\u636e\u7edf\u8ba1\u4fe1\u606f\u548c\u89c4\u5219\u6765\u4f30\u8ba1\u6210\u672c\uff0c\u518d\u9009\u62e9\u6700\u9ad8\u6548\u7684\u90a3\u4e2a\u8ba1\u5212\u3002\u67e5\u8be2\u8ba1\u5212\u7684\u8d28\u91cf\u53d6\u51b3\u4e8e\u7edf\u8ba1\u6570\u636e\u7684\u51c6\u786e\u6027\u3002\u51c6\u786e\u7684\u6570\u636e\u5e26\u6765\u4f18\u79c0\u7684\u6267\u884c\uff08\u8fd9\u4e5f\u662f\u6570\u636e\u79d1\u5b66\u548c\u6570\u636e\u9a71\u52a8\u4e1a\u52a1\u7684\u4e00\u4e2a\u597d\u539f\u5219\uff09\u3002<br \/>\u6b63\u5982\u6240\u63d0\u5230\u7684\uff0cJSONB\u4e0a\u7684GIN\u7684\u6210\u672c\u4f30\u8ba1\u4e0d\u662f\u5f88\u51c6\u786e\u7684\u3002\u800c\u6807\u91cf\u7c7b\u578b\u4e0a\u7684BTREE\u7684\u6210\u672c\u4f30\u8ba1\u5219\u51c6\u786e\u5f97\u591a\uff0c\u4f46\u4e0d\u662f\u5b8c\u5168\u51c6\u5907\u3002\u56e0\u6b64JSONB\u4e0d\u9002\u5408\u67d0\u4e9b\u60c5\u51b5\u3002\u4e3a\u4e86\u8ffd\u6c42\u6548\u7387\uff0c\u4f5c\u4e3a\u53d8\u901a\u65b9\u6cd5\uff0c\u53ef\u4ee5\u5bf9JSONB\u7684\u67d0\u4e2a\u6807\u91cf\u7c7b\u578b\u5c5e\u6027\u5efa\u4e00\u4e2aBTREE\u8868\u8fbe\u5f0f\u7d22\u5f15\u3002\u6765\u81eaScaleGrid\u7684\u8fd9\u7247\u6587\u7ae0\u5f88\u597d\u5730\u4ecb\u7ecd\u4e86\u600e\u6837\u9ad8\u6548\u4f7f\u7528JSONB\u548cGIN\u3002<br \/>\u5efa\u8bae\uff1aPostgreSQL\u6709\u4e00\u4e9b\u7279\u6027\uff0c\u5982\u8868\u8fbe\u5f0f\u7d22\u5f15\u548c\u90e8\u5206\u7d22\u5f15\u90fd\u662f\u5f3a\u5927\u800c\u6709\u6210\u672c\u6548\u76ca\u7684\u3002\u53ea\u8981\u57fa\u4e8e\u6570\u636e\u5206\u6790\u8ba4\u4e3a\u6709\u6548\u76ca\uff0c\u90fd\u503c\u5f97\u9009\u7528\u4e4b\u3002<br \/><a name=\"lh90p\"><\/a><\/p>\n<h3>\u539f\u52193: \u63d0\u9ad8\u53ef\u89c2\u5bdf\u6027<\/h3>\n<p>\u65e0\u8bba\u6211\u4eec\u662f\u5426\u5bf9\u95ee\u9898\u7684\u6f5c\u5728\u6839\u56e0\u6709\u63a8\u6d4b\uff0c\u63d0\u9ad8\u53ef\u89c2\u5bdf\u6027\u90fd\u662f\u6700\u597d\u7684\u505a\u6cd5\u3002\u67e5\u8be2\u65e5\u5fd7\u80fd\u8bc1\u660e\u5bfc\u81f4\u6162\u8bf7\u6c42\u7684\u662f\u6162\u67e5\u8be2\uff0c\u800c\u4e0d\u662f\u5e94\u7528\u7a0b\u5e8f\u4ee3\u7801\u6216\u8fde\u63a5\u7b49\u5f85\u3002\u81ea\u52a8EXPLAIN\u80fd\u6355\u83b7\u6162\u67e5\u8be2\u6240\u7528\u7684\u771f\u5b9e\u7684\u67e5\u8be2\u8ba1\u5212\u3002<br \/>\u50cfDatadog\u8fd9\u6837\u7684APM\uff08\u5e94\u7528\u7a0b\u5e8f\u6027\u80fd\u7ba1\u7406\uff09\u4e5f\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u5de5\u5177\u3002\u5b83\u80fd\u63d0\u4f9b\u5f88\u591a\u6d1e\u5bdf\uff1a<\/p>\n<ol>\n<li>\u8fd9\u4e2a\u95ee\u9898\u662f\u7531\u8d44\u6e90\u4e0d\u8db3\u6240\u81f4\u5417\uff1f\u4e0d\uff0c\u8d44\u6e90\u4e0d\u8db3\u5e94\u5e73\u7b49\u5f71\u54cd\u4efb\u4f55SQL CRUD\u8bed\u53e5\uff0c\u4f46\u6211\u4eec\u53ea\u89c2\u5bdf\u5230\u6162\u7684SELECT\u3002<\/li>\n<li>\u8fd9\u4e2a\u95ee\u9898\u53d1\u751f\u4e8e\u6bcf\u5929\u7684\u540c\u4e00\u65f6\u95f4\u5417\uff1f\u4e0d\uff0c\u5b83\u80fd\u53d1\u751f\u4e8e\u4efb\u4f55\u65f6\u95f4\u3002<\/li>\n<li>\n<p>\u6bcf\u4e00\u6b21\u53d1\u751f\u662f\u72ec\u7acb\u4e8b\u4ef6\u5417\uff1f\u4e0d\uff0c\u4f1a\u5728\u67d0\u4e2a\u5c0f\u7684\u65f6\u95f4\u7a97\u805a\u96c6\u53d1\u751f\u591a\u4e2a\u4e8b\u4ef6\u3002\u90a3\u65f6\u4e00\u5b9a\u662f\u53d1\u751f\u4e86\u4ec0\u4e48\u4e8b\u624d\u5bfc\u81f4\u8fd9\u4e2a\u95ee\u9898\u3002<br \/><a name=\"CgSut\"><\/a><\/p>\n<h2>\u4e00\u4e9b\u4f18\u5316\u7684\u63aa\u65bd<\/h2>\n<p>\u9488\u5bf9\u5b9c\u642d\u672c\u8eab\uff0c\u4e00\u4e9b\u53ef\u80fd\u6709\u53c2\u8003\u4ef7\u503c\u7684\u4f18\u5316\u63aa\u65bd\uff0c\u5f53\u7136\u8fd9\u91cc\u4e0d\u6d89\u53ca\u7f13\u5b58\uff0c\u4e3b\u8981\u8bb2\u5b58\u50a8\u5c42\u9762\u7684<\/p>\n<blockquote><p>\u5b9c\u642d\u4f5c\u4e3a\u9489\u9489\u4e0a\u4f4e\u4ee3\u7801\u5f00\u53d1\u5e73\u53f0\uff0c\u4e0a\u9762\u627f\u8f7d\u7740\u4e0a\u767e\u4e07\u5e94\u7528\uff0c\u5b9e\u4f8b\u6570\u636e\u603b\u5171\u8fbe\u5230\u51e0\u5341\u4ebf\u89c4\u6a21\uff1b\u4e0d\u540c\u7684\u5e94\u7528\u4f1a\u751f\u957f\u51fa\u4e0d\u540c\u7684\u573a\u666f\u4e1a\u52a1\uff0c\u4e0d\u540c\u7684\u573a\u666f\u4e1a\u52a1\u4e5f\u4f1a\u884d\u751f\u51fa\u5f88\u591a\u4e1a\u52a1\u7ec4\u4ef6\uff0c\u6bd4\u5982\u5355\u884c\u6587\u672c\u7ec4\u4ef6\uff0c\u6210\u672c\u7ec4\u4ef6\u7b49\uff1b \u8fd9\u4e9b\u7ec4\u4ef6\u5bf9\u5e94\u5b58\u50a8\u4f1a\u6709\u5f88\u591a\u4e0d\u540c\u7684\u7d22\u5f15\u53bb\u52a0\u901f\u67e5\u8be2\uff1b<\/p><\/blockquote>\n<\/li>\n<\/ol>\n<p><strong>\u8fd9\u91cc\u4f18\u5316\u63aa\u65bd\u5927\u4f53\u67093\u7c7b<\/strong><br \/><a name=\"hth7d\"><\/a><\/p>\n<h5>\u8c03\u6574SQL\u8bed\u53e5\uff0c\u4f7f\u5f97\u4e4b\u524d\u7279\u5b9a\u7ec4\u4ef6\u6ca1\u8d70\u7d22\u5f15\uff0c\u5145\u5206\u8d70\u7d22\u5f15\uff1b\u6bd4\u5982\u4e0b\u9762gin\u8bed\u53e5\u7684\u67e5\u8be2<\/h5>\n<p>\u9488\u5bf9\u4e00\u4e9b\u7279\u5b9a\u7ec4\u4ef6\u7684\u67e5\u8be2\uff0c\u4f18\u5316\u4e86\u67e5\u8be2\u8bed\u53e5\uff0c\u4f7f\u5f97\u5145\u5206\u5229\u7528\u7d22\u5f15\uff0c\u5728\u6570\u636e\u91cf\u5927\u65f6\u5019\u67e5\u8be2\u66f4\u5feb\uff1b<br \/>\u6b63\u5982\u4e0a\u9762\u6848\u4f8b\u8bf4\u7684\uff0c\u6709\u4e86\u7d22\u5f15\u4e0d\u4e00\u5b9a\u5feb\uff0c\u6ca1\u7d22\u5f15\u53ef\u80fd\u66f4\u5feb\uff0c\u6240\u4ee5\u9700\u8981\u6839\u636e\u67e5\u8be2\u573a\u666f\u63a7\u5236\u5e76\u5224\u65ad\uff1b<br \/>\u4f18\u5316\u524d<\/p>\n<pre><code class=\"sql\">                           QUERY PLAN\n\n---------------------------------------------------------------------------------------------------------------------------------------------------\n---------------------------------------------------------------------------------------------------------------------------------------------------\n----------------------------------------------------------------\n Limit  (cost=4085.57..4086.38 rows=36 width=51) (actual time=100.118..100.121 rows=1 loops=1)\n   -&gt;  Gather Motion 128:1  (slice1; segments: 128)  (cost=4085.57..4086.38 rows=36 width=51) (actual time=100.111..100.111 rows=1 loops=1)\n         Merge Key: gmt_create, pid\n         -&gt;  Limit  (cost=4085.57..4085.66 rows=1 width=51) (actual time=52.273..52.276 rows=1 loops=1)\n               -&gt;  Sort  (cost=4085.57..4085.66 rows=1 width=51) (actual time=52.271..52.273 rows=1 loops=1)\n                     Sort Key: gmt_create, pid\n                     Sort Method:  top-N heapsort  Memory: 4224kB\n                     -&gt;  Index Scan Backward using idx_app_type_table_name_gmt_create on yida_entity_instance a  (cost=0.20..4084.66 rows=1 width=5\n1) (actual time=0.361..52.258 rows=1 loops=1)\n                           Index Cond: (((app_type)::text = 'APP_GXUUGZJ1ZPPBIJKLE9BH'::text) AND ((model_uuid)::text = 'FORM-EX866CB1E6TV7F6SZME2Y\nQA5IKWO1052BFOWKK'::text))\n                           Filter: (is_deleted = 'n'::bpchar) AND (json_data -&gt; 'employeeField_kw0b5hyf_code'::text) = '[&quot;050323&quot;]'::jsonb) \n Planning time: 0.254 ms\n   (slice0)    Executor memory: 180K bytes.\n   (slice1)    Executor memory: 188K bytes avg x 128 workers, 188K bytes max (seg0).  Work_mem: 33K bytes max.\n Memory used:  2047000kB\n Optimizer: Postgres query optimizer\n Execution time: 101.799 ms\n(16 rows)<\/code><\/pre>\n<p>\u4f18\u5316\u540e<\/p>\n<pre><code class=\"sql\">                                                                                                                   QUERY PLAN\n---------------------------------------------------------------------------------------------------------------------------------------------------\n---------------------------------------------------------------------------------------------------------------------------------------------------\n---------\n Limit  (cost=5.55..6.36 rows=36 width=51) (actual time=28.342..28.344 rows=1 loops=1)\n   -&gt;  Gather Motion 128:1  (slice1; segments: 128)  (cost=5.55..6.36 rows=36 width=51) (actual time=28.339..28.339 rows=1 loops=1)\n         Merge Key: gmt_create, pid\n         -&gt;  Limit  (cost=5.55..5.64 rows=1 width=51) (actual time=5.173..5.175 rows=1 loops=1)\n               -&gt;  Sort  (cost=5.55..5.64 rows=1 width=51) (actual time=5.171..5.173 rows=1 loops=1)\n                     Sort Key: gmt_create, pid\n                     Sort Method:  top-N heapsort  Memory: 4224kB\n                     -&gt;  Bitmap Heap Scan on yida_entity_instance a  (cost=4.51..4.64 rows=1 width=51) (actual time=5.156..5.159 rows=1 loops=1)\n                           Recheck Cond: (json_data @&gt; '{&quot;employeeField_kw0b5hyf_code&quot;: [&quot;050323&quot;]}'::jsonb)\n                           Filter: ((is_deleted = 'n'::bpchar) AND ((app_type)::text = 'APP_GXUUGZJ1ZPPBIJKLE9BH'::text) AND ((model_uuid)::text =\n'FORM-EX866CB1E6TV7F6SZME2YQA5IKWO1052BFOWKK'::text))\n                           -&gt;  Bitmap Index Scan on idx_json_data_path  (cost=0.00..4.50 rows=1 width=0) (actual time=5.129..5.129 rows=4 loops=1)\n                                 Index Cond: (json_data @&gt; '{&quot;employeeField_kw0b5hyf_code&quot;: [&quot;050323&quot;]}'::jsonb)\n Planning time: 0.345 ms\n   (slice0)    Executor memory: 157K bytes.\n   (slice1)    Executor memory: 495K bytes avg x 128 workers, 576K bytes max (seg31).  Work_mem: 33K bytes max.\n Memory used:  2047000kB\n Optimizer: Postgres query optimizer\n Execution time: 30.130 ms\n(18 rows)<\/code><\/pre>\n<p>\u8fd9\u91cc\u4e3b\u8981\u8c03\u6574\u4e86\u67e5\u8be2SQL<br \/>\u628aa.json_data-&gt;&#8217;employeeField_kw0b5hyf_code&#8217;='[&#8220;050323&#8221;]&#8217;  \u6362\u6210json_data @&gt; &#8216;{&#8220;employeeField_kw0b5hyf_code&#8221;: [&#8220;050323&#8221;]}&#8217;::jsonb\u67e5\u8be2\uff0c\u5145\u5206\u5229\u7528gin jsonb_path_ops\u7684\u7d22\u5f15\uff08\u76f8\u6bd4gin jsob_ops\u7d22\u5f15\u66f4\u9ad8\u6548\uff09\uff1b<br \/>\u5f53\u7136\u4e0a\u9762\u5728\u6570\u636e\u91cf\u7a0d\u5fae\u5927\u4e00\u70b9\u6548\u679c\u66f4\u660e\u663e\uff0c\u56e0\u4e3a\u6570\u636e\u91cf\u592a\u5c11\u4f1a\u9ed8\u8ba4\u8d70\u8868\u626b\u63cf\uff0c\u4e0d\u8d70\u7d22\u5f15\u66f4\u5feb\uff1b<\/p>\n<blockquote><p>\u8bf4\u660e \u5728JSONB\u4e0a\u521b\u5efaGIN\u7d22\u5f15\u7684\u65b9\u5f0f\u6709\u4e24\u79cd\uff1a\u4f7f\u7528\u9ed8\u8ba4\u7684jsonb_ops\u64cd\u4f5c\u7b26\u521b\u5efa\u548c\u4f7f\u7528jsonb_path_ops\u64cd\u4f5c\u7b26\u521b\u5efa\u3002\u4e24\u8005\u7684\u533a\u522b\u5728jsonb_ops\u7684GIN\u7d22\u5f15\u4e2d\uff0cJSONB\u6570\u636e\u4e2d\u7684\u6bcf\u4e2akey\u548cvalue\u90fd\u662f\u4f5c\u4e3a\u4e00\u4e2a\u5355\u72ec\u7684\u7d22\u5f15\u9879\u7684\uff0c\u800cjsonb_path_ops\u5219\u53ea\u4e3a\u6bcf\u4e2avalue\u521b\u5efa\u4e00\u4e2a\u7d22\u5f15\u9879\u3002<\/p><\/blockquote>\n<p>\u5927\u5bb6\u8fd8\u8bb0\u5f97\u6700\u4e0a\u9762\u6848\u4f8b\u4e8c\u5417\uff1f \u4e0a\u9762\u8c03\u6574\u540e\u7684SQL\u8bed\u53e5\uff0c\u5982\u679c\u662f\u5728\u6848\u4f8b\u4e8c\u573a\u666f\u5f53\u4e2d\uff0c\u8c03\u6574\u540e\u4f1a\u66f4\u6162\uff0c\u6240\u4ee5\u9700\u8981\u5177\u4f53\u573a\u666f\u5177\u4f53\u5206\u6790\uff1b<br \/><a name=\"zZzyF\"><\/a><\/p>\n<h5>\u9488\u5bf9\u5b58\u50a8\u4f18\u5316\u5668\u7684\u67e5\u8be2\u6548\u7387\u60c5\u51b5\uff0c\u81ea\u884c\u9009\u62e9\u6700\u4f73\u7684\u626b\u63cf\u8ba1\u5212\u65b9\u5f0f<\/h5>\n<p>\u6b63\u5982\u4e0a\u9762\u6848\u4f8b\u8bf4\u7684\uff0c\u4f18\u5316\u5668\u4e5f\u4e0d\u4e00\u5b9a\u662f\u51c6\u786e\u7684\uff0c\u6240\u4ee5\u6709\u4e9b\u65f6\u5019\u9700\u8981\u6211\u4eec\u4ee3\u7801\u4f18\u5316\u5668\u81ea\u884c\u9009\u62e9\u6700\u4f73\u7684\u626b\u63cf\u8ba1\u5212\u65b9\u5f0f<\/p>\n<p>\u6bd4\u5982\u8bf4\uff1a\u6211\u4eec\u67e5\u8be2\u573a\u666f\u4e2d\u4f1a\u6709limit 1\uff0climit 2\uff0climit 5\u7b49\u8fd9\u79cd\u67e5\u8be2\u8bed\u53e5\uff0c\u7528limit\u6765\u786e\u4fdd\u5b83\u5728\u627e\u5230n\u4e2a\u6ee1\u8db3\u6761\u4ef6\u7684\u884c\u65f6\u5c31\u505c\u4e0b\uff0c\u800c\u4e0d\u7528\u626b\u63cf\u6574\u4e2a\u8868\uff1b<br \/>\u5bf9\u4e8e\u4f18\u5316\u5668\u6765\u8bf4\uff0c\u9488\u5bf9limit\u5f88\u5c0f\u7684\u6570\u503c\uff0c\u8ba4\u4e3a\u8868\u626b\u63cf\u53ef\u80fd\u4f1a\u66f4\u5feb\uff0c\u6211\u4eec\u6709\u4e9b\u5e94\u7528\u6570\u636e\u91cf\u5f88\u5927\uff0c\u8fd9\u79cd\u573a\u666f\u5982\u679c\u8d70\u8868\u626b\u63cf\uff0c\u4e0d\u8d70\u7d22\u5f15\uff0c\u6548\u7387\u4f1a\u66f4\u6162\uff08\u53d6\u51b3\u4e8e\u626b\u63cf\u884c\u6570\u6765\u547d\u4e2d\uff09\uff1b\u8fd9\u79cd\u60c5\u51b5\u6211\u4eec\u67e5\u8be2\u7684\u65f6\u5019\uff0c\u4f1a\u9488\u5bf9\u6027force index\u8bf7\u6c42\u53bb\u5f3a\u5236\u8d70\u7d22\u5f15\u626b\u63cf\u8ba1\u5212\uff0c\u63d0\u5347\u901f\u5ea6\uff1b<\/p>\n<p>\u540e\u7eed\u9488\u5bf9\u6bcf\u4e2aSQL\u7684\u4ee3\u4ef7\u6548\u7387\u7edf\u8ba1\uff0c\u4e5f\u53ef\u4ee5\u81ea\u52a8\u9009\u62e9\u5bf9\u5e94\u626b\u63cf\u8ba1\u5212\uff0c\u4e5f\u7b97\u662f\u5bf9\u4f18\u5316\u5668\u9488\u5bf9\u4e1a\u52a1\u573a\u666f\u4e0d\u540c\u4e0b\u4e0d\u540c\u4ee3\u4ef7\u6267\u884c\u7684\u4e00\u4e2a\u8865\u5145\uff1b\u8fd9\u4e2a\u6b65\u9aa4\u53ef\u4ee5\u7406\u89e3\u53eb\u201c\u63a2\u67e5\u6267\u884c\u201d\uff0c\u5728\u5b9c\u642d\u4e13\u5c5e\u5927\u5ba2\u6237\u573a\u666f\u4e0b\uff0c\u540e\u7eed\u4f1a\u57fa\u4e8e\u8fd9\u4e2a\u8fdb\u884c\u201c\u63a2\u67e5\u6267\u884c\u201d\uff0c\u4ee5\u8fbe\u5230\u5927\u6570\u636e\u91cf\u4e0b\u67e5\u8be2\u6548\u7387\u7684\u6700\u4f18\u89e3\uff1b<\/p>\n<p><a name=\"YBMeW\"><\/a><\/p>\n<h5>SQL Parse\u4e1a\u52a1\u4f18\u5316\u5668\uff0c\u524d\u7f6e\u5904\u7406\u4f18\u5316SQL\u67e5\u8be2<\/h5>\n<p>\u8fd9\u91ccSQL Parse\u4e1a\u52a1\u4f18\u5316\u5668\uff0c\u4e3b\u8981\u662f\u9488\u5bf9\u6211\u4eec\u4e1a\u52a1\u4e0a\u4e0d\u5408\u7406\u5f85\u4f18\u5316\u7684SQL\uff0c\u7b97\u662f\u524d\u7f6e\u62e6\u622a\u4f18\u5316\uff0c\u4e0e\u5b58\u50a8\u7684\u8fd8\u662f\u6709\u70b9\u533a\u522b\uff1b\u76ee\u7684\u662f\u4f18\u5316\u5668\u7684\u8865\u5145\uff0c\u8ba9\u5b58\u50a8\u66f4\u52a0\u4e13\u6ce8\u4e8e\u57fa\u4e8e\u4ee3\u4ef7\u548c\u6210\u672c\u7684\u4f18\u5316\uff08CBO\uff0ccost based optimization\uff09\u4e0a\uff0c\u8ba9\u4f18\u5316\u5668\u80fd\u66f4\u591a\u7684\u96c6\u4e2d\u5728\u7406\u89e3\u8ba1\u7b97\u8fdb\u884c\u6267\u884c\u8ba1\u5212\u4f18\u5316\u8fd9\u4ef6\u4e8b\u60c5\u4e0a\u3002<\/p>\n<blockquote><p>\u5b9c\u642d\u672c\u8eab\u6709\u5f88\u591a\u4e1a\u52a1\u529f\u80fd\uff0c\u8fd9\u4e9b\u4e1a\u52a1\u529f\u80fd\u5bf9\u63a5\u5e95\u5c42\u5143\u6570\u636e\u5f15\u64ce\uff0c\u6765\u64cd\u4f5c\u83b7\u53d6\u6570\u636e\uff1b\u4e1a\u52a1\u4e0a\u5177\u4f53\u9009\u62e9AST\u6570\u636e\u64cd\u4f5c\u53c2\u6570\u6765\u8c03\u7528\u5143\u6570\u636e\u5f15\u64ce\u7edf\u4e00API\uff1b<\/p><\/blockquote>\n<p>\u5143\u6570\u636e\u5e95\u5c42\u83b7\u53d6\u5230\u5bf9\u5e94\u7684AST\u53c2\u6570\uff0c\u89e3\u6790\u540e\u7ec4\u88c5SQL\uff0c\u8fd9\u91cc\u9488\u5bf9\u6027\u751f\u6210\u7684\u4e0d\u5408\u7406SQL\u8fdb\u884c\u4f18\u5316<\/p>\n<ul>\n<li>\u6ca1\u6709\u5e94\u7528\u6807\u8bc6\uff0c\u5e95\u5c42\u4f1a\u515c\u5e95\u4e0a\u4e0b\u6587\u53bb\u53d6\uff0c\u5982\u679c\u6ca1\u6709\u7684\u8bdd\uff0c\u4f1a\u629b\u51fa\u4e0d\u5408\u7406\u7684\u5f02\u5e38\uff0c\u62d2\u7edd\u4e0d\u5408\u7406\u7684SQL\u53bb\u67e5\u8be2\uff0c\u51cf\u5c11\u67e5\u8be2\u8303\u56f4\u5230\u5177\u4f53\u5e94\u7528\u5c42\u9762\uff1b<\/li>\n<li>limit\u6ca1\u4f20\u7684\uff0c\u7ec4\u88c5SQL\u4f1a\u9ed8\u8ba4\u7ed9\u4e2a\u503c\uff1b<\/li>\n<li>\u9488\u5bf9select * \u67e5\u8be2\u8bed\u6cd5\uff0c\u9ed8\u8ba4\u89e3\u6790\u6210select\u5b57\u6bb5<\/li>\n<li>\u4ee5\u53ca\u4e00\u4e9b\u51fd\u6570\u6216\u8005\u8868\u8fbe\u5f0f\u7684\u53d8\u6362\uff0c\u6bd4\u5982\u65e5\u671f\u51fd\u6570\u5927\u4e8e\u7b49\u4e8e\u503c\uff0c\u547d\u4e2d\u4e0d\u4e86\u7d22\u5f15\uff0cSQL\u4f18\u5316\uff1b<\/li>\n<li>\u6570\u503c\u4ecb\u4e8e\u67e5\u8be2\uff0c\u660e\u663e\u547d\u4e2d\u4e0d\u4e86\u6570\u636e\u7684\uff0c\u76f4\u63a5\u5224\u65ad\u62e6\u622a\uff1b<\/li>\n<li>\u4e00\u4e9b\u4e0d\u5408\u7406\u591a\u6761\u4ef6\u67e5\u8be2\uff0c\u5408\u5e76<\/li>\n<li>&#8230;<\/li>\n<\/ul>\n<p>\u6700\u8fd1\u56e2\u961f\u6709\u4e00\u4e9bhc\uff0c\u5bf9\u5143\u6570\u636e\u6216\u8005\u4f4e\u4ee3\u7801\u5e73\u53f0\u611f\u5174\u8da3\u7684\u6765\u8bd5\u8bd5\uff1bjava\u7814\u53d1\uff0c\u6570\u636e\u7814\u53d1\uff0c\u6280\u672f\u4e13\u5bb6\u7b49\u90fd\u53ef\uff0c\u6b22\u8fce\u6765\u64a9<br \/>\u7b80\u5386\u53d1 <a href=\"https:\/\/segmentfault.com\/a\/mailto:edagarli.lz@alibaba-inc.com\">edagarli.lz@alibaba-inc.com<\/a> \u6216\u8005\u5fae\u4fe1\u79c1\u6211hangzhoushoot<\/p>\n<p><a name=\"nBcF8\"><\/a><\/p>\n<h2>\u53c2\u8003<\/h2>\n<p><a href=\"https:\/\/link.segmentfault.com\/?enc=pFQFZjNKFAwkRnwlh65Qyw%3D%3D.hFzjIWhdjZIepLwc9ajtSXUIXV%2FP%2FDWwnRalSRfuxUXJ6aYNvnwTbrQfGKpUg8ia\" rel=\"nofollow\">Understanding Postgres GIN Indexes: The Good and the Bad<\/a><br \/><a href=\"https:\/\/link.segmentfault.com\/?enc=21iEOAUMGBvQQkTsv%2BNWpQ%3D%3D.tshLElnMEh5JlF8cQLv9mzH6tb6UEnxz82%2BuE%2B%2FKKzU3%2FKW8lUHdDMD%2BBlpeJDFlPoVSFq1Jrm4eUDoxePJtUCncaH2iYEfSHxXYlznGPj6W3xICT797nICMN7X%2F8VDwZMnkQ4NNPqnEDDo1sdUUIQ%3D%3D\" rel=\"nofollow\">Postgres Planner not using GIN index Occasionally<\/a><br \/><a href=\"https:\/\/link.segmentfault.com\/?enc=FnSVL6pALkBrL6dYaAoJ1Q%3D%3D.R24sei5jBtCv4p6%2Bwii%2BYCna%2F5ceTU0YRl54nDBlXhyDNua0UYFgrhS4EViyib%2FpeNvISWX%2B3GvH415Ixpv9Hg%3D%3D\" rel=\"nofollow\">Gitlab once faced a GIN related issue<\/a><br \/><a href=\"https:\/\/link.segmentfault.com\/?enc=APlIrgdSLaxsiM6yuI6f2Q%3D%3D.ABTJoK4II0Zs2kaZxFfOKa1PkXxOTovT%2FvVp7RKInwRlj1oCy7r0u%2By8oHpczhHFqto70DAgdO8z6bp6XZGG2hbvDy2xoDQFb0JMk5GUoYCJT26nxGaUUeRGBNQsmHV1Qkg83LWzecQDNXDHbL0phA%3D%3D\" rel=\"nofollow\">Understanding Postgres query planner behaviour on GIN index<\/a><br \/><a href=\"https:\/\/link.segmentfault.com\/?enc=q1LCrlCNy62rFrJwndUnYA%3D%3D.j%2BVXBH9F2mLbY6iVNRYvMPmO%2BrjdIEcnzrnCDIT6qv37c0Y4npBBVcAOtqpLKr0ocwAkMTIxkBOtGfm8ZyssWg%3D%3D\" rel=\"nofollow\">Statistics used by the query planner<\/a><br \/><a href=\"https:\/\/link.segmentfault.com\/?enc=g8WBoZP1EYf0rXVFsrKEIg%3D%3D.iGIWzfTSKy1smoAJOL40BS9nxXYf%2BG0l%2BONJg2a8eTo7igJb92Am450UqdbOWKbEcp4nMVjAbUxMq9kjt1ZjuUbThAYfnRalXAIIJ%2BupkwU%3D\" rel=\"nofollow\">When To Avoid JSONB In A PostgreSQL Schema<\/a><br \/><a href=\"https:\/\/link.segmentfault.com\/?enc=qBfzYi8Jw0sI9AXzjN8p3w%3D%3D.02xKG1N%2FiGgP%2FKet4FTF5qtyvJR4LqteX6r7%2BJjqs7vpHrWeeOikzZZgdY2zXJNaIHdu2ZtaFe%2BaoKJBlXRCLMJWhS0%2FAic98pWWFERynSPhm6GaHuUj5neGbDVaG0Fv6VIRs%2BempwrIhRqzBIF6AA%3D%3D\" rel=\"nofollow\">Using JSONB in PostgreSQL: How to Effectively Store &#038; Index JSON Data in PostgreSQL<\/a><br \/><a href=\"https:\/\/link.segmentfault.com\/?enc=hHje8%2BfvlKfsiNc47c9t4w%3D%3D.ewAOHQ2jd8pbo6YAF9Lg75hy5CDvFCfY3LjXQq%2FOdwk3OFc6JMEst9B%2BGsByt62r\" rel=\"nofollow\">https:\/\/zhuanlan.zhihu.com\/p\/523900025<\/a><br \/><a href=\"https:\/\/link.segmentfault.com\/?enc=3A%2BnwXCj75UXWVlh%2ByYaAA%3D%3D.%2B8IVz%2BWsBv7kbtQFotOQUVi8wRK%2BMoxwDW1f2IBoSFcLwo7fh9ukaAyfyoMo3WOmOcCP5RsO3szTxg5TUBMNXA%3D%3D\" rel=\"nofollow\">https:\/\/www.cnblogs.com\/flying-tiger\/p\/6702796.html<\/a><br \/><a href=\"https:\/\/link.segmentfault.com\/?enc=DIQU2KXdS%2FbXipCIUo%2Bfsg%3D%3D.Nn724MKgAObOfSSLSSI2%2B%2F7ZDgvZFJz6oJQ2QwUg8JMe8FAQN53eJouqhUU6hpI854AZOuHABhEslTKpOQtRMA%3D%3D\" rel=\"nofollow\">https:\/\/cloud.tencent.com\/developer\/article\/1943819<\/a><\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000043146823\">https:\/\/segmentfault.com\/a\/1190000043146823<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e4b\u524d\u5728\u641e\u5b9c\u642d\u5143\u6570\u636e\u5e95\u5c42\u7d22\u5f15\u4f18\u5316\u7684\u65f6\u5019\uff0c\u9488\u5bf9\u4e00\u4e9b\u67e5\u8be2\u65f6\u5feb\u65f6\u6162\uff0c\u4ee5\u53ca\u4e00\u4e9b\u7d22\u5f15\u9009\u62e9\u7684\u95ee\u9898\uff0c\u7814\u7a76\u8fc7\uff0c\u4e5f\u57fa\u4e8e\u770b\u8fc7\u7684\u4e00\u4e9b\u6848 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10007007","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10007007","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10007007"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10007007\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10007007"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10007007"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10007007"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}