{"id":110006,"date":"2026-09-25T15:40:44","date_gmt":"2026-09-25T07:40:44","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/110006.html"},"modified":"2026-09-25T15:40:44","modified_gmt":"2026-09-25T07:40:44","slug":"%e4%bb%8e%e4%b8%80%e6%9d%a1-sql-%e7%9a%84%e7%94%9f%e5%91%bd%e5%91%a8%e6%9c%9f%e5%bc%80%e5%a7%8b%ef%bc%8c%e6%8a%8a-mysql-%e4%b8%80%e6%ac%a1%e6%80%a7%e6%83%b3%e6%98%8e%e7%99%bd","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/110006.html","title":{"rendered":"\u4ece\u4e00\u6761 SQL \u7684\u751f\u547d\u5468\u671f\u5f00\u59cb\uff0c\u628a MySQL \u4e00\u6b21\u6027\u60f3\u660e\u767d"},"content":{"rendered":"<hr \/>\n<p>\u8fd9\u7bc7\u7b14\u8bb0\u6cbf\u7740\u300c\u4e00\u6761 SQL \u8bed\u53e5\u5728 MySQL \u91cc\u7684\u5b8c\u6574\u65c5\u7a0b\u300d\u8fd9\u6761\u4e3b\u7ebf&#xff0c;\u628a\u67b6\u6784\u3001\u5b58\u50a8\u5f15\u64ce\u3001\u7d22\u5f15\u3001\u4e8b\u52a1\u3001MVCC\u3001\u9501\u3001\u65e5\u5fd7\u4e0e\u6027\u80fd\u4f18\u5316\u5168\u90e8\u4e32\u8d77\u6765\u2014\u2014\u6bcf\u7ae0\u90fd\u914d\u53ef\u8dd1\u7684\u5b9e\u6218 SQL&#xff08;\u7535\u5546\u5e93 mall&#xff09;&#xff0c;\u7ed3\u5c3e\u9644 20 \u95ee\u901f\u7b54\u3002\u5fd8\u4e86\u6ca1\u5173\u7cfb&#xff0c;\u8ddf\u7740\u4e3b\u7ebf\u8d70\u4e00\u904d&#xff0c;\u77e5\u8bc6\u70b9\u4f1a\u81ea\u5df1\u300c\u957f\u300d\u56de\u8111\u5b50\u91cc\u3002 \u9002\u7528\u7248\u672c&#xff1a;MySQL 8.0 \u00b7 InnoDB \u00b7 10 \u7ae0 \u00b7 40&#043; \u6bb5\u5b9e\u6218 SQL \u00b7 20 \u95ee\u901f\u67e5<\/p>\n<h3>\u76ee\u5f55<\/h3>\n<li>\u67b6\u6784\u5168\u666f&#xff1a;\u4e00\u6761 SQL \u7684\u751f\u547d\u5468\u671f<\/li>\n<li>\u5b58\u50a8\u5f15\u64ce\u4e0e\u6570\u636e\u7c7b\u578b<\/li>\n<li>SQL \u6838\u5fc3\u8bed\u6cd5\u4e0e\u7535\u5546\u5efa\u5e93\u5b9e\u6218<\/li>\n<li>\u67e5\u8be2\u8fdb\u9636&#xff1a;JOIN\u3001\u7a97\u53e3\u51fd\u6570\u4e0e\u7ecf\u5178\u4e1a\u52a1\u9898<\/li>\n<li>\u7d22\u5f15&#xff1a;\u4ece B&#043; \u6811\u5230 EXPLAIN \u5b9e\u6218<\/li>\n<li>\u4e8b\u52a1\u4e0e MVCC<\/li>\n<li>\u9501&#xff1a;\u4ece MDL \u5230\u95f4\u9699\u9501<\/li>\n<li>\u65e5\u5fd7\u7cfb\u7edf&#xff1a;redo \/ undo \/ binlog<\/li>\n<li>\u6027\u80fd\u4f18\u5316\u5b9e\u6218<\/li>\n<li>\u901f\u67e5\u624b\u518c\u4e0e\u590d\u4e60\u8def\u7ebf<\/li>\n<hr \/>\n<h3>\u4e00\u3001\u67b6\u6784\u5168\u666f&#xff1a;\u4e00\u6761 SQL \u7684\u751f\u547d\u5468\u671f<\/h3>\n<p>\u56de\u5fc6 MySQL \u6700\u9ad8\u6548\u7684\u8def\u5f84&#xff0c;\u4e0d\u662f\u628a\u77e5\u8bc6\u70b9\u4e00\u4e2a\u4e2a\u80cc\u56de\u6765&#xff0c;\u800c\u662f\u987a\u7740**\u300c\u4e00\u6761 SQL \u8bed\u53e5\u4ece\u5ba2\u6237\u7aef\u53d1\u51fa\u53bb&#xff0c;\u5230\u62ff\u5230\u7ed3\u679c\u300d\u7684\u5b8c\u6574\u65c5\u7a0b**\u8d70\u4e00\u904d\u2014\u2014\u6bcf\u4e2a\u7ec4\u4ef6\u5728\u65c5\u7a0b\u4e2d\u8f6e\u756a\u767b\u573a&#xff0c;\u5fd8\u4e86\u54ea\u4e2a\u5c31\u8865\u54ea\u4e2a\u3002\u8fd9\u4e00\u7ae0\u5148\u5efa\u7acb\u5168\u666f\u5730\u56fe&#xff0c;\u540e\u9762\u4e5d\u7ae0\u90fd\u662f\u5bf9\u5730\u56fe\u67d0\u4e00\u5757\u7684\u653e\u5927\u3002<\/p>\n<h4>1.1 \u5148\u770b\u5168\u666f&#xff1a;\u56db\u5c42\u7ed3\u6784<\/h4>\n<p>\u628a MySQL \u60f3\u8c61\u6210\u4e00\u680b\u56db\u5c42\u697c&#xff1a;\u5ba2\u6237\u7aef\u5728\u697c\u5916&#xff0c;SQL \u4ece\u697c\u9876\u8fdb\u3001\u6570\u636e\u5728\u697c\u5e95\u5b58\u3002<\/p>\n<p>  #mermaid-svg-7YIbNxS3lh4m5yJp{font-family:\\&#8221;trebuchet ms\\&#8221;,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-7YIbNxS3lh4m5yJp .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-7YIbNxS3lh4m5yJp .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-7YIbNxS3lh4m5yJp .error-icon{fill:#552222;}#mermaid-svg-7YIbNxS3lh4m5yJp .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-7YIbNxS3lh4m5yJp .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-7YIbNxS3lh4m5yJp .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-7YIbNxS3lh4m5yJp .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-7YIbNxS3lh4m5yJp .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-7YIbNxS3lh4m5yJp .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-7YIbNxS3lh4m5yJp .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-7YIbNxS3lh4m5yJp .marker{fill:#333333;stroke:#333333;}#mermaid-svg-7YIbNxS3lh4m5yJp .marker.cross{stroke:#333333;}#mermaid-svg-7YIbNxS3lh4m5yJp svg{font-family:\\&#8221;trebuchet ms\\&#8221;,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-7YIbNxS3lh4m5yJp p{margin:0;}#mermaid-svg-7YIbNxS3lh4m5yJp .label{font-family:\\&#8221;trebuchet ms\\&#8221;,verdana,arial,sans-serif;color:#333;}#mermaid-svg-7YIbNxS3lh4m5yJp .cluster-label text{fill:#333;}#mermaid-svg-7YIbNxS3lh4m5yJp .cluster-label span{color:#333;}#mermaid-svg-7YIbNxS3lh4m5yJp .cluster-label span p{background-color:transparent;}#mermaid-svg-7YIbNxS3lh4m5yJp .label text,#mermaid-svg-7YIbNxS3lh4m5yJp span{fill:#333;color:#333;}#mermaid-svg-7YIbNxS3lh4m5yJp .node rect,#mermaid-svg-7YIbNxS3lh4m5yJp .node circle,#mermaid-svg-7YIbNxS3lh4m5yJp .node ellipse,#mermaid-svg-7YIbNxS3lh4m5yJp .node polygon,#mermaid-svg-7YIbNxS3lh4m5yJp .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-7YIbNxS3lh4m5yJp .rough-node .label text,#mermaid-svg-7YIbNxS3lh4m5yJp .node .label text,#mermaid-svg-7YIbNxS3lh4m5yJp .image-shape .label,#mermaid-svg-7YIbNxS3lh4m5yJp .icon-shape .label{text-anchor:middle;}#mermaid-svg-7YIbNxS3lh4m5yJp .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-7YIbNxS3lh4m5yJp .rough-node .label,#mermaid-svg-7YIbNxS3lh4m5yJp .node .label,#mermaid-svg-7YIbNxS3lh4m5yJp .image-shape .label,#mermaid-svg-7YIbNxS3lh4m5yJp .icon-shape .label{text-align:center;}#mermaid-svg-7YIbNxS3lh4m5yJp .node.clickable{cursor:pointer;}#mermaid-svg-7YIbNxS3lh4m5yJp .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-7YIbNxS3lh4m5yJp .arrowheadPath{fill:#333333;}#mermaid-svg-7YIbNxS3lh4m5yJp .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-7YIbNxS3lh4m5yJp .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-7YIbNxS3lh4m5yJp .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-7YIbNxS3lh4m5yJp .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-7YIbNxS3lh4m5yJp .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-7YIbNxS3lh4m5yJp .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-7YIbNxS3lh4m5yJp .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-7YIbNxS3lh4m5yJp .cluster text{fill:#333;}#mermaid-svg-7YIbNxS3lh4m5yJp .cluster span{color:#333;}#mermaid-svg-7YIbNxS3lh4m5yJp div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\\&#8221;trebuchet ms\\&#8221;,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-7YIbNxS3lh4m5yJp .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-7YIbNxS3lh4m5yJp rect.text{fill:none;stroke-width:0;}#mermaid-svg-7YIbNxS3lh4m5yJp .icon-shape,#mermaid-svg-7YIbNxS3lh4m5yJp .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-7YIbNxS3lh4m5yJp .icon-shape p,#mermaid-svg-7YIbNxS3lh4m5yJp .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-7YIbNxS3lh4m5yJp .icon-shape .label rect,#mermaid-svg-7YIbNxS3lh4m5yJp .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-7YIbNxS3lh4m5yJp .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-7YIbNxS3lh4m5yJp .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-7YIbNxS3lh4m5yJp :root{&#8211;mermaid-font-family:\\&#8221;trebuchet ms\\&#8221;,verdana,arial,sans-serif;}<\/p>\n<p>         <span class=\"nodeLabel\"><\/p>\n<p>\u6587\u4ef6\u7cfb\u7edf\u5c42<\/p>\n<p><\/span><\/p>\n<p>         <span class=\"nodeLabel\"><\/p>\n<p>\u5b58\u50a8\u5f15\u64ce\u5c42&#xff08;InnoDB&#xff09;<\/p>\n<p><\/span><\/p>\n<p>         <span class=\"nodeLabel\"><\/p>\n<p>MySQL \u670d\u52a1\u5c42&#xff08;Server Layer&#xff09;<\/p>\n<p><\/span><\/p>\n<p>         <span class=\"edgeLabel\"><\/p>\n<p>SQL \u8bed\u53e5<\/p>\n<p><\/span><\/p>\n<p>         <span class=\"edgeLabel\"><\/span><\/p>\n<p>         <span class=\"edgeLabel\"><\/span><\/p>\n<p>         <span class=\"edgeLabel\"><\/span><\/p>\n<p>         <span class=\"edgeLabel\"><\/span><\/p>\n<p>         <span class=\"edgeLabel\"><\/span><\/p>\n<p>         <span class=\"edgeLabel\"><\/span><\/p>\n<p>         <span class=\"edgeLabel\"><\/span><\/p>\n<p>         <span class=\"edgeLabel\"><\/span><\/p>\n<p>         <span class=\"edgeLabel\"><\/p>\n<p>\u4ec5\u66f4\u65b0\u8bed\u53e5&#xff0c;\u63d0\u4ea4\u65f6\u5199<\/p>\n<p><\/span><\/p>\n<p>         <span class=\"nodeLabel\"><\/p>\n<p>\u5ba2\u6237\u7aefNavicat \u00b7 JDBC \u00b7 \u7a0b\u5e8f<\/p>\n<p><\/span><\/p>\n<p>         <span class=\"nodeLabel\"><\/p>\n<p>\u8fde\u63a5\u5668\u8fde\u63a5\u7ba1\u7406 \u00b7 \u6743\u9650\u6821\u9a8c<\/p>\n<p><\/span><\/p>\n<p>         <span class=\"nodeLabel\"><\/p>\n<p>\u5206\u6790\u5668\u8bcd\u6cd5 \u00b7 \u8bed\u6cd5\u89e3\u6790<\/p>\n<p><\/span><\/p>\n<p>         <span class=\"nodeLabel\"><\/p>\n<p>\u4f18\u5316\u5668\u9009\u7d22\u5f15 \u00b7 \u5b9a\u6267\u884c\u8ba1\u5212<\/p>\n<p><\/span><\/p>\n<p>         <span class=\"nodeLabel\"><\/p>\n<p>\u6267\u884c\u5668\u8c03\u7528\u5f15\u64ce\u63a5\u53e3<\/p>\n<p><\/span><\/p>\n<p>         <span class=\"nodeLabel\"><\/p>\n<p>Buffer Pool\u5185\u5b58\u6570\u636e\u9875<\/p>\n<p><\/span><\/p>\n<p>         <span class=\"nodeLabel\"><\/p>\n<p>\u4e8b\u52a1 \u00b7 \u9501 \u00b7 MVCC<\/p>\n<p><\/span><\/p>\n<p>         <span class=\"nodeLabel\"><\/p>\n<p>\u6570\u636e\u6587\u4ef6.ibd \u8868\u7a7a\u95f4<\/p>\n<p><\/span><\/p>\n<p>         <span class=\"nodeLabel\"><\/p>\n<p>redo log<\/p>\n<p><\/span><\/p>\n<p>         <span class=\"nodeLabel\"><\/p>\n<p>undo log<\/p>\n<p><\/span><\/p>\n<p>         <span class=\"nodeLabel\"><\/p>\n<p>binlog<\/p>\n<p><\/span><\/p>\n<ul>\n<li>\u8fde\u63a5\u5c42&#xff1a;\u7ba1\u7406 TCP \u8fde\u63a5\u3001\u8d26\u53f7\u5bc6\u7801\u8ba4\u8bc1\u3001\u7ebf\u7a0b\u590d\u7528\u3002\u5ba2\u6237\u7aef\u771f\u6b63\u8fde\u7684\u662f\u5b83\u3002<\/li>\n<li>\u670d\u52a1\u5c42&#xff1a;\u5305\u542b\u8fde\u63a5\u5668\u3001\u5206\u6790\u5668\u3001\u4f18\u5316\u5668\u3001\u6267\u884c\u5668&#xff0c;\u4ee5\u53ca\u5185\u7f6e\u51fd\u6570\u3001\u5b58\u50a8\u8fc7\u7a0b\u3001\u89e6\u53d1\u5668\u7b49\u8de8\u5f15\u64ce\u80fd\u529b\u3002\u6838\u5fc3 SQL \u903b\u8f91\u90fd\u5728\u8fd9\u5c42\u3002<\/li>\n<li>\u5b58\u50a8\u5f15\u64ce\u5c42&#xff1a;\u63d2\u4ef6\u5f0f\u8bbe\u8ba1&#xff0c;\u771f\u6b63\u8d1f\u8d23\u6570\u636e\u7684\u5b58\u4e0e\u53d6\u3002InnoDB \u8fd8\u72ec\u5360\u7ba1\u7406\u4e8b\u52a1\u3001\u9501\u3001MVCC \u548c redo\/undo \u65e5\u5fd7\u3002<\/li>\n<li>\u6587\u4ef6\u7cfb\u7edf\u5c42&#xff1a;\u6570\u636e\u6587\u4ef6&#xff08;.ibd&#xff09;\u548c\u4e09\u7c7b\u65e5\u5fd7\u7684\u6700\u7ec8\u5f52\u5bbf&#xff0c;\u843d\u76d8\u7684\u5730\u65b9\u3002<\/li>\n<\/ul>\n<p>\u8bb0\u5fc6\u951a\u70b9&#xff1a;\u670d\u52a1\u5c42\u7ba1\u300c\u600e\u4e48\u6267\u884c SQL\u300d&#xff0c;\u5f15\u64ce\u5c42\u7ba1\u300c\u6570\u636e\u600e\u4e48\u5b58\u53d6\u300d\u3002binlog \u5c5e\u4e8e\u670d\u52a1\u5c42&#xff0c;redo\/undo \u5c5e\u4e8e InnoDB\u2014\u2014\u8fd9\u4e2a\u5f52\u5c5e\u5173\u7cfb\u540e\u9762\u8bb2\u4e24\u9636\u6bb5\u63d0\u4ea4\u65f6\u4f1a\u53cd\u590d\u7528\u5230\u3002<\/p>\n<h4>1.2 \u4e00\u6761 SELECT \u7684\u4e94\u6b65\u65c5\u7a0b<\/h4>\n<p>\u4ee5\u6700\u5e38\u89c1\u7684\u4e00\u53e5\u8bdd\u4e3a\u4f8b&#xff1a;SELECT * FROM users WHERE id &#061; 1;<\/p>\n<li>\u8fde\u63a5\u5668&#xff1a;\u5efa\u7acb&#xff08;\u6216\u590d\u7528&#xff09;\u8fde\u63a5&#xff0c;\u6821\u9a8c\u8d26\u53f7\u5bc6\u7801&#xff0c;\u968f\u540e\u8bfb\u53d6\u8be5\u8d26\u53f7\u7684\u6743\u9650\u8868\u5e76\u7f13\u5b58\u5230\u8fd9\u4e2a\u8fde\u63a5\u91cc\u3002\u6240\u4ee5\u4fee\u6539\u7528\u6237\u6743\u9650\u53ea\u5bf9\u65b0\u8fde\u63a5\u751f\u6548&#xff0c;\u8001\u8fde\u63a5\u8981\u91cd\u8fde\u624d\u611f\u77e5\u3002<\/li>\n<li>\u67e5\u8be2\u7f13\u5b58&#xff08;8.0 \u5df2\u79fb\u9664&#xff09;&#xff1a;5.7 \u53ca\u4ee5\u524d\u7684\u7248\u672c\u4f1a\u5148\u62ff SQL \u539f\u6587\u5f53 key \u67e5\u7f13\u5b58&#xff0c;\u547d\u4e2d\u76f4\u63a5\u8fd4\u56de\u3002\u4f46\u53ea\u8981\u8868\u91cc\u6709\u4e00\u884c\u66f4\u65b0&#xff0c;\u8fd9\u5f20\u8868\u76f8\u5173\u7684\u7f13\u5b58\u5168\u90e8\u5931\u6548&#xff0c;\u5b9e\u9645\u547d\u4e2d\u7387\u6781\u4f4e&#xff0c;MySQL 8.0 \u628a\u6574\u5757\u529f\u80fd\u5220\u6389\u4e86\u3002<\/li>\n<li>\u5206\u6790\u5668&#xff1a;\u5148\u505a\u8bcd\u6cd5\u5206\u6790&#xff08;\u628a\u5b57\u7b26\u4e32\u62c6\u6210\u5173\u952e\u5b57\u3001\u8868\u540d\u3001\u5217\u540d&#xff09;&#xff0c;\u518d\u505a\u8bed\u6cd5\u5206\u6790\u751f\u6210\u8bed\u6cd5\u6811\u3002\u62a5\u9519 You have an error in your SQL syntax \u5c31\u662f\u8fd9\u4e00\u6b65\u629b\u7684\u3002<\/li>\n<li>\u4f18\u5316\u5668&#xff1a;\u51b3\u5b9a\u300c\u600e\u4e48\u6267\u884c\u6700\u5feb\u300d\u2014\u2014\u7528\u4e3b\u952e\u7d22\u5f15\u8fd8\u662f\u4e8c\u7ea7\u7d22\u5f15\u3001\u591a\u8868 JOIN \u65f6\u8c01\u505a\u9a71\u52a8\u8868\u3002\u5b83\u4ea7\u51fa\u7684\u65b9\u6848\u5c31\u662f EXPLAIN \u80fd\u770b\u5230\u7684\u4e1c\u897f\u3002<\/li>\n<li>\u6267\u884c\u5668&#xff1a;\u5148\u68c0\u67e5\u5bf9\u8fd9\u4e2a\u8868\u6709\u6ca1\u6709\u6267\u884c\u6743\u9650&#xff0c;\u7136\u540e\u8c03\u7528 InnoDB \u7684\u63a5\u53e3\u53d6\u6570\u636e\u3002InnoDB \u5b9a\u4f4d\u5230 id&#061;1 \u7684\u884c\u8fd4\u56de\u7ed9\u6267\u884c\u5668&#xff0c;\u6267\u884c\u5668\u628a\u7ed3\u679c\u96c6\u53d1\u7ed9\u5ba2\u6237\u7aef\u3002<\/li>\n<p>\u8bb0\u5fc6\u951a\u70b9&#xff1a;\u5206\u6790\u5668\u7ba1\u300c\u8fd9\u53e5\u8bdd\u5bf9\u4e0d\u5bf9\u300d&#xff0c;\u4f18\u5316\u5668\u7ba1\u300c\u600e\u4e48\u6267\u884c\u6700\u5feb\u300d&#xff0c;\u6267\u884c\u5668\u7ba1\u300c\u771f\u53bb\u5e72\u6d3b\u300d&#xff0c;\u5b58\u50a8\u5f15\u64ce\u7ba1\u300c\u6570\u636e\u600e\u4e48\u5b58\u53d6\u300d\u3002\u56db\u8fde\u95ee&#xff0c;\u9762\u8bd5\u548c\u6392\u969c\u90fd\u9760\u5b83\u5b9a\u4f4d\u95ee\u9898\u5c42\u3002<\/p>\n<h4>1.3 \u4e00\u6761 UPDATE \u591a\u4e86\u4ec0\u4e48&#xff08;\u4f0f\u7b14&#xff09;<\/h4>\n<p>\u6362\u6210 UPDATE users SET status &#061; 0 WHERE id &#061; 1;&#xff0c;\u524d\u56db\u6b65\u5b8c\u5168\u4e00\u6837&#xff0c;\u4f46\u8fdb\u5165 InnoDB \u540e\u591a\u51fa\u4e00\u6574\u5957\u52a8\u4f5c&#xff1a;<\/p>\n<li>\u628a\u76ee\u6807\u884c\u6240\u5728\u6570\u636e\u9875\u52a0\u8f7d\u8fdb Buffer Pool&#xff08;\u5185\u5b58&#xff09;&#xff1b;<\/li>\n<li>\u5199 undo log&#xff08;\u8bb0\u5f55\u4fee\u6539\u524d\u7684\u503c&#xff0c;\u4f9b\u56de\u6eda\u548c MVCC \u7528&#xff09;&#xff1b;<\/li>\n<li>\u5728 Buffer Pool \u91cc\u6539\u6570\u636e\u884c&#xff0c;\u8be5\u9875\u53d8\u6210\u810f\u9875&#xff0c;\u7531\u540e\u53f0\u7ebf\u7a0b\u5f02\u6b65\u5237\u56de\u78c1\u76d8&#xff1b;<\/li>\n<li>\u5199 redo log&#xff08;prepare \u72b6\u6001&#xff09;&#xff1b;<\/li>\n<li>\u6267\u884c\u5668\u5199 binlog&#xff0c;\u968f\u540e redo log \u7f6e\u4e3a commit\u2014\u2014\u8fd9\u5c31\u662f\u4e24\u9636\u6bb5\u63d0\u4ea4&#xff0c;\u7b2c 8 \u7ae0\u4e13\u95e8\u62c6\u3002<\/li>\n<p>\u8fd9\u5957\u300c\u5148\u5199\u65e5\u5fd7\u3001\u540e\u5237\u6570\u636e\u9875\u300d\u7684\u8bbe\u8ba1\u53eb WAL&#xff08;Write-Ahead Logging&#xff09;&#xff0c;\u662f MySQL \u5d29\u6e83\u540e\u80fd\u81ea\u52a8\u6062\u590d\u7684\u5168\u90e8\u5e95\u6c14\u3002<\/p>\n<h4>1.4 \u8fde\u63a5\u7ba1\u7406\u5b9e\u6218<\/h4>\n<p>\u8fde\u63a5\u5efa\u7acb\u6709\u771f\u5b9e\u5f00\u9500&#xff1a;TCP \u63e1\u624b\u3001\u8eab\u4efd\u8ba4\u8bc1\u3001\u8bfb\u6743\u9650\u8868\u3002\u9ad8\u5e76\u53d1\u77ed\u8fde\u63a5\u573a\u666f\u4e0b\u8fd9\u4e9b\u5f00\u9500\u4f1a\u88ab\u653e\u5927&#xff0c;\u6240\u4ee5\u5e94\u7528\u4fa7\u6c38\u8fdc\u7528\u8fde\u63a5\u6c60&#xff08;FastAPI \u4fa7\u5982 SQLAlchemy \u7684 pool_size\u3001pool_recycle&#xff09;\u3002\u4e24\u4e2a\u7ecf\u5178\u95ee\u9898\u5148\u8bb0\u4e0b\u6765&#xff1a;<\/p>\n<ul>\n<li>\u7a7a\u95f2\u8fde\u63a5\u8d85\u65f6&#xff1a;\u7531 wait_timeout&#xff08;\u9ed8\u8ba4 8 \u5c0f\u65f6&#xff09;\u63a7\u5236&#xff0c;\u8d85\u65f6\u540e\u670d\u52a1\u7aef\u4e3b\u52a8\u65ad\u5f00&#xff0c;\u5ba2\u6237\u7aef\u518d\u53d1 SQL \u5c31\u4f1a\u6536\u5230 MySQL server has gone away\u3002\u8fde\u63a5\u6c60\u7684 pool_recycle \u8981\u8bbe\u5f97\u6bd4\u5b83\u5c0f\u3002<\/li>\n<li>\u957f\u8fde\u63a5\u5185\u5b58\u4e0a\u6da8&#xff1a;5.7 \u53ca\u4ee5\u524d\u7248\u672c\u4e2d&#xff0c;\u8fde\u63a5\u5185\u6267\u884c\u8fc7\u7684\u5927\u7684\u4e34\u65f6\u7ed3\u679c\u4e0d\u4f1a\u7acb\u5373\u91ca\u653e&#xff0c;\u957f\u8fde\u63a5\u8d8a\u4e45\u5185\u5b58\u8d8a\u9ad8\u3002\u5bf9\u7b56&#xff1a;\u8fde\u63a5\u6c60\u5b9a\u671f\u56de\u6536\u91cd\u5efa\u8fde\u63a5&#xff0c;\u6216\u6267\u884c\u5b8c\u5927\u4efb\u52a1\u540e\u8c03\u7528 mysql_reset_connection()\u3002<\/li>\n<\/ul>\n<p>\u5b9e\u6218\u6f14\u7ec3&#xff1a;\u4e09\u6761\u547d\u4ee4\u770b\u6e05\u5f53\u524d\u8fde\u63a5<\/p>\n<p><span class=\"token keyword\">SHOW<\/span> PROCESSLIST<span class=\"token punctuation\">;<\/span>                      <span class=\"token comment\">&#8212; \u8c01\u8fde\u7740\u3001\u6b63\u5728\u5e72\u561b<\/span><br \/>\n<span class=\"token keyword\">SHOW<\/span> <span class=\"token keyword\">STATUS<\/span> <span class=\"token operator\">LIKE<\/span> <span class=\"token string\">&#039;Threads%&#039;<\/span><span class=\"token punctuation\">;<\/span>           <span class=\"token comment\">&#8212; \u8fde\u63a5\u6570\u6982\u51b5<\/span><br \/>\n<span class=\"token keyword\">SHOW<\/span> VARIABLES <span class=\"token operator\">LIKE<\/span> <span class=\"token string\">&#039;wait_timeout&#039;<\/span><span class=\"token punctuation\">;<\/span>    <span class=\"token comment\">&#8212; \u7a7a\u95f2\u591a\u4e45\u4f1a\u88ab\u65ad\u5f00<\/span><\/p>\n<p>mysql&gt; SHOW PROCESSLIST;<br \/>\n&#043;&#8212;-&#043;&#8212;&#8212;&#043;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#043;&#8212;&#8212;&#043;&#8212;&#8212;&#8212;&#043;&#8212;&#8212;&#043;&#8212;&#8212;&#8212;&#8212;&#043;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;&#043;<br \/>\n| Id | User | Host                | db   | Command | Time | State      | Info                     |<br \/>\n&#043;&#8212;-&#043;&#8212;&#8212;&#043;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#043;&#8212;&#8212;&#043;&#8212;&#8212;&#8212;&#043;&#8212;&#8212;&#043;&#8212;&#8212;&#8212;&#8212;&#043;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;&#043;<br \/>\n|  8 | root | 192.168.1.10:52331  | mall | Query   |    0 | starting   | SHOW PROCESSLIST         |<br \/>\n| 12 | app  | 192.168.1.20:60012  | mall | Sleep   |  120 |            | NULL                     |<br \/>\n| 15 | app  | 192.168.1.20:60018  | mall | Query   |   12 | sending da | SELECT * FROM orders &#8230; |<br \/>\n&#043;&#8212;-&#043;&#8212;&#8212;&#043;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#043;&#8212;&#8212;&#043;&#8212;&#8212;&#8212;&#043;&#8212;&#8212;&#043;&#8212;&#8212;&#8212;&#8212;&#043;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;&#043;<br \/>\n&#8212; Command&#061;Query \u4e14 Time \u5f88\u5927&#xff1a;\u5927\u6982\u7387\u662f\u6162 SQL \u6216\u88ab\u9501\u963b\u585e&#xff1b;\u786e\u8ba4\u540e KILL 15; \u6390\u6389\u5b83<\/p>\n<hr \/>\n<h3>\u4e8c\u3001\u5b58\u50a8\u5f15\u64ce\u4e0e\u6570\u636e\u7c7b\u578b<\/h3>\n<p>\u8fd9\u4e00\u7ae0\u56de\u7b54\u4e24\u4e2a\u95ee\u9898&#xff1a;\u8868\u7528\u4ec0\u4e48\u5f15\u64ce\u5efa&#xff1f;\u6bcf\u4e2a\u5b57\u6bb5\u9009\u4ec0\u4e48\u7c7b\u578b&#xff1f;\u2014\u2014\u8fd9\u4e24\u4ef6\u4e8b\u5728\u5efa\u8868\u90a3\u4e00\u523b\u5c31\u51b3\u5b9a\u4e86\u8fd9\u5f20\u8868\u672a\u6765\u4e94\u5e74\u7684\u6027\u80fd\u4e0a\u9650\u3002<\/p>\n<h4>2.1 InnoDB \u4e0e MyISAM&#xff1a;\u4e00\u573a\u5df2\u7ecf\u7ed3\u675f\u7684\u6bd4\u8d5b<\/h4>\n<p>MySQL 5.5 \u4e4b\u524d\u9ed8\u8ba4\u5f15\u64ce\u662f MyISAM&#xff0c;5.5 \u8d77\u6362\u6210 InnoDB&#xff0c;8.0 \u8fde\u7cfb\u7edf\u8868\u4e5f\u5168\u90e8\u6539\u7528 InnoDB\u3002\u9762\u8bd5\u8fd8\u7231\u95ee\u8fd9\u4e2a\u5bf9\u6bd4&#xff0c;\u56e0\u4e3a\u5bf9\u6bd4\u7684\u6bcf\u4e00\u884c\u90fd\u662f\u8003\u70b9&#xff1a;<\/p>\n<table>\n<tr>\u7ef4\u5ea6InnoDBMyISAM<\/tr>\n<tbody>\n<tr>\n<td>\u4e8b\u52a1<\/td>\n<td>\u652f\u6301&#xff08;ACID&#xff09;<\/td>\n<td>\u4e0d\u652f\u6301<\/td>\n<\/tr>\n<tr>\n<td>\u9501\u7c92\u5ea6<\/td>\n<td>\u884c\u7ea7\u9501&#xff08;\u5e76\u53d1\u9ad8&#xff09;<\/td>\n<td>\u8868\u7ea7\u9501&#xff08;\u8bfb\u5199\u4e92\u65a5&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u5916\u952e<\/td>\n<td>\u652f\u6301<\/td>\n<td>\u4e0d\u652f\u6301<\/td>\n<\/tr>\n<tr>\n<td>\u5d29\u6e83\u6062\u590d<\/td>\n<td>redo log \u81ea\u52a8\u6062\u590d&#xff0c;\u5d29\u6e83\u5b89\u5168<\/td>\n<td>\u51e0\u4e4e\u6ca1\u6709&#xff0c;\u65ad\u7535\u6613\u574f\u8868<\/td>\n<\/tr>\n<tr>\n<td>\u7d22\u5f15\u7ec4\u7ec7<\/td>\n<td>\u805a\u7c07\u7d22\u5f15&#xff1a;\u53f6\u5b50\u8282\u70b9\u5b58\u6574\u884c\u6570\u636e<\/td>\n<td>\u975e\u805a\u7c07&#xff1a;\u7d22\u5f15\u4e0e\u6570\u636e\u6587\u4ef6\u5206\u79bb<\/td>\n<\/tr>\n<tr>\n<td>\u65e0 where \u7684 count(*)<\/td>\n<td>\u8981\u626b\u63cf&#xff08;MVCC \u4e0b\u5404\u4e8b\u52a1\u53ef\u89c1\u884c\u6570\u4e0d\u540c&#xff09;<\/td>\n<td>O(1)&#xff0c;\u76f4\u63a5\u8bfb\u5b58\u50a8\u7684\u884c\u6570<\/td>\n<\/tr>\n<tr>\n<td>\u5168\u6587\u7d22\u5f15<\/td>\n<td>5.6 \u8d77\u652f\u6301<\/td>\n<td>\u652f\u6301<\/td>\n<\/tr>\n<tr>\n<td>\u4eca\u5929\u7684\u7528\u9014<\/td>\n<td>99% \u7684 OLTP \u573a\u666f\u9ed8\u8ba4\u9009\u5b83<\/td>\n<td>\u7eaf\u53ea\u8bfb\u7edf\u8ba1\u3001\u5f52\u6863&#xff08;\u4e5f\u57fa\u672c\u88ab\u6dd8\u6c70&#xff09;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5b9e\u6218\u6f14\u7ec3&#xff1a;\u67e5\u770b\u5f15\u64ce<\/p>\n<p><span class=\"token keyword\">SHOW<\/span> ENGINES<span class=\"token punctuation\">;<\/span>                                      <span class=\"token comment\">&#8212; \u67e5\u770b\u652f\u6301\u7684\u5f15\u64ce\u6e05\u5355<\/span><br \/>\n<span class=\"token keyword\">SHOW<\/span> <span class=\"token keyword\">TABLE<\/span> <span class=\"token keyword\">STATUS<\/span> <span class=\"token keyword\">FROM<\/span> mall <span class=\"token keyword\">WHERE<\/span> Name <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#039;orders&#039;<\/span><span class=\"token punctuation\">;<\/span> <span class=\"token comment\">&#8212; \u770b\u67d0\u5f20\u8868\u7528\u7684\u5f15\u64ce<\/span><br \/>\n<span class=\"token comment\">&#8212; 8.0 \u91cc\u67e5\u5230\u7684 Engine \u5217\u5e94\u4e3a InnoDB&#xff1b;\u60f3\u8fc1\u79fb\u8001\u8868&#xff1a;<\/span><br \/>\n<span class=\"token keyword\">ALTER<\/span> <span class=\"token keyword\">TABLE<\/span> old_table <span class=\"token keyword\">ENGINE<\/span> <span class=\"token operator\">&#061;<\/span> <span class=\"token keyword\">InnoDB<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<h4>2.2 \u6570\u503c\u7c7b\u578b&#xff1a;\u6574\u6570\u4e0e\u5b9e\u6570<\/h4>\n<table>\n<tr>\u7c7b\u578b\u5b57\u8282\u6709\u7b26\u53f7\u8303\u56f4UNSIGNED\u5178\u578b\u7528\u9014<\/tr>\n<tbody>\n<tr>\n<td>TINYINT<\/td>\n<td>1<\/td>\n<td>-128 ~ 127<\/td>\n<td>0 ~ 255<\/td>\n<td>\u72b6\u6001\u3001\u7c7b\u578b\u3001\u5f00\u5173<\/td>\n<\/tr>\n<tr>\n<td>SMALLINT<\/td>\n<td>2<\/td>\n<td>\u00b13.2 \u4e07<\/td>\n<td>0 ~ 6.5 \u4e07<\/td>\n<td>\u5c0f\u8303\u56f4\u8ba1\u6570<\/td>\n<\/tr>\n<tr>\n<td>MEDIUMINT<\/td>\n<td>3<\/td>\n<td>\u00b1838 \u4e07<\/td>\n<td>0 ~ 1677 \u4e07<\/td>\n<td>\u4e2d\u7b49\u8ba1\u6570<\/td>\n<\/tr>\n<tr>\n<td>INT<\/td>\n<td>4<\/td>\n<td>\u00b121.4 \u4ebf<\/td>\n<td>0 ~ 42.9 \u4ebf<\/td>\n<td>\u4e00\u822c\u6570\u503c<\/td>\n<\/tr>\n<tr>\n<td>BIGINT<\/td>\n<td>8<\/td>\n<td>\u00b19.2 \u00d7 10^18<\/td>\n<td>\u66f4\u5927<\/td>\n<td>\u4e3b\u952e\u3001\u8ba2\u5355\u53f7\u3001\u65f6\u95f4\u6233(ms)<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<ul>\n<li>INT(11) \u91cc\u7684 11 \u53ea\u662f\u300c\u663e\u793a\u5bbd\u5ea6\u300d&#xff0c;\u4e0d\u9650\u5236\u53d6\u503c\u8303\u56f4&#xff0c;8.0.17 \u8d77\u5b98\u65b9\u5df2\u5e9f\u5f03\u8fd9\u4e2a\u5199\u6cd5&#xff0c;\u76f4\u63a5\u5199 INT \u5373\u53ef\u3002<\/li>\n<li>\u72b6\u6001\u5b57\u6bb5\u7528 TINYINT UNSIGNED \u5e76\u5728 COMMENT \u91cc\u5199\u6e05\u679a\u4e3e\u542b\u4e49&#xff0c;\u522b\u7528 ENUM&#xff08;\u89c1 2.3&#xff09;\u3002<\/li>\n<li>\u4e3b\u952e\u7edf\u4e00 BIGINT UNSIGNED&#xff1a;INT \u7684 21 \u4ebf\u5728\u6570\u636e\u91cf\u5927\u7684\u4e1a\u52a1\u91cc\u771f\u4f1a\u7528\u5b8c&#xff0c;\u4e14\u672a\u6765\u63a5\u96ea\u82b1 ID\u3001\u5408\u5e76\u8868\u90fd\u65b9\u4fbf\u3002<\/li>\n<\/ul>\n<p>\u5b9e\u6570\u53ea\u6709\u4e00\u6761\u519b\u89c4&#xff1a;\u91d1\u989d\u5fc5\u987b\u7528 DECIMAL\u3002FLOAT\/DOUBLE \u662f\u4e8c\u8fdb\u5236\u6d6e\u70b9\u6570&#xff0c;\u5929\u751f\u65e0\u6cd5\u7cbe\u786e\u8868\u793a 0.1 \u8fd9\u6837\u7684\u5341\u8fdb\u5236\u5c0f\u6570&#xff1a;<\/p>\n<p><span class=\"token keyword\">SELECT<\/span> <span class=\"token number\">0.1<\/span> <span class=\"token operator\">&#043;<\/span> <span class=\"token number\">0.2<\/span> <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">0.3<\/span><span class=\"token punctuation\">;<\/span>   <span class=\"token comment\">&#8212; 0&#xff08;\u4e0d\u76f8\u7b49&#xff01;\u6d6e\u70b9\u7cbe\u5ea6\u4e22\u5931&#xff09;<\/span><br \/>\n<span class=\"token keyword\">SELECT<\/span> <span class=\"token number\">0.1<\/span> <span class=\"token operator\">&#043;<\/span> <span class=\"token number\">0.2<\/span><span class=\"token punctuation\">;<\/span>         <span class=\"token comment\">&#8212; 0.30000000000000004<\/span><\/p>\n<p><span class=\"token comment\">&#8212; \u6b63\u786e\u59ff\u52bf&#xff1a;\u91d1\u989d DECIMAL(10,2)&#xff0c;\u603b\u5171 10 \u4f4d\u3001\u5c0f\u6570 2 \u4f4d<\/span><br \/>\n<span class=\"token keyword\">SELECT<\/span> CAST<span class=\"token punctuation\">(<\/span><span class=\"token number\">0.1<\/span> <span class=\"token keyword\">AS<\/span> <span class=\"token keyword\">DECIMAL<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">10<\/span><span class=\"token punctuation\">,<\/span><span class=\"token number\">2<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&#043;<\/span> CAST<span class=\"token punctuation\">(<\/span><span class=\"token number\">0.2<\/span> <span class=\"token keyword\">AS<\/span> <span class=\"token keyword\">DECIMAL<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">10<\/span><span class=\"token punctuation\">,<\/span><span class=\"token number\">2<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>  <span class=\"token comment\">&#8212; 0.3<\/span><\/p>\n<p>DECIMAL \u662f\u7cbe\u786e\u5b58\u50a8&#xff0c;\u4ee3\u4ef7\u662f\u6bd4 DOUBLE \u5360\u7a7a\u95f4\u3001\u8ba1\u7b97\u6162\u3002\u8d85\u5927\u5e76\u53d1\u7684\u652f\u4ed8\u6838\u5fc3\u8868\u91cc&#xff0c;\u5e38\u89c1\u7684\u66ff\u4ee3\u65b9\u6848\u662f\u300c\u4ee5\u5206\u4e3a\u5355\u4f4d\u5b58 BIGINT\u300d&#xff0c;\u5728\u5e94\u7528\u5c42\u6362\u7b97\u3002<\/p>\n<h4>2.3 \u5b57\u7b26\u4e32&#xff1a;CHAR \/ VARCHAR \/ TEXT \/ ENUM<\/h4>\n<ul>\n<li>CHAR(N)&#xff1a;\u5b9a\u957f&#xff0c;\u4e0d\u8db3 N \u4f4d\u81ea\u52a8\u8865\u7a7a\u683c&#xff08;\u8bfb\u51fa\u65f6\u53bb\u6389&#xff09;\u3002\u9002\u5408\u957f\u5ea6\u6052\u5b9a\u7684\u503c&#xff1a;\u56fd\u5bb6\u7801 CHAR(2)\u3001MD5 \u503c CHAR(32)\u3002<\/li>\n<li>VARCHAR(N)&#xff1a;\u53d8\u957f&#xff0c;\u989d\u5916\u82b1 1~2 \u5b57\u8282\u8bb0\u5f55\u5b9e\u9645\u957f\u5ea6\u3002N \u662f\u300c\u5b57\u7b26\u6570\u300d&#xff0c;utf8mb4 \u4e0b\u4e00\u4e2a\u5b57\u7b26\u6700\u591a\u5360 4 \u5b57\u8282&#xff0c;\u6240\u4ee5\u5355\u5217\u7406\u8bba\u4e0a\u9650\u7ea6 16383 \u5b57\u7b26&#xff08;\u53d7\u884c\u5927\u5c0f 65535 \u5b57\u8282\u7ea6\u675f&#xff09;\u3002<\/li>\n<li>TEXT \/ BLOB&#xff1a;\u8d85\u957f\u5185\u5bb9\u6ea2\u51fa\u5230\u72ec\u7acb\u6570\u636e\u9875\u5b58\u50a8&#xff0c;\u67e5\u8be2\u4ee3\u4ef7\u9ad8\u3002\u786e\u6709\u9700\u6c42\u65f6\u5782\u76f4\u62c6\u5230\u9644\u8868&#xff0c;\u522b\u548c\u4e3b\u4e1a\u52a1\u5b57\u6bb5\u6df7\u5728\u4e00\u884c\u3002<\/li>\n<li>ENUM&#xff1a;\u6539\u4e00\u4e2a\u679a\u4e3e\u503c\u5c31\u8981 DDL\u3001\u6392\u5e8f\u6309\u5b9a\u4e49\u987a\u5e8f\u800c\u975e\u5b57\u5178\u5e8f&#xff0c;\u56e2\u961f\u89c4\u8303\u901a\u5e38\u7981\u7528\u2014\u2014\u7528 TINYINT &#043; \u5b57\u5178\u8868&#xff08;\u6216 COMMENT \u679a\u4e3e\u8bf4\u660e&#xff09;\u66ff\u4ee3\u3002<\/li>\n<\/ul>\n<p>\u5b9e\u6218\u6f14\u7ec3&#xff1a;IP \u5730\u5740\u5b58\u4ec0\u4e48\u7c7b\u578b&#xff1f;<\/p>\n<p><span class=\"token comment\">&#8212; \u65b9\u6848 A&#xff1a;VARCHAR(15)&#xff0c;\u53ef\u8bfb\u6027\u6700\u597d&#xff0c;\u901a\u7528\u4e1a\u52a1\u76f4\u63a5\u7528<\/span><br \/>\n<span class=\"token comment\">&#8212; \u65b9\u6848 B&#xff1a;INT UNSIGNED&#xff0c;\u7701 7 \u5b57\u8282&#xff0c;\u4e14\u80fd\u6309\u7f51\u6bb5\u8303\u56f4\u6bd4\u8f83&#xff08;\u5185\u7f51\/\u65e5\u5fd7\u7edf\u8ba1\u573a\u666f&#xff09;<\/span><br \/>\n<span class=\"token keyword\">SELECT<\/span> INET_ATON<span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;192.168.1.100&#039;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>  <span class=\"token comment\">&#8212; 3232235876&#xff08;\u70b9\u5206\u5341\u8fdb\u5236 \u2192 \u6574\u6570&#xff09;<\/span><br \/>\n<span class=\"token keyword\">SELECT<\/span> INET_NTOA<span class=\"token punctuation\">(<\/span><span class=\"token number\">3232235876<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>       <span class=\"token comment\">&#8212; &#039;192.168.1.100&#039;&#xff08;\u6574\u6570 \u2192 \u70b9\u5206\u5341\u8fdb\u5236&#xff09;<\/span><\/p>\n<p><span class=\"token comment\">&#8212; \u65b9\u6848 C&#xff1a;VARBINARY(16)&#xff0c;\u9700\u8981\u652f\u6301 IPv6 \u65f6\u7528 INET6_ATON\/INET6_NTOA<\/span><\/p>\n<h4>2.4 \u65f6\u95f4\u7c7b\u578b&#xff1a;DATETIME vs TIMESTAMP<\/h4>\n<table>\n<tr>\u7c7b\u578b\u5b57\u8282\u8303\u56f4\u65f6\u533a\u884c\u4e3a<\/tr>\n<tbody>\n<tr>\n<td>DATETIME<\/td>\n<td>8<\/td>\n<td>1000-01-01 ~ 9999-12-31<\/td>\n<td>\u65e0\u65f6\u533a\u8bed\u4e49&#xff1a;\u5199\u5165\u4ec0\u4e48\u8bfb\u51fa\u4ec0\u4e48<\/td>\n<\/tr>\n<tr>\n<td>TIMESTAMP<\/td>\n<td>4<\/td>\n<td>1970 ~ 2038-01-19&#xff08;2038 \u95ee\u9898&#xff09;<\/td>\n<td>\u5b58 UTC&#xff0c;\u8bfb\u5199\u6309\u4f1a\u8bdd\u65f6\u533a\u81ea\u52a8\u8f6c\u6362<\/td>\n<\/tr>\n<tr>\n<td>DATE<\/td>\n<td>3<\/td>\n<td>\u4ec5\u65e5\u671f<\/td>\n<td>\u2014<\/td>\n<\/tr>\n<tr>\n<td>TIME \/ YEAR<\/td>\n<td>3 \/ 1<\/td>\n<td>\u65f6\u95f4 \/ \u5e74\u4efd<\/td>\n<td>\u2014<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u63a8\u8350\u7ec4\u5408&#xff1a;DATETIME &#043; DEFAULT CURRENT_TIMESTAMP&#xff08;5.6.5 \u8d77 DATETIME \u4e5f\u652f\u6301\u8be5\u9ed8\u8ba4\u503c\u4e0e ON UPDATE CURRENT_TIMESTAMP&#xff09;&#xff0c;\u907f\u5f00 TIMESTAMP \u7684 2038 \u4e0a\u9650&#xff1b;\u8de8\u56fd\u4e1a\u52a1\u7edf\u4e00\u5b58 UTC&#xff0c;\u5c55\u793a\u5c42\u518d\u8f6c\u65f6\u533a\u3002<\/p>\n<h4>2.5 JSON \u4e0e\u751f\u6210\u5217&#xff08;8.0&#xff09;<\/h4>\n<p>8.0 \u7684 JSON \u4e0d\u662f\u7b80\u5355\u5b58\u4e2a\u5b57\u7b26\u4e32&#xff1a;\u5b83\u662f\u4e8c\u8fdb\u5236\u683c\u5f0f&#xff0c;\u80fd\u6309\u8def\u5f84\u67e5\u8be2\u3002\u4f46 JSON \u5217\u4e0d\u80fd\u76f4\u63a5\u5efa\u7d22\u5f15&#xff0c;\u5957\u8def\u662f\u300c\u751f\u6210\u5217 &#043; \u7d22\u5f15\u300d&#xff1a;<\/p>\n<p><span class=\"token keyword\">ALTER<\/span> <span class=\"token keyword\">TABLE<\/span> products <span class=\"token keyword\">ADD<\/span> <span class=\"token keyword\">COLUMN<\/span> attrs JSON <span class=\"token keyword\">COMMENT<\/span> <span class=\"token string\">&#039;\u6269\u5c55\u5c5e\u6027&#039;<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">&#8212; \u628a\u5e38\u7528\u7684 JSON \u5b57\u6bb5\u7269\u5316\u6210\u751f\u6210\u5217&#xff0c;\u518d\u5efa\u7d22\u5f15<\/span><br \/>\n<span class=\"token keyword\">ALTER<\/span> <span class=\"token keyword\">TABLE<\/span> products<br \/>\n  <span class=\"token keyword\">ADD<\/span> <span class=\"token keyword\">COLUMN<\/span> attrs_brand <span class=\"token keyword\">VARCHAR<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">50<\/span><span class=\"token punctuation\">)<\/span><br \/>\n    GENERATED ALWAYS <span class=\"token keyword\">AS<\/span> <span class=\"token punctuation\">(<\/span>JSON_UNQUOTE<span class=\"token punctuation\">(<\/span>attrs <span class=\"token operator\">&#8211;<\/span><span class=\"token operator\">&gt;&gt;<\/span> <span class=\"token string\">&#039;$.brand&#039;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span> STORED<span class=\"token punctuation\">,<\/span><br \/>\n  <span class=\"token keyword\">ADD<\/span> <span class=\"token keyword\">INDEX<\/span> idx_attrs_brand <span class=\"token punctuation\">(<\/span>attrs_brand<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token keyword\">SELECT<\/span> <span class=\"token operator\">*<\/span> <span class=\"token keyword\">FROM<\/span> products <span class=\"token keyword\">WHERE<\/span> attrs_brand <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#039;Apple&#039;<\/span><span class=\"token punctuation\">;<\/span>  <span class=\"token comment\">&#8212; \u80fd\u8d70\u7d22\u5f15\u4e86<\/span><\/p>\n<h4>2.6 \u5efa\u8868\u89c4\u8303\u6e05\u5355&#xff08;\u56e2\u961f\u901a\u7528\u7248&#xff09;<\/h4>\n<ul>\n<li>\u5b57\u6bb5\u5c3d\u91cf NOT NULL &#043; DEFAULT&#xff1a;NULL \u53c2\u4e0e\u6bd4\u8f83\u3001\u7edf\u8ba1\u3001\u7d22\u5f15\u90fd\u6709\u989d\u5916\u6210\u672c&#xff0c;COUNT(\u5b57\u6bb5) \u8fd8\u4f1a\u8df3\u8fc7 NULL \u884c&#xff1b;<\/li>\n<li>\u4e3b\u952e BIGINT UNSIGNED AUTO_INCREMENT&#xff0c;\u4fdd\u6301\u4e1a\u52a1\u65e0\u610f\u4e49&#xff1b;<\/li>\n<li>\u91d1\u989d DECIMAL(10,2)&#xff08;\u6216\u5206\u5355\u4f4d BIGINT&#xff09;&#xff0c;\u7981 FLOAT\/DOUBLE&#xff1b;<\/li>\n<li>\u5b57\u7b26\u96c6 utf8mb4\u2014\u2014MySQL \u7684 utf8 \u662f\u5386\u53f2\u9609\u5272\u7248&#xff08;utf8mb3&#xff09;&#xff0c;\u5b58\u4e0d\u4e86 emoji&#xff1b;<\/li>\n<li>\u6bcf\u8868\u6bcf\u5b57\u6bb5\u5199 COMMENT&#xff0c;\u534a\u5e74\u540e\u7684\u4f60\u4f1a\u611f\u8c22\u73b0\u5728\u7684\u4f60&#xff1b;<\/li>\n<li>created_at \/ updated_at \u4ea4\u7ed9\u9ed8\u8ba4\u503c\u81ea\u52a8\u7ef4\u62a4&#xff0c;\u5e94\u7528\u5c42\u5c11\u4e00\u6b21\u8d4b\u503c&#xff1b;<\/li>\n<li>\u5355\u8868\u5b57\u6bb5\u63a7\u5236\u5728 50 \u4e2a\u4ee5\u5185&#xff0c;TEXT \u5927\u5b57\u6bb5\u5782\u76f4\u62c6\u5206&#xff1b;<\/li>\n<li>\u4e0d\u5efa\u7269\u7406\u5916\u952e&#xff1a;\u5916\u952e\u5e26\u6765\u989d\u5916\u7684\u9501\u4e0e\u7ea7\u8054\u5f00\u9500\u3001DDL \u8026\u5408&#xff0c;\u5206\u5e93\u5206\u8868\u540e\u5b8c\u5168\u6ca1\u6cd5\u7528\u2014\u2014\u7528\u300c\u903b\u8f91\u5916\u952e &#043; \u5e94\u7528\u5c42\u4fdd\u8bc1\u300d\u3002<\/li>\n<\/ul>\n<p>\u8bb0\u5fc6\u951a\u70b9&#xff1a;\u7c7b\u578b\u9009\u578b\u4e00\u53e5\u8bdd\u2014\u2014\u80fd\u5c0f\u4e0d\u7528\u5927&#xff0c;\u80fd\u5b9a\u957f\u4e0d\u7528\u53d8\u957f&#xff0c;\u6570\u503c\u4f18\u4e8e\u5b57\u7b26\u4e32&#xff0c;NULL \u8d8a\u5c11\u8d8a\u597d\u3002\u7d22\u5f15\u5217\u5c24\u5176\u5982\u6b64&#xff1a;\u7c7b\u578b\u8d8a\u5c0f&#xff0c;\u4e00\u9875\u88c5\u5f97\u8d8a\u591a&#xff0c;\u6811\u8d8a\u77ee&#xff0c;\u67e5\u5f97\u8d8a\u5feb\u3002<\/p>\n<hr \/>\n<h3>\u4e09\u3001SQL \u6838\u5fc3\u8bed\u6cd5\u4e0e\u7535\u5546\u5efa\u5e93\u5b9e\u6218<\/h3>\n<p>\u4ece\u8fd9\u4e00\u7ae0\u5f00\u59cb\u52a8\u624b&#xff1a;\u6211\u4eec\u5efa\u4e00\u4e2a\u8d2f\u7a7f\u5168\u6587\u7684\u7535\u5546\u5e93 mall&#xff0c;\u540e\u9762\u6240\u6709\u7684\u7d22\u5f15\u3001\u4e8b\u52a1\u3001\u9501\u3001\u4f18\u5316\u6848\u4f8b\u90fd\u5728\u8fd9\u4e94\u5f20\u8868\u4e0a\u53d1\u751f\u3002\u8bed\u6cd5\u53ea\u8bb2\u300c\u7528\u4e86\u4f1a\u8e29\u5751\u300d\u7684\u90e8\u5206&#xff0c;\u624b\u518c\u4e0a\u67e5\u5f97\u5230\u7684\u7565\u8fc7\u3002<\/p>\n<h4>3.1 \u5efa\u5e93\u5efa\u8868&#xff1a;\u4e94\u5f20\u8868\u7684\u5b8c\u6574 DDL<\/h4>\n<p><span class=\"token keyword\">CREATE<\/span> <span class=\"token keyword\">DATABASE<\/span> mall <span class=\"token keyword\">DEFAULT<\/span> <span class=\"token keyword\">CHARACTER<\/span> <span class=\"token keyword\">SET<\/span> utf8mb4 <span class=\"token keyword\">COLLATE<\/span> utf8mb4_unicode_ci<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">USE<\/span> mall<span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token keyword\">CREATE<\/span> <span class=\"token keyword\">TABLE<\/span> users <span class=\"token punctuation\">(<\/span><br \/>\n  id         <span class=\"token keyword\">BIGINT<\/span> <span class=\"token keyword\">UNSIGNED<\/span> <span class=\"token keyword\">AUTO_INCREMENT<\/span> <span class=\"token keyword\">COMMENT<\/span> <span class=\"token string\">&#039;\u7528\u6237ID&#039;<\/span><span class=\"token punctuation\">,<\/span><br \/>\n  username   <span class=\"token keyword\">VARCHAR<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">50<\/span><span class=\"token punctuation\">)<\/span>  <span class=\"token operator\">NOT<\/span> <span class=\"token boolean\">NULL<\/span> <span class=\"token keyword\">COMMENT<\/span> <span class=\"token string\">&#039;\u7528\u6237\u540d&#039;<\/span><span class=\"token punctuation\">,<\/span><br \/>\n  email      <span class=\"token keyword\">VARCHAR<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">100<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">NOT<\/span> <span class=\"token boolean\">NULL<\/span> <span class=\"token keyword\">COMMENT<\/span> <span class=\"token string\">&#039;\u90ae\u7bb1&#039;<\/span><span class=\"token punctuation\">,<\/span><br \/>\n  <span class=\"token keyword\">status<\/span>     <span class=\"token keyword\">TINYINT<\/span> <span class=\"token keyword\">UNSIGNED<\/span> <span class=\"token operator\">NOT<\/span> <span class=\"token boolean\">NULL<\/span> <span class=\"token keyword\">DEFAULT<\/span> <span class=\"token number\">1<\/span> <span class=\"token keyword\">COMMENT<\/span> <span class=\"token string\">&#039;1\u6b63\u5e38 0\u7981\u7528&#039;<\/span><span class=\"token punctuation\">,<\/span><br \/>\n  created_at <span class=\"token keyword\">DATETIME<\/span> <span class=\"token operator\">NOT<\/span> <span class=\"token boolean\">NULL<\/span> <span class=\"token keyword\">DEFAULT<\/span> <span class=\"token keyword\">CURRENT_TIMESTAMP<\/span> <span class=\"token keyword\">COMMENT<\/span> <span class=\"token string\">&#039;\u6ce8\u518c\u65f6\u95f4&#039;<\/span><span class=\"token punctuation\">,<\/span><br \/>\n  updated_at <span class=\"token keyword\">DATETIME<\/span> <span class=\"token operator\">NOT<\/span> <span class=\"token boolean\">NULL<\/span> <span class=\"token keyword\">DEFAULT<\/span> <span class=\"token keyword\">CURRENT_TIMESTAMP<\/span> <span class=\"token keyword\">ON<\/span> <span class=\"token keyword\">UPDATE<\/span> <span class=\"token keyword\">CURRENT_TIMESTAMP<\/span><span class=\"token punctuation\">,<\/span><br \/>\n  <span class=\"token keyword\">PRIMARY<\/span> <span class=\"token keyword\">KEY<\/span> <span class=\"token punctuation\">(<\/span>id<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span><br \/>\n  <span class=\"token keyword\">UNIQUE<\/span> <span class=\"token keyword\">KEY<\/span> uk_username <span class=\"token punctuation\">(<\/span>username<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span><br \/>\n  <span class=\"token keyword\">UNIQUE<\/span> <span class=\"token keyword\">KEY<\/span> uk_email <span class=\"token punctuation\">(<\/span>email<span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">ENGINE<\/span><span class=\"token operator\">&#061;<\/span><span class=\"token keyword\">InnoDB<\/span> <span class=\"token keyword\">COMMENT<\/span><span class=\"token operator\">&#061;<\/span><span class=\"token string\">&#039;\u7528\u6237\u8868&#039;<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token keyword\">CREATE<\/span> <span class=\"token keyword\">TABLE<\/span> categories <span class=\"token punctuation\">(<\/span>                       <span class=\"token comment\">&#8212; \u6811\u5f62\u5206\u7c7b<\/span><br \/>\n  id        <span class=\"token keyword\">BIGINT<\/span> <span class=\"token keyword\">UNSIGNED<\/span> <span class=\"token keyword\">AUTO_INCREMENT<\/span> <span class=\"token keyword\">COMMENT<\/span> <span class=\"token string\">&#039;\u5206\u7c7bID&#039;<\/span><span class=\"token punctuation\">,<\/span><br \/>\n  name      <span class=\"token keyword\">VARCHAR<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">50<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">NOT<\/span> <span class=\"token boolean\">NULL<\/span> <span class=\"token keyword\">COMMENT<\/span> <span class=\"token string\">&#039;\u5206\u7c7b\u540d&#039;<\/span><span class=\"token punctuation\">,<\/span><br \/>\n  parent_id <span class=\"token keyword\">BIGINT<\/span> <span class=\"token keyword\">UNSIGNED<\/span> <span class=\"token operator\">NOT<\/span> <span class=\"token boolean\">NULL<\/span> <span class=\"token keyword\">DEFAULT<\/span> <span class=\"token number\">0<\/span> <span class=\"token keyword\">COMMENT<\/span> <span class=\"token string\">&#039;\u7236\u5206\u7c7bID&#xff0c;0\u4e3a\u9876\u7ea7&#039;<\/span><span class=\"token punctuation\">,<\/span><br \/>\n  sort      <span class=\"token keyword\">INT<\/span> <span class=\"token operator\">NOT<\/span> <span class=\"token boolean\">NULL<\/span> <span class=\"token keyword\">DEFAULT<\/span> <span class=\"token number\">0<\/span> <span class=\"token keyword\">COMMENT<\/span> <span class=\"token string\">&#039;\u6392\u5e8f\u6743\u91cd&#039;<\/span><span class=\"token punctuation\">,<\/span><br \/>\n  <span class=\"token keyword\">PRIMARY<\/span> <span class=\"token keyword\">KEY<\/span> <span class=\"token punctuation\">(<\/span>id<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span><br \/>\n  <span class=\"token keyword\">KEY<\/span> idx_parent <span class=\"token punctuation\">(<\/span>parent_id<span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">ENGINE<\/span><span class=\"token operator\">&#061;<\/span><span class=\"token keyword\">InnoDB<\/span> <span class=\"token keyword\">COMMENT<\/span><span class=\"token operator\">&#061;<\/span><span class=\"token string\">&#039;\u5546\u54c1\u5206\u7c7b\u8868&#039;<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token keyword\">CREATE<\/span> <span class=\"token keyword\">TABLE<\/span> products <span class=\"token punctuation\">(<\/span><br \/>\n  id          <span class=\"token keyword\">BIGINT<\/span> <span class=\"token keyword\">UNSIGNED<\/span> <span class=\"token keyword\">AUTO_INCREMENT<\/span> <span class=\"token keyword\">COMMENT<\/span> <span class=\"token string\">&#039;\u5546\u54c1ID&#039;<\/span><span class=\"token punctuation\">,<\/span><br \/>\n  category_id <span class=\"token keyword\">BIGINT<\/span> <span class=\"token keyword\">UNSIGNED<\/span> <span class=\"token operator\">NOT<\/span> <span class=\"token boolean\">NULL<\/span> <span class=\"token keyword\">COMMENT<\/span> <span class=\"token string\">&#039;\u5206\u7c7bID&#xff08;\u903b\u8f91\u5916\u952e&#xff09;&#039;<\/span><span class=\"token punctuation\">,<\/span><br \/>\n  name        <span class=\"token keyword\">VARCHAR<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">100<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">NOT<\/span> <span class=\"token boolean\">NULL<\/span> <span class=\"token keyword\">COMMENT<\/span> <span class=\"token string\">&#039;\u5546\u54c1\u540d&#039;<\/span><span class=\"token punctuation\">,<\/span><br \/>\n  price       <span class=\"token keyword\">DECIMAL<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">10<\/span><span class=\"token punctuation\">,<\/span><span class=\"token number\">2<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">NOT<\/span> <span class=\"token boolean\">NULL<\/span> <span class=\"token keyword\">COMMENT<\/span> <span class=\"token string\">&#039;\u552e\u4ef7&#039;<\/span><span class=\"token punctuation\">,<\/span><br \/>\n  stock       <span class=\"token keyword\">INT<\/span> <span class=\"token keyword\">UNSIGNED<\/span> <span class=\"token operator\">NOT<\/span> <span class=\"token boolean\">NULL<\/span> <span class=\"token keyword\">DEFAULT<\/span> <span class=\"token number\">0<\/span> <span class=\"token keyword\">COMMENT<\/span> <span class=\"token string\">&#039;\u5e93\u5b58&#039;<\/span><span class=\"token punctuation\">,<\/span><br \/>\n  sales       <span class=\"token keyword\">INT<\/span> <span class=\"token keyword\">UNSIGNED<\/span> <span class=\"token operator\">NOT<\/span> <span class=\"token boolean\">NULL<\/span> <span class=\"token keyword\">DEFAULT<\/span> <span class=\"token number\">0<\/span> <span class=\"token keyword\">COMMENT<\/span> <span class=\"token string\">&#039;\u7d2f\u8ba1\u9500\u91cf&#039;<\/span><span class=\"token punctuation\">,<\/span><br \/>\n  <span class=\"token keyword\">status<\/span>      <span class=\"token keyword\">TINYINT<\/span> <span class=\"token keyword\">UNSIGNED<\/span> <span class=\"token operator\">NOT<\/span> <span class=\"token boolean\">NULL<\/span> <span class=\"token keyword\">DEFAULT<\/span> <span class=\"token number\">1<\/span> <span class=\"token keyword\">COMMENT<\/span> <span class=\"token string\">&#039;1\u4e0a\u67b6 0\u4e0b\u67b6&#039;<\/span><span class=\"token punctuation\">,<\/span><br \/>\n  created_at  <span class=\"token keyword\">DATETIME<\/span> <span class=\"token operator\">NOT<\/span> <span class=\"token boolean\">NULL<\/span> <span class=\"token keyword\">DEFAULT<\/span> <span class=\"token keyword\">CURRENT_TIMESTAMP<\/span><span class=\"token punctuation\">,<\/span><br \/>\n  updated_at  <span class=\"token keyword\">DATETIME<\/span> <span class=\"token operator\">NOT<\/span> <span class=\"token boolean\">NULL<\/span> <span class=\"token keyword\">DEFAULT<\/span> <span class=\"token keyword\">CURRENT_TIMESTAMP<\/span> <span class=\"token keyword\">ON<\/span> <span class=\"token keyword\">UPDATE<\/span> <span class=\"token keyword\">CURRENT_TIMESTAMP<\/span><span class=\"token punctuation\">,<\/span><br \/>\n  <span class=\"token keyword\">PRIMARY<\/span> <span class=\"token keyword\">KEY<\/span> <span class=\"token punctuation\">(<\/span>id<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span><br \/>\n  <span class=\"token keyword\">KEY<\/span> idx_category <span class=\"token punctuation\">(<\/span>category_id<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span><br \/>\n  <span class=\"token keyword\">KEY<\/span> idx_status_created <span class=\"token punctuation\">(<\/span><span class=\"token keyword\">status<\/span><span class=\"token punctuation\">,<\/span> created_at<span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">ENGINE<\/span><span class=\"token operator\">&#061;<\/span><span class=\"token keyword\">InnoDB<\/span> <span class=\"token keyword\">COMMENT<\/span><span class=\"token operator\">&#061;<\/span><span class=\"token string\">&#039;\u5546\u54c1\u8868&#039;<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token keyword\">CREATE<\/span> <span class=\"token keyword\">TABLE<\/span> orders <span class=\"token punctuation\">(<\/span><br \/>\n  id           <span class=\"token keyword\">BIGINT<\/span> <span class=\"token keyword\">UNSIGNED<\/span> <span class=\"token keyword\">AUTO_INCREMENT<\/span> <span class=\"token keyword\">COMMENT<\/span> <span class=\"token string\">&#039;\u8ba2\u5355ID&#039;<\/span><span class=\"token punctuation\">,<\/span><br \/>\n  order_no     <span class=\"token keyword\">VARCHAR<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">32<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">NOT<\/span> <span class=\"token boolean\">NULL<\/span> <span class=\"token keyword\">COMMENT<\/span> <span class=\"token string\">&#039;\u4e1a\u52a1\u8ba2\u5355\u53f7&#039;<\/span><span class=\"token punctuation\">,<\/span><br \/>\n  user_id      <span class=\"token keyword\">BIGINT<\/span> <span class=\"token keyword\">UNSIGNED<\/span> <span class=\"token operator\">NOT<\/span> <span class=\"token boolean\">NULL<\/span> <span class=\"token keyword\">COMMENT<\/span> <span class=\"token string\">&#039;\u4e0b\u5355\u7528\u6237&#xff08;\u903b\u8f91\u5916\u952e&#xff09;&#039;<\/span><span class=\"token punctuation\">,<\/span><br \/>\n  total_amount <span class=\"token keyword\">DECIMAL<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">12<\/span><span class=\"token punctuation\">,<\/span><span class=\"token number\">2<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">NOT<\/span> <span class=\"token boolean\">NULL<\/span> <span class=\"token keyword\">COMMENT<\/span> <span class=\"token string\">&#039;\u8ba2\u5355\u603b\u989d&#039;<\/span><span class=\"token punctuation\">,<\/span><br \/>\n  <span class=\"token keyword\">status<\/span>       <span class=\"token keyword\">TINYINT<\/span> <span class=\"token keyword\">UNSIGNED<\/span> <span class=\"token operator\">NOT<\/span> <span class=\"token boolean\">NULL<\/span> <span class=\"token keyword\">DEFAULT<\/span> <span class=\"token number\">0<\/span> <span class=\"token keyword\">COMMENT<\/span> <span class=\"token string\">&#039;0\u5f85\u652f\u4ed8 1\u5df2\u652f\u4ed8 2\u5df2\u53d1\u8d27 3\u5df2\u5b8c\u6210 4\u5df2\u53d6\u6d88&#039;<\/span><span class=\"token punctuation\">,<\/span><br \/>\n  pay_time     <span class=\"token keyword\">DATETIME<\/span> <span class=\"token boolean\">NULL<\/span> <span class=\"token keyword\">COMMENT<\/span> <span class=\"token string\">&#039;\u652f\u4ed8\u65f6\u95f4&#039;<\/span><span class=\"token punctuation\">,<\/span><br \/>\n  created_at   <span class=\"token keyword\">DATETIME<\/span> <span class=\"token operator\">NOT<\/span> <span class=\"token boolean\">NULL<\/span> <span class=\"token keyword\">DEFAULT<\/span> <span class=\"token keyword\">CURRENT_TIMESTAMP<\/span> <span class=\"token keyword\">COMMENT<\/span> <span class=\"token string\">&#039;\u4e0b\u5355\u65f6\u95f4&#039;<\/span><span class=\"token punctuation\">,<\/span><br \/>\n  updated_at   <span class=\"token keyword\">DATETIME<\/span> <span class=\"token operator\">NOT<\/span> <span class=\"token boolean\">NULL<\/span> <span class=\"token keyword\">DEFAULT<\/span> <span class=\"token keyword\">CURRENT_TIMESTAMP<\/span> <span class=\"token keyword\">ON<\/span> <span class=\"token keyword\">UPDATE<\/span> <span class=\"token keyword\">CURRENT_TIMESTAMP<\/span><span class=\"token punctuation\">,<\/span><br \/>\n  <span class=\"token keyword\">PRIMARY<\/span> <span class=\"token keyword\">KEY<\/span> <span class=\"token punctuation\">(<\/span>id<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span><br \/>\n  <span class=\"token keyword\">UNIQUE<\/span> <span class=\"token keyword\">KEY<\/span> uk_order_no <span class=\"token punctuation\">(<\/span>order_no<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span><br \/>\n  <span class=\"token keyword\">KEY<\/span> idx_user_created <span class=\"token punctuation\">(<\/span>user_id<span class=\"token punctuation\">,<\/span> created_at<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span><br \/>\n  <span class=\"token keyword\">KEY<\/span> idx_status_created <span class=\"token punctuation\">(<\/span><span class=\"token keyword\">status<\/span><span class=\"token punctuation\">,<\/span> created_at<span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">ENGINE<\/span><span class=\"token operator\">&#061;<\/span><span class=\"token keyword\">InnoDB<\/span> <span class=\"token keyword\">COMMENT<\/span><span class=\"token operator\">&#061;<\/span><span class=\"token string\">&#039;\u8ba2\u5355\u8868&#039;<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token keyword\">CREATE<\/span> <span class=\"token keyword\">TABLE<\/span> order_items <span class=\"token punctuation\">(<\/span><br \/>\n  id         <span class=\"token keyword\">BIGINT<\/span> <span class=\"token keyword\">UNSIGNED<\/span> <span class=\"token keyword\">AUTO_INCREMENT<\/span> <span class=\"token keyword\">COMMENT<\/span> <span class=\"token string\">&#039;\u660e\u7ec6ID&#039;<\/span><span class=\"token punctuation\">,<\/span><br \/>\n  order_id   <span class=\"token keyword\">BIGINT<\/span> <span class=\"token keyword\">UNSIGNED<\/span> <span class=\"token operator\">NOT<\/span> <span class=\"token boolean\">NULL<\/span> <span class=\"token keyword\">COMMENT<\/span> <span class=\"token string\">&#039;\u8ba2\u5355ID&#xff08;\u903b\u8f91\u5916\u952e&#xff09;&#039;<\/span><span class=\"token punctuation\">,<\/span><br \/>\n  product_id <span class=\"token keyword\">BIGINT<\/span> <span class=\"token keyword\">UNSIGNED<\/span> <span class=\"token operator\">NOT<\/span> <span class=\"token boolean\">NULL<\/span> <span class=\"token keyword\">COMMENT<\/span> <span class=\"token string\">&#039;\u5546\u54c1ID&#xff08;\u903b\u8f91\u5916\u952e&#xff09;&#039;<\/span><span class=\"token punctuation\">,<\/span><br \/>\n  quantity   <span class=\"token keyword\">INT<\/span> <span class=\"token keyword\">UNSIGNED<\/span> <span class=\"token operator\">NOT<\/span> <span class=\"token boolean\">NULL<\/span> <span class=\"token keyword\">DEFAULT<\/span> <span class=\"token number\">1<\/span> <span class=\"token keyword\">COMMENT<\/span> <span class=\"token string\">&#039;\u8d2d\u4e70\u6570\u91cf&#039;<\/span><span class=\"token punctuation\">,<\/span><br \/>\n  price      <span class=\"token keyword\">DECIMAL<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">10<\/span><span class=\"token punctuation\">,<\/span><span class=\"token number\">2<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">NOT<\/span> <span class=\"token boolean\">NULL<\/span> <span class=\"token keyword\">COMMENT<\/span> <span class=\"token string\">&#039;\u6210\u4ea4\u5355\u4ef7&#xff08;\u4e0b\u5355\u65f6\u5feb\u7167&#xff09;&#039;<\/span><span class=\"token punctuation\">,<\/span><br \/>\n  <span class=\"token keyword\">PRIMARY<\/span> <span class=\"token keyword\">KEY<\/span> <span class=\"token punctuation\">(<\/span>id<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span><br \/>\n  <span class=\"token keyword\">KEY<\/span> idx_order <span class=\"token punctuation\">(<\/span>order_id<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span><br \/>\n  <span class=\"token keyword\">KEY<\/span> idx_product <span class=\"token punctuation\">(<\/span>product_id<span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">ENGINE<\/span><span class=\"token operator\">&#061;<\/span><span class=\"token keyword\">InnoDB<\/span> <span class=\"token keyword\">COMMENT<\/span><span class=\"token operator\">&#061;<\/span><span class=\"token string\">&#039;\u8ba2\u5355\u660e\u7ec6\u8868&#039;<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>\u4e09\u4e2a\u8bbe\u8ba1\u51b3\u7b56\u503c\u5f97\u505c\u4e0b\u6765\u60f3\u4e00\u60f3&#xff0c;\u5b83\u4eec\u662f\u6240\u6709\u4e1a\u52a1\u8868\u8bbe\u8ba1\u7684\u901a\u7528\u5957\u8def&#xff1a;<\/p>\n<ul>\n<li>order_items.price \u5b58\u300c\u5feb\u7167\u300d&#xff1a;\u5546\u54c1\u4ef7\u683c\u4f1a\u53d8&#xff0c;\u5386\u53f2\u8ba2\u5355\u5fc5\u987b\u8bb0\u5f53\u65f6\u7684\u6210\u4ea4\u4ef7&#xff0c;\u4e0d\u80fd JOIN products \u73b0\u4ef7\u6765\u7b97\u2014\u2014\u8d26\u5355\u4f1a\u9519&#xff1b;<\/li>\n<li>orders.total_amount \u5b58\u300c\u5197\u4f59\u300d&#xff1a;\u660e\u660e\u80fd SUM(\u660e\u7ec6) \u7b97\u51fa\u6765&#xff0c;\u4f46\u8ba2\u5355\u9875\u662f\u9ad8\u9891\u8bfb&#xff0c;\u5197\u4f59\u4e00\u5217\u6362\u53d6\u514d\u805a\u5408&#xff0c;\u8bfb\u591a\u5199\u5c11\u573a\u666f\u7684\u6807\u51c6\u53d6\u820d&#xff1b;<\/li>\n<li>\u53ea\u6709\u903b\u8f91\u5916\u952e&#xff1a;\u5b57\u6bb5\u4e0a\u52a0\u4e86\u7d22\u5f15&#xff08;\u5982 idx_user_created&#xff09;&#xff0c;\u4f46\u4e0d\u5efa FOREIGN KEY \u7ea6\u675f&#xff0c;\u7406\u7531\u89c1 2.6\u3002<\/li>\n<\/ul>\n<h4>3.2 DDL&#xff1a;ALTER \u7684\u5751\u4e0e 8.0 \u7684\u751c<\/h4>\n<p><span class=\"token keyword\">ALTER<\/span> <span class=\"token keyword\">TABLE<\/span> products <span class=\"token keyword\">ADD<\/span> <span class=\"token keyword\">COLUMN<\/span> subtitle <span class=\"token keyword\">VARCHAR<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">100<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">NOT<\/span> <span class=\"token boolean\">NULL<\/span> <span class=\"token keyword\">DEFAULT<\/span> <span class=\"token string\">&#039;&#039;<\/span> <span class=\"token keyword\">COMMENT<\/span> <span class=\"token string\">&#039;\u526f\u6807\u9898&#039;<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token comment\">&#8212; 8.0 \u52a0\u5217\u662f INSTANT \u7b97\u6cd5&#xff1a;\u53ea\u6539\u5143\u6570\u636e&#xff0c;\u5343\u4e07\u884c\u5927\u8868\u4e5f\u662f\u79d2\u7ea7<\/span><\/p>\n<p><span class=\"token keyword\">ALTER<\/span> <span class=\"token keyword\">TABLE<\/span> products <span class=\"token keyword\">ADD<\/span> <span class=\"token keyword\">INDEX<\/span> idx_name <span class=\"token punctuation\">(<\/span>name<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>  <span class=\"token comment\">&#8212; INPLACE&#xff0c;\u53ef\u5728\u7ebf\u52a0<\/span><br \/>\n<span class=\"token keyword\">ALTER<\/span> <span class=\"token keyword\">TABLE<\/span> products <span class=\"token keyword\">DROP<\/span> <span class=\"token keyword\">INDEX<\/span> idx_name<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">ALTER<\/span> <span class=\"token keyword\">TABLE<\/span> products<br \/>\n  <span class=\"token keyword\">MODIFY<\/span> <span class=\"token keyword\">COLUMN<\/span> subtitle <span class=\"token keyword\">VARCHAR<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">200<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">NOT<\/span> <span class=\"token boolean\">NULL<\/span> <span class=\"token keyword\">DEFAULT<\/span> <span class=\"token string\">&#039;&#039;<\/span> <span class=\"token keyword\">COMMENT<\/span> <span class=\"token string\">&#039;\u526f\u6807\u9898&#039;<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token comment\">&#8212; \u6ce8\u610f&#xff1a;\u6539\u5217\u7c7b\u578b\/\u957f\u5ea6\u65e0\u6cd5 INSTANT&#xff0c;\u5927\u8868\u4f1a\u9501\u6216\u957f\u65f6\u95f4 INPLACE&#xff0c;\u52a1\u5fc5\u4f4e\u5cf0\u64cd\u4f5c<\/span><\/p>\n<p>\u6613\u9519\u70b9&#xff1a;\u7ebf\u4e0a ALTER \u524d\u5148\u770b\u7b97\u6cd5\u3002 \u6267\u884c\u524d\u7528 ALTER TABLE &#8230; , ALGORITHM&#061;INSTANT \u8bd5\u63a2&#xff1a;\u652f\u6301\u5c31\u79d2\u56de&#xff1b;\u4e0d\u652f\u6301\u4f1a\u76f4\u63a5\u62a5\u9519\u800c\u4e0d\u662f\u9ed8\u9ed8\u9501\u8868\u3002\u5927\u8868\u53d8\u66f4\u63a8\u8350 gh-ost \/ pt-online-schema-change \u5de5\u5177\u3002<\/p>\n<h4>3.3 DML&#xff1a;\u589e\u5220\u6539\u7684\u56db\u4e2a\u7ec6\u8282<\/h4>\n<p><span class=\"token comment\">&#8212; \u2460 \u6279\u91cf\u63d2\u5165&#xff1a;\u4e00\u6761 SQL \u63d2\u591a\u884c&#xff0c;\u6bd4\u5faa\u73af\u5355\u63d2\u5feb\u4e00\u4e2a\u6570\u91cf\u7ea7<\/span><br \/>\n<span class=\"token keyword\">INSERT<\/span> <span class=\"token keyword\">INTO<\/span> users <span class=\"token punctuation\">(<\/span>username<span class=\"token punctuation\">,<\/span> email<span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">VALUES<\/span><br \/>\n  <span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;alice&#039;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#039;alice&#064;test.com&#039;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span><br \/>\n  <span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;bob&#039;<\/span><span class=\"token punctuation\">,<\/span>   <span class=\"token string\">&#039;bob&#064;test.com&#039;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">&#8212; \u2461 \u5b58\u5728\u5219\u66f4\u65b0&#xff08;8.0.20&#043; \u884c\u522b\u540d\u5199\u6cd5&#xff0c;VALUES() \u5199\u6cd5\u5df2\u5e9f\u5f03&#xff09;<\/span><br \/>\n<span class=\"token keyword\">INSERT<\/span> <span class=\"token keyword\">INTO<\/span> products <span class=\"token punctuation\">(<\/span>id<span class=\"token punctuation\">,<\/span> name<span class=\"token punctuation\">,<\/span> price<span class=\"token punctuation\">,<\/span> stock<span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">VALUES<\/span><br \/>\n  <span class=\"token punctuation\">(<\/span><span class=\"token number\">1<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#039;iPhone 17&#039;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">6999.00<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">100<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">AS<\/span> new<br \/>\n<span class=\"token keyword\">ON<\/span> <span class=\"token keyword\">DUPLICATE<\/span> <span class=\"token keyword\">KEY<\/span> <span class=\"token keyword\">UPDATE<\/span> stock <span class=\"token operator\">&#061;<\/span> stock <span class=\"token operator\">&#043;<\/span> new<span class=\"token punctuation\">.<\/span>stock<span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">&#8212; \u2462 UPDATE \u6761\u4ef6\u5199\u5728 WHERE \u91cc&#xff0c;\u987a\u624b\u9632\u8d85\u5356&#xff08;\u7b2c 7 \u7ae0\u5c55\u5f00&#xff09;<\/span><br \/>\n<span class=\"token keyword\">UPDATE<\/span> products <span class=\"token keyword\">SET<\/span> stock <span class=\"token operator\">&#061;<\/span> stock <span class=\"token operator\">&#8211;<\/span> <span class=\"token number\">1<\/span> <span class=\"token keyword\">WHERE<\/span> id <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">1<\/span> <span class=\"token operator\">AND<\/span> stock <span class=\"token operator\">&gt;&#061;<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">&#8212; \u2463 \u5b89\u5168\u5e26&#xff1a;\u5f00\u542f\u540e&#xff0c;\u6ca1\u5199 WHERE \u6216 WHERE \u6ca1\u8d70\u7d22\u5f15\u5217\u7684 UPDATE\/DELETE \u76f4\u63a5\u62a5\u9519<\/span><br \/>\n<span class=\"token keyword\">SET<\/span> SQL_SAFE_UPDATES <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<table>\n<tr>\u7ef4\u5ea6DELETETRUNCATE<\/tr>\n<tbody>\n<tr>\n<td>WHERE \u6761\u4ef6<\/td>\n<td>\u652f\u6301&#xff0c;\u9010\u884c\u5220<\/td>\n<td>\u4e0d\u652f\u6301&#xff0c;\u6e05\u7a7a\u5168\u8868<\/td>\n<\/tr>\n<tr>\n<td>\u4e8b\u52a1\u56de\u6eda<\/td>\n<td>\u53ef\u4ee5&#xff08;\u8bb0\u5f55 undo&#xff09;<\/td>\n<td>\u5c5e\u4e8e DDL&#xff0c;\u4e0d\u53ef\u56de\u6eda<\/td>\n<\/tr>\n<tr>\n<td>\u81ea\u589e\u503c<\/td>\n<td>\u4fdd\u7559<\/td>\n<td>\u91cd\u7f6e\u4e3a 1<\/td>\n<\/tr>\n<tr>\n<td>\u89e6\u53d1\u5668<\/td>\n<td>\u89e6\u53d1 DELETE \u89e6\u53d1\u5668<\/td>\n<td>\u4e0d\u89e6\u53d1<\/td>\n<\/tr>\n<tr>\n<td>\u901f\u5ea6<\/td>\n<td>\u6162&#xff08;\u9010\u884c &#043; binlog&#xff09;<\/td>\n<td>\u5feb&#xff08;\u91cd\u5efa\u8868&#xff09;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>3.4 DCL&#xff1a;\u7ed9\u5e94\u7528\u8d26\u53f7\u6700\u5c0f\u6743\u9650<\/h4>\n<p><span class=\"token keyword\">CREATE<\/span> <span class=\"token keyword\">USER<\/span> <span class=\"token string\">&#039;mall_app&#039;<\/span><span class=\"token variable\">&#064;&#039;192.168.1.%&#039;<\/span> IDENTIFIED <span class=\"token keyword\">BY<\/span> <span class=\"token string\">&#039;\u8bf7\u7528\u5f3a\u5bc6\u7801&#039;<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">GRANT<\/span> <span class=\"token keyword\">SELECT<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">INSERT<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">UPDATE<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">DELETE<\/span> <span class=\"token keyword\">ON<\/span> mall<span class=\"token punctuation\">.<\/span><span class=\"token operator\">*<\/span> <span class=\"token keyword\">TO<\/span> <span class=\"token string\">&#039;mall_app&#039;<\/span><span class=\"token variable\">&#064;&#039;192.168.1.%&#039;<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">SHOW<\/span> GRANTS <span class=\"token keyword\">FOR<\/span> <span class=\"token string\">&#039;mall_app&#039;<\/span><span class=\"token variable\">&#064;&#039;192.168.1.%&#039;<\/span><span class=\"token punctuation\">;<\/span>      <span class=\"token comment\">&#8212; \u68c0\u67e5\u6388\u4e88\u7ed3\u679c<\/span><br \/>\n<span class=\"token keyword\">REVOKE<\/span> <span class=\"token keyword\">DELETE<\/span> <span class=\"token keyword\">ON<\/span> mall<span class=\"token punctuation\">.<\/span><span class=\"token operator\">*<\/span> <span class=\"token keyword\">FROM<\/span> <span class=\"token string\">&#039;mall_app&#039;<\/span><span class=\"token variable\">&#064;&#039;192.168.1.%&#039;<\/span><span class=\"token punctuation\">;<\/span>  <span class=\"token comment\">&#8212; \u6536\u56de\u8bef\u6388\u6743\u9650<\/span><\/p>\n<ul>\n<li>\u5e94\u7528\u8d26\u53f7\u53ea\u7ed9\u8868\u7684\u589e\u5220\u6539\u67e5&#xff0c;\u6c38\u8fdc\u4e0d\u7ed9 DROP \/ ALTER \/ GRANT&#xff1b;<\/li>\n<li>\u62a5\u8868\/\u770b\u677f\u8d26\u53f7\u7ed9\u53ea\u8bfb GRANT SELECT&#xff1b;<\/li>\n<li>GRANT\/REVOKE \u7acb\u5373\u751f\u6548&#xff0c;\u4e0d\u9700\u8981 FLUSH PRIVILEGES&#xff08;\u90a3\u662f\u76f4\u63a5\u6539 mysql.user \u8868\u65f6\u7684\u8001\u89c4\u77e9&#xff09;&#xff1b;<\/li>\n<li>\u8d26\u53f7\u6309\u300c\u7528\u9014 &#043; \u6765\u6e90\u7f51\u6bb5\u300d\u6536\u7d27&#xff1a;mall_app \u53ea\u5141\u8bb8\u4ece\u5e94\u7528\u7f51\u6bb5\u8fde\u3002<\/li>\n<\/ul>\n<h4>3.5 \u5e38\u7528\u51fd\u6570\u901f\u67e5<\/h4>\n<table>\n<tr>\u7c7b\u522b\u51fd\u6570\u4e00\u53e5\u8bdd\u8bf4\u660e<\/tr>\n<tbody>\n<tr>\n<td>\u5b57\u7b26\u4e32<\/td>\n<td>CONCAT \/ SUBSTRING \/ UPPER \/ TRIM<\/td>\n<td>\u62fc\u63a5\u3001\u622a\u53d6\u3001\u5927\u5c0f\u5199\u3001\u53bb\u7a7a\u683c<\/td>\n<\/tr>\n<tr>\n<td>\u65e5\u671f<\/td>\n<td>NOW() \/ DATE_FORMAT \/ DATE_ADD \/ DATEDIFF<\/td>\n<td>\u5f53\u524d\u65f6\u95f4\u3001\u683c\u5f0f\u5316\u3001\u504f\u79fb\u3001\u5dee\u503c<\/td>\n<\/tr>\n<tr>\n<td>\u63a7\u5236\u6d41<\/td>\n<td>IFNULL \/ CASE WHEN<\/td>\n<td>NULL \u515c\u5e95\u3001\u591a\u6761\u4ef6\u5206\u652f<\/td>\n<\/tr>\n<tr>\n<td>\u805a\u5408<\/td>\n<td>COUNT \/ SUM \/ AVG \/ MIN \/ MAX<\/td>\n<td>\u7b2c 4 \u7ae0\u914d\u5408 GROUP BY \u4f7f\u7528<\/td>\n<\/tr>\n<tr>\n<td>\u6570\u503c<\/td>\n<td>ROUND \/ CEIL \/ FLOOR \/ ABS<\/td>\n<td>\u56db\u820d\u4e94\u5165\u3001\u4e0a\u4e0b\u53d6\u6574\u3001\u7edd\u5bf9\u503c<\/td>\n<\/tr>\n<tr>\n<td>JSON<\/td>\n<td>-&gt; \/ -&gt;&gt; \/ JSON_EXTRACT<\/td>\n<td>\u8def\u5f84\u53d6\u503c&#xff0c;-&gt;&gt; \u987a\u5e26\u53bb\u5f15\u53f7<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span class=\"token keyword\">SELECT<\/span> DATE_FORMAT<span class=\"token punctuation\">(<\/span><span class=\"token function\">NOW<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#039;%Y-%m-%d %H:%i&#039;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>   <span class=\"token comment\">&#8212; &#039;2026-09-25 14:30&#039;<\/span><br \/>\n<span class=\"token keyword\">SELECT<\/span> DATE_ADD<span class=\"token punctuation\">(<\/span><span class=\"token function\">NOW<\/span><span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">INTERVAL<\/span> <span class=\"token number\">7<\/span> <span class=\"token keyword\">DAY<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>        <span class=\"token comment\">&#8212; \u4e00\u5468\u540e<\/span><br \/>\n<span class=\"token keyword\">SELECT<\/span> DATEDIFF<span class=\"token punctuation\">(<\/span><span class=\"token string\">&#039;2026-09-25&#039;<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#039;2026-09-01&#039;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>   <span class=\"token comment\">&#8212; 24&#xff08;\u53ea\u6309\u65e5\u671f\u7b97\u5929\u6570&#xff09;<\/span><br \/>\n<span class=\"token keyword\">SELECT<\/span> IFNULL<span class=\"token punctuation\">(<\/span><span class=\"token boolean\">NULL<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#039;\u9ed8\u8ba4\u503c&#039;<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span>                  <span class=\"token comment\">&#8212; &#039;\u9ed8\u8ba4\u503c&#039;<\/span><br \/>\n<span class=\"token keyword\">SELECT<\/span> <span class=\"token keyword\">CASE<\/span> <span class=\"token keyword\">WHEN<\/span> <span class=\"token keyword\">status<\/span> <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">1<\/span> <span class=\"token keyword\">THEN<\/span> <span class=\"token string\">&#039;\u6b63\u5e38&#039;<\/span> <span class=\"token keyword\">ELSE<\/span> <span class=\"token string\">&#039;\u7981\u7528&#039;<\/span> <span class=\"token keyword\">END<\/span> <span class=\"token keyword\">FROM<\/span> users<span class=\"token punctuation\">;<\/span><\/p>\n<hr \/>\n<h3>\u56db\u3001\u67e5\u8be2\u8fdb\u9636&#xff1a;JOIN\u3001\u7a97\u53e3\u51fd\u6570\u4e0e\u7ecf\u5178\u4e1a\u52a1\u9898<\/h3>\n<p>\u4e1a\u52a1 SQL \u7684\u590d\u6742\u5ea6 80% \u6765\u81ea\u300c\u591a\u8868 &#043; \u5206\u7ec4 &#043; \u6392\u540d\u300d\u3002\u8fd9\u4e00\u7ae0\u628a JOIN\u3001\u5b50\u67e5\u8be2\u3001\u5206\u7ec4\u3001\u7a97\u53e3\u51fd\u6570\u4e00\u53e3\u6c14\u8fc7\u5b8c&#xff0c;\u6700\u540e\u7528\u56db\u9053\u7ecf\u5178\u4e1a\u52a1\u9898\u6536\u5c3e\u2014\u2014\u6bcf\u4e00\u9053\u90fd\u662f\u9762\u8bd5\u548c\u5b9e\u6218\u7684\u9ad8\u9891\u5ba2\u3002<\/p>\n<h4>4.1 JOIN&#xff1a;INNER \u548c LEFT \u5c31\u591f\u7528\u4e86<\/h4>\n<p><span class=\"token comment\">&#8212; INNER JOIN&#xff1a;\u53d6\u4ea4\u96c6\u3002\u5df2\u652f\u4ed8\u8ba2\u5355 &#043; \u4e0b\u5355\u7528\u6237\u540d<\/span><br \/>\n<span class=\"token keyword\">SELECT<\/span> o<span class=\"token punctuation\">.<\/span>order_no<span class=\"token punctuation\">,<\/span> u<span class=\"token punctuation\">.<\/span>username<span class=\"token punctuation\">,<\/span> o<span class=\"token punctuation\">.<\/span>total_amount<br \/>\n<span class=\"token keyword\">FROM<\/span> orders o<br \/>\n<span class=\"token keyword\">INNER<\/span> <span class=\"token keyword\">JOIN<\/span> users u <span class=\"token keyword\">ON<\/span> o<span class=\"token punctuation\">.<\/span>user_id <span class=\"token operator\">&#061;<\/span> u<span class=\"token punctuation\">.<\/span>id<br \/>\n<span class=\"token keyword\">WHERE<\/span> o<span class=\"token punctuation\">.<\/span><span class=\"token keyword\">status<\/span> <span class=\"token operator\">&gt;&#061;<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">&#8212; LEFT JOIN&#xff1a;\u4fdd\u7559\u5de6\u8868\u5168\u90e8\u3002\u6240\u6709\u5546\u54c1\u5404\u81ea\u7684\u9500\u91cf&#xff08;\u5305\u62ec\u4e00\u4ef6\u6ca1\u5356\u51fa\u53bb\u7684&#xff09;<\/span><br \/>\n<span class=\"token keyword\">SELECT<\/span> p<span class=\"token punctuation\">.<\/span>id<span class=\"token punctuation\">,<\/span> p<span class=\"token punctuation\">.<\/span>name<span class=\"token punctuation\">,<\/span> IFNULL<span class=\"token punctuation\">(<\/span><span class=\"token function\">SUM<\/span><span class=\"token punctuation\">(<\/span>oi<span class=\"token punctuation\">.<\/span>quantity<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">0<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">AS<\/span> sold<br \/>\n<span class=\"token keyword\">FROM<\/span> products p<br \/>\n<span class=\"token keyword\">LEFT<\/span> <span class=\"token keyword\">JOIN<\/span> order_items oi <span class=\"token keyword\">ON<\/span> oi<span class=\"token punctuation\">.<\/span>product_id <span class=\"token operator\">&#061;<\/span> p<span class=\"token punctuation\">.<\/span>id<br \/>\n<span class=\"token keyword\">GROUP<\/span> <span class=\"token keyword\">BY<\/span> p<span class=\"token punctuation\">.<\/span>id<span class=\"token punctuation\">,<\/span> p<span class=\"token punctuation\">.<\/span>name<span class=\"token punctuation\">;<\/span><\/p>\n<p>RIGHT JOIN \u5728\u5b9e\u9645\u4ee3\u7801\u91cc\u51e0\u4e4e\u4e0d\u51fa\u73b0\u2014\u2014\u628a\u4e24\u5f20\u8868\u6362\u4e2a\u4f4d\u7f6e\u5199 LEFT \u5c31\u884c\u3002\u771f\u6b63\u5bb9\u6613\u51fa\u9519\u7684\u662f\u4e0b\u9762\u8fd9\u4e2a\u7ec6\u8282&#xff1a;<\/p>\n<p>\u6613\u9519\u70b9&#xff1a;LEFT JOIN \u7684\u8fc7\u6ee4\u6761\u4ef6\u5199 ON \u8fd8\u662f WHERE&#xff1f;<\/p>\n<p>\u5bf9\u53f3\u8868\u7684\u8fc7\u6ee4\u6761\u4ef6\u5199\u5728 ON \u91cc&#xff1a;\u5de6\u8868\u884c\u5168\u4fdd\u7559&#xff0c;\u4e0d\u5339\u914d\u7684\u8865 NULL&#xff08;\u300c\u6bcf\u4e2a\u5546\u54c1\u53ca\u5176\u9ad8\u4ef7\u8ba2\u5355&#xff0c;\u6ca1\u5356\u8fc7\u9ad8\u4ef7\u7684\u663e\u793a NULL\u300d&#xff09;\u3002\u5199\u5728 WHERE \u91cc&#xff1a;NULL \u884c\u4f1a\u88ab\u8fc7\u6ee4\u6389&#xff0c;LEFT JOIN \u6084\u6084\u9000\u5316\u6210 INNER JOIN\u3002\u8fd9\u662f\u7edf\u8ba1 SQL \u51fa\u300c\u6570\u636e\u53d8\u5c11\u300dbug \u7684\u5934\u53f7\u539f\u56e0\u3002<\/p>\n<p><span class=\"token comment\">&#8212; ON \u8fc7\u6ee4\u53f3\u8868&#xff1a;\u4fdd\u7559\u6240\u6709\u5546\u54c1&#xff08;\u6b63\u786e\u59ff\u52bf&#xff09;<\/span><br \/>\n<span class=\"token keyword\">FROM<\/span> products p <span class=\"token keyword\">LEFT<\/span> <span class=\"token keyword\">JOIN<\/span> order_items oi<br \/>\n  <span class=\"token keyword\">ON<\/span> oi<span class=\"token punctuation\">.<\/span>product_id <span class=\"token operator\">&#061;<\/span> p<span class=\"token punctuation\">.<\/span>id <span class=\"token operator\">AND<\/span> oi<span class=\"token punctuation\">.<\/span>price <span class=\"token operator\">&gt;<\/span> <span class=\"token number\">100<\/span><\/p>\n<p><span class=\"token comment\">&#8212; WHERE \u8fc7\u6ee4&#xff1a;\u5356\u4e0d\u51fa\u9ad8\u4ef7\u7684\u5546\u54c1\u76f4\u63a5\u6d88\u5931&#xff08;LEFT \u767d\u5199\u4e86&#xff09;<\/span><br \/>\n<span class=\"token keyword\">FROM<\/span> products p <span class=\"token keyword\">LEFT<\/span> <span class=\"token keyword\">JOIN<\/span> order_items oi<br \/>\n  <span class=\"token keyword\">ON<\/span> oi<span class=\"token punctuation\">.<\/span>product_id <span class=\"token operator\">&#061;<\/span> p<span class=\"token punctuation\">.<\/span>id<br \/>\n<span class=\"token keyword\">WHERE<\/span> oi<span class=\"token punctuation\">.<\/span>price <span class=\"token operator\">&gt;<\/span> <span class=\"token number\">100<\/span><\/p>\n<h4>4.2 \u5b50\u67e5\u8be2&#xff1a;IN \u8fd8\u662f EXISTS<\/h4>\n<p><span class=\"token comment\">&#8212; \u4e0b\u8fc7\u5355\u7684\u7528\u6237&#xff1a;IN \u5199\u6cd5<\/span><br \/>\n<span class=\"token keyword\">SELECT<\/span> <span class=\"token operator\">*<\/span> <span class=\"token keyword\">FROM<\/span> users u<br \/>\n<span class=\"token keyword\">WHERE<\/span> u<span class=\"token punctuation\">.<\/span>id <span class=\"token operator\">IN<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token keyword\">SELECT<\/span> user_id <span class=\"token keyword\">FROM<\/span> orders<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">&#8212; EXISTS \u5199\u6cd5<\/span><br \/>\n<span class=\"token keyword\">SELECT<\/span> <span class=\"token operator\">*<\/span> <span class=\"token keyword\">FROM<\/span> users u<br \/>\n<span class=\"token keyword\">WHERE<\/span> <span class=\"token keyword\">EXISTS<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token keyword\">SELECT<\/span> <span class=\"token number\">1<\/span> <span class=\"token keyword\">FROM<\/span> orders o <span class=\"token keyword\">WHERE<\/span> o<span class=\"token punctuation\">.<\/span>user_id <span class=\"token operator\">&#061;<\/span> u<span class=\"token punctuation\">.<\/span>id<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>\u7ecf\u5178\u7ed3\u8bba\u662f**\u300c\u5c0f\u8868\u9a71\u52a8\u5927\u8868\u300d**&#xff1a;\u5b50\u67e5\u8be2\u7ed3\u679c\u96c6\u5c0f\u7528 IN&#xff08;\u5916\u5c42\u5927\u8868\u88ab\u5c0f\u96c6\u5408\u8fc7\u6ee4&#xff09;&#xff1b;\u5916\u8868\u5c0f\u3001\u5185\u5c42\u5927\u4e14\u5185\u5c42\u8fde\u63a5\u5217\u6709\u7d22\u5f15\u65f6 EXISTS \u66f4\u5feb&#xff08;\u5916\u8868\u6bcf\u884c\u53bb\u5185\u5c42\u7d22\u5f15\u63a2\u6d4b\u4e00\u6b21&#xff09;\u30028.0 \u7684\u4f18\u5316\u5668\u5728\u591a\u6570\u573a\u666f\u4f1a\u81ea\u52a8\u6539\u5199\u4e24\u8005&#xff0c;\u4f18\u5148\u4fdd\u8bc1\u8bed\u4e49\u6e05\u6670&#xff0c;\u522b\u4e3a\u60f3\u8c61\u4e2d\u7684\u6027\u80fd\u727a\u7272\u53ef\u8bfb\u6027\u3002<\/p>\n<h4>4.3 GROUP BY \u4e0e HAVING&#xff1a;\u9500\u552e\u65e5\u62a5<\/h4>\n<p><span class=\"token keyword\">SELECT<\/span> <span class=\"token keyword\">DATE<\/span><span class=\"token punctuation\">(<\/span>o<span class=\"token punctuation\">.<\/span>pay_time<span class=\"token punctuation\">)<\/span>              <span class=\"token keyword\">AS<\/span> <span class=\"token keyword\">day<\/span><span class=\"token punctuation\">,<\/span><br \/>\n       <span class=\"token function\">COUNT<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">*<\/span><span class=\"token punctuation\">)<\/span>                      <span class=\"token keyword\">AS<\/span> order_cnt<span class=\"token punctuation\">,<\/span><br \/>\n       <span class=\"token function\">SUM<\/span><span class=\"token punctuation\">(<\/span>o<span class=\"token punctuation\">.<\/span>total_amount<span class=\"token punctuation\">)<\/span>           <span class=\"token keyword\">AS<\/span> gmv<span class=\"token punctuation\">,<\/span><br \/>\n       <span class=\"token function\">ROUND<\/span><span class=\"token punctuation\">(<\/span><span class=\"token function\">AVG<\/span><span class=\"token punctuation\">(<\/span>o<span class=\"token punctuation\">.<\/span>total_amount<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">2<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">AS<\/span> avg_amount<br \/>\n<span class=\"token keyword\">FROM<\/span> orders o<br \/>\n<span class=\"token keyword\">WHERE<\/span> o<span class=\"token punctuation\">.<\/span><span class=\"token keyword\">status<\/span> <span class=\"token operator\">&gt;&#061;<\/span> <span class=\"token number\">1<\/span>                  <span class=\"token comment\">&#8212; \u53ea\u7edf\u8ba1\u6709\u6548\u8ba2\u5355<\/span><br \/>\n<span class=\"token keyword\">GROUP<\/span> <span class=\"token keyword\">BY<\/span> <span class=\"token keyword\">DATE<\/span><span class=\"token punctuation\">(<\/span>o<span class=\"token punctuation\">.<\/span>pay_time<span class=\"token punctuation\">)<\/span>            <span class=\"token comment\">&#8212; \u6309\u652f\u4ed8\u65e5\u5206\u7ec4<\/span><br \/>\n<span class=\"token keyword\">HAVING<\/span> <span class=\"token function\">SUM<\/span><span class=\"token punctuation\">(<\/span>o<span class=\"token punctuation\">.<\/span>total_amount<span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&gt;<\/span> <span class=\"token number\">10000<\/span>   <span class=\"token comment\">&#8212; \u53ea\u8981 GMV \u8fc7\u4e07\u7684\u65e5\u5b50<\/span><br \/>\n<span class=\"token keyword\">ORDER<\/span> <span class=\"token keyword\">BY<\/span> <span class=\"token keyword\">day<\/span> <span class=\"token keyword\">DESC<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>\u8bb0\u5fc6\u951a\u70b9&#xff1a;\u4e00\u6761 SELECT \u7684\u6267\u884c\u987a\u5e8f\u2014\u2014FROM \u2192 WHERE \u2192 GROUP BY \u2192 HAVING \u2192 SELECT \u2192 ORDER BY \u2192 LIMIT\u3002 WHERE \u8fc7\u6ee4\u300c\u884c\u300d&#xff08;\u4e0d\u80fd\u7528\u805a\u5408\u51fd\u6570&#xff09;&#xff0c;HAVING \u8fc7\u6ee4\u300c\u7ec4\u300d&#xff08;\u53ef\u4ee5\u7528\u805a\u5408\u51fd\u6570&#xff09;\u3002\u8fd9\u4e5f\u662f\u4e3a\u4ec0\u4e48 SELECT \u91cc\u8d77\u7684\u522b\u540d\u5728 WHERE \u91cc\u7528\u4e0d\u4e86\u3001\u5728 ORDER BY \u91cc\u80fd\u7528\u3002<\/p>\n<p>\u6613\u9519\u70b9&#xff1a;ONLY_FULL_GROUP_BY\u3002 8.0 \u9ed8\u8ba4\u5f00\u542f\u8be5 sql_mode&#xff1a;SELECT \u91cc\u51fa\u73b0\u7684\u975e\u805a\u5408\u5217\u5fc5\u987b\u51fa\u73b0\u5728 GROUP BY \u91cc&#xff0c;\u5426\u5219\u62a5 1055 \u9519\u8bef\u3002\u522b\u6025\u7740\u5173\u5b83\u2014\u2014\u5b83\u9632\u7684\u662f\u300c\u4e00\u884c\u7ed3\u679c\u6df7\u5165\u522b\u7684\u884c\u7684\u503c\u300d\u8fd9\u79cd\u903b\u8f91\u70b8\u5f39\u3002<\/p>\n<h4>4.4 \u7a97\u53e3\u51fd\u6570&#xff08;8.0&#xff09;&#xff1a;\u6392\u540d\u795e\u5668<\/h4>\n<p>\u7a97\u53e3\u51fd\u6570 &#061; \u300c\u4e0d\u5408\u5e76\u884c\u7684 GROUP BY\u300d&#xff1a;\u5206\u7ec4\u805a\u5408\u4f1a\u628a N \u884c\u538b\u6210 1 \u884c&#xff0c;\u7a97\u53e3\u51fd\u6570\u5728\u6bcf\u884c\u65c1\u8fb9\u9644\u4e0a\u4e00\u4e2a\u8ba1\u7b97\u7ed3\u679c\u3002\u5148\u8ba4\u4e09\u5144\u5f1f&#xff1a;<\/p>\n<p><span class=\"token keyword\">SELECT<\/span> name<span class=\"token punctuation\">,<\/span> score<span class=\"token punctuation\">,<\/span><br \/>\n  ROW_NUMBER<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">OVER<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token keyword\">ORDER<\/span> <span class=\"token keyword\">BY<\/span> score <span class=\"token keyword\">DESC<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">AS<\/span> rn<span class=\"token punctuation\">,<\/span>  <span class=\"token comment\">&#8212; 1 2 3 4&#xff08;\u5e76\u5217\u4e5f\u786c\u6392&#xff09;<\/span><br \/>\n  RANK<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span>       <span class=\"token keyword\">OVER<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token keyword\">ORDER<\/span> <span class=\"token keyword\">BY<\/span> score <span class=\"token keyword\">DESC<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">AS<\/span> rk<span class=\"token punctuation\">,<\/span>  <span class=\"token comment\">&#8212; 1 2 2 4&#xff08;\u5e76\u5217\u540c\u540d&#xff0c;\u8df3\u53f7&#xff09;<\/span><br \/>\n  DENSE_RANK<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">OVER<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token keyword\">ORDER<\/span> <span class=\"token keyword\">BY<\/span> score <span class=\"token keyword\">DESC<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">AS<\/span> dr   <span class=\"token comment\">&#8212; 1 2 2 3&#xff08;\u5e76\u5217\u540c\u540d&#xff0c;\u4e0d\u8df3\u53f7&#xff09;<\/span><br \/>\n<span class=\"token keyword\">FROM<\/span> students<span class=\"token punctuation\">;<\/span><\/p>\n<p>\u914d PARTITION BY \u5c31\u662f\u300c\u5206\u7ec4\u5185\u6392\u540d\u300d\u2014\u2014\u76f4\u63a5\u89e3\u6389\u6700\u7ecf\u5178\u7684\u4e1a\u52a1\u9898&#xff1a;<\/p>\n<p><span class=\"token comment\">&#8212; \u7ecf\u5178\u9898 1&#xff1a;\u6bcf\u4e2a\u5206\u7c7b\u9500\u91cf Top3 \u7684\u5546\u54c1<\/span><br \/>\n<span class=\"token keyword\">SELECT<\/span> <span class=\"token operator\">*<\/span> <span class=\"token keyword\">FROM<\/span> <span class=\"token punctuation\">(<\/span><br \/>\n  <span class=\"token keyword\">SELECT<\/span> p<span class=\"token punctuation\">.<\/span>id<span class=\"token punctuation\">,<\/span> p<span class=\"token punctuation\">.<\/span>name<span class=\"token punctuation\">,<\/span> p<span class=\"token punctuation\">.<\/span>category_id<span class=\"token punctuation\">,<\/span> p<span class=\"token punctuation\">.<\/span>sales<span class=\"token punctuation\">,<\/span><br \/>\n         ROW_NUMBER<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">OVER<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token keyword\">PARTITION<\/span> <span class=\"token keyword\">BY<\/span> p<span class=\"token punctuation\">.<\/span>category_id <span class=\"token keyword\">ORDER<\/span> <span class=\"token keyword\">BY<\/span> p<span class=\"token punctuation\">.<\/span>sales <span class=\"token keyword\">DESC<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">AS<\/span> rn<br \/>\n  <span class=\"token keyword\">FROM<\/span> products p<br \/>\n<span class=\"token punctuation\">)<\/span> t<br \/>\n<span class=\"token keyword\">WHERE<\/span> t<span class=\"token punctuation\">.<\/span>rn <span class=\"token operator\">&lt;&#061;<\/span> <span class=\"token number\">3<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">&#8212; \u7ecf\u5178\u9898 2&#xff1a;\u6bcf\u65e5 GMV \u4e0e\u7d2f\u8ba1 GMV&#xff08;\u5148\u6309\u5929\u805a\u5408&#xff0c;\u518d\u5bf9\u805a\u5408\u7ed3\u679c\u5f00\u7a97&#xff09;<\/span><br \/>\n<span class=\"token keyword\">SELECT<\/span> <span class=\"token keyword\">DATE<\/span><span class=\"token punctuation\">(<\/span>pay_time<span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">AS<\/span> <span class=\"token keyword\">day<\/span><span class=\"token punctuation\">,<\/span><br \/>\n       <span class=\"token function\">SUM<\/span><span class=\"token punctuation\">(<\/span>total_amount<span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">AS<\/span> daily<span class=\"token punctuation\">,<\/span><br \/>\n       <span class=\"token function\">SUM<\/span><span class=\"token punctuation\">(<\/span><span class=\"token function\">SUM<\/span><span class=\"token punctuation\">(<\/span>total_amount<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">OVER<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token keyword\">ORDER<\/span> <span class=\"token keyword\">BY<\/span> <span class=\"token keyword\">DATE<\/span><span class=\"token punctuation\">(<\/span>pay_time<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">AS<\/span> cumulative<br \/>\n<span class=\"token keyword\">FROM<\/span> orders<br \/>\n<span class=\"token keyword\">WHERE<\/span> <span class=\"token keyword\">status<\/span> <span class=\"token operator\">&gt;&#061;<\/span> <span class=\"token number\">1<\/span><br \/>\n<span class=\"token keyword\">GROUP<\/span> <span class=\"token keyword\">BY<\/span> <span class=\"token keyword\">DATE<\/span><span class=\"token punctuation\">(<\/span>pay_time<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p>\u7b2c\u4e8c\u9898\u91cc SUM(SUM(&#8230;)) OVER \u770b\u7740\u5413\u4eba&#xff1a;\u5185\u5c42 SUM \u5148\u5b8c\u6210\u300c\u6309\u5929\u5206\u7ec4\u300d&#xff0c;\u5916\u5c42\u7a97\u53e3\u5bf9\u5206\u7ec4\u7ed3\u679c\u505a\u300c\u6309\u5929\u7d2f\u52a0\u300d\u3002\u7a97\u53e3\u51fd\u6570\u5728 GROUP BY \u4e4b\u540e\u6267\u884c&#xff0c;\u6240\u4ee5\u805a\u5408\u51fd\u6570\u53ef\u4ee5\u6574\u4f53\u4f5c\u4e3a\u7a97\u53e3\u7684\u8f93\u5165\u3002<\/p>\n<h4>4.5 \u9012\u5f52 CTE&#xff1a;\u67e5\u6574\u68f5\u5206\u7c7b\u6811<\/h4>\n<p>categories \u662f\u6811\u5f62\u7ed3\u6784&#xff08;parent_id \u6307\u5411\u7236\u8282\u70b9&#xff09;&#xff0c;\u67e5\u51fa\u300c\u67d0\u5206\u7c7b\u7684\u6240\u6709\u5b50\u5b59\u300d\u6216\u6574\u68f5\u6811&#xff0c;8.0 \u4e00\u4e2a\u9012\u5f52 CTE \u641e\u5b9a&#xff1a;<\/p>\n<p><span class=\"token keyword\">WITH<\/span> RECURSIVE tree <span class=\"token keyword\">AS<\/span> <span class=\"token punctuation\">(<\/span><br \/>\n  <span class=\"token comment\">&#8212; \u951a\u70b9&#xff1a;\u9876\u7ea7\u5206\u7c7b<\/span><br \/>\n  <span class=\"token keyword\">SELECT<\/span> id<span class=\"token punctuation\">,<\/span> name<span class=\"token punctuation\">,<\/span> parent_id<span class=\"token punctuation\">,<\/span> <span class=\"token number\">1<\/span> <span class=\"token keyword\">AS<\/span> lvl<span class=\"token punctuation\">,<\/span> CAST<span class=\"token punctuation\">(<\/span>id <span class=\"token keyword\">AS<\/span> <span class=\"token keyword\">CHAR<\/span><span class=\"token punctuation\">(<\/span><span class=\"token number\">200<\/span><span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">AS<\/span> path<br \/>\n  <span class=\"token keyword\">FROM<\/span> categories <span class=\"token keyword\">WHERE<\/span> parent_id <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">0<\/span><br \/>\n  <span class=\"token keyword\">UNION<\/span> <span class=\"token keyword\">ALL<\/span><br \/>\n  <span class=\"token comment\">&#8212; \u9012\u5f52&#xff1a;\u4e0d\u65ad\u627e\u4e0a\u4e00\u5c42\u8282\u70b9\u7684\u5b69\u5b50<\/span><br \/>\n  <span class=\"token keyword\">SELECT<\/span> c<span class=\"token punctuation\">.<\/span>id<span class=\"token punctuation\">,<\/span> c<span class=\"token punctuation\">.<\/span>name<span class=\"token punctuation\">,<\/span> c<span class=\"token punctuation\">.<\/span>parent_id<span class=\"token punctuation\">,<\/span> tree<span class=\"token punctuation\">.<\/span>lvl <span class=\"token operator\">&#043;<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">,<\/span> CONCAT<span class=\"token punctuation\">(<\/span>tree<span class=\"token punctuation\">.<\/span>path<span class=\"token punctuation\">,<\/span> <span class=\"token string\">&#039;-&#039;<\/span><span class=\"token punctuation\">,<\/span> c<span class=\"token punctuation\">.<\/span>id<span class=\"token punctuation\">)<\/span><br \/>\n  <span class=\"token keyword\">FROM<\/span> categories c<br \/>\n  <span class=\"token keyword\">INNER<\/span> <span class=\"token keyword\">JOIN<\/span> tree <span class=\"token keyword\">ON<\/span> c<span class=\"token punctuation\">.<\/span>parent_id <span class=\"token operator\">&#061;<\/span> tree<span class=\"token punctuation\">.<\/span>id<br \/>\n<span class=\"token punctuation\">)<\/span><br \/>\n<span class=\"token keyword\">SELECT<\/span> <span class=\"token operator\">*<\/span> <span class=\"token keyword\">FROM<\/span> tree <span class=\"token keyword\">ORDER<\/span> <span class=\"token keyword\">BY<\/span> path<span class=\"token punctuation\">;<\/span>  <span class=\"token comment\">&#8212; path \u524d\u7f00\u5929\u7136\u6309\u6811\u5f62\u6392\u5e8f<\/span><\/p>\n<h4>4.6 \u4e24\u9053\u538b\u8f74\u5b9e\u6218\u9898<\/h4>\n<p>\u538b\u8f74 1&#xff1a;\u6df1\u5206\u9875\u4e3a\u4ec0\u4e48\u6162\u3001\u600e\u4e48\u6551<\/p>\n<p><span class=\"token comment\">&#8212; \u6162&#xff1a;LIMIT 1000000, 20 \u8981\u53d6 1000020 \u884c\u3001\u4e22\u6389\u524d 100 \u4e07\u884c<\/span><br \/>\n<span class=\"token keyword\">SELECT<\/span> <span class=\"token operator\">*<\/span> <span class=\"token keyword\">FROM<\/span> orders <span class=\"token keyword\">ORDER<\/span> <span class=\"token keyword\">BY<\/span> id <span class=\"token keyword\">LIMIT<\/span> <span class=\"token number\">1000000<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">20<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">&#8212; \u4f18\u5316 A&#xff1a;\u6e38\u6807\/\u4e66\u7b7e\u7ffb\u9875\u2014\u2014\u8bb0\u4f4f\u4e0a\u4e00\u9875\u6700\u540e\u7684 id&#xff08;\u8981\u6c42\u6392\u5e8f\u952e\u8fde\u7eed\u9012\u589e&#xff09;<\/span><br \/>\n<span class=\"token keyword\">SELECT<\/span> <span class=\"token operator\">*<\/span> <span class=\"token keyword\">FROM<\/span> orders <span class=\"token keyword\">WHERE<\/span> id <span class=\"token operator\">&gt;<\/span> <span class=\"token number\">1000000<\/span> <span class=\"token keyword\">ORDER<\/span> <span class=\"token keyword\">BY<\/span> id <span class=\"token keyword\">LIMIT<\/span> <span class=\"token number\">20<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">&#8212; \u4f18\u5316 B&#xff1a;\u5ef6\u8fdf\u5173\u8054\u2014\u2014\u5148\u7528\u8986\u76d6\u7d22\u5f15\u628a 20 \u4e2a id \u627e\u51fa\u6765&#xff0c;\u518d\u56de\u8868\u53d6\u6574\u884c<\/span><br \/>\n<span class=\"token keyword\">SELECT<\/span> o<span class=\"token punctuation\">.<\/span><span class=\"token operator\">*<\/span><br \/>\n<span class=\"token keyword\">FROM<\/span> orders o<br \/>\n<span class=\"token keyword\">INNER<\/span> <span class=\"token keyword\">JOIN<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token keyword\">SELECT<\/span> id <span class=\"token keyword\">FROM<\/span> orders <span class=\"token keyword\">ORDER<\/span> <span class=\"token keyword\">BY<\/span> id <span class=\"token keyword\">LIMIT<\/span> <span class=\"token number\">1000000<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">20<\/span><span class=\"token punctuation\">)<\/span> t<br \/>\n        <span class=\"token keyword\">ON<\/span> o<span class=\"token punctuation\">.<\/span>id <span class=\"token operator\">&#061;<\/span> t<span class=\"token punctuation\">.<\/span>id<span class=\"token punctuation\">;<\/span><\/p>\n<p>\u538b\u8f74 2&#xff1a;\u627e\u51fa\u8fde\u7eed\u767b\u5f55 \u2265 3 \u5929\u7684\u7528\u6237<\/p>\n<p>\u601d\u8def\u4e00\u773c\u96be\u3001\u4ee3\u7801\u4e00\u884c\u900f&#xff1a;\u8fde\u7eed\u7684\u65e5\u671f\u51cf\u53bb\u8fde\u7eed\u7684\u5e8f\u53f7&#xff0c;\u5dee\u503c\u6052\u5b9a\u2014\u2014\u5dee\u503c\u76f8\u540c\u7684\u65e5\u5b50\u5fc5\u7136\u8fde\u6210\u4e00\u6bb5\u3002<\/p>\n<p><span class=\"token comment\">&#8212; \u8868&#xff1a;users_login(user_id, login_date)<\/span><br \/>\n<span class=\"token keyword\">SELECT<\/span> user_id<span class=\"token punctuation\">,<\/span> <span class=\"token function\">MIN<\/span><span class=\"token punctuation\">(<\/span>login_date<span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">AS<\/span> streak_start<span class=\"token punctuation\">,<\/span> <span class=\"token function\">COUNT<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">*<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">AS<\/span> days<br \/>\n<span class=\"token keyword\">FROM<\/span> <span class=\"token punctuation\">(<\/span><br \/>\n  <span class=\"token keyword\">SELECT<\/span> user_id<span class=\"token punctuation\">,<\/span> login_date<span class=\"token punctuation\">,<\/span><br \/>\n         DATE_SUB<span class=\"token punctuation\">(<\/span>login_date<span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">INTERVAL<\/span> ROW_NUMBER<span class=\"token punctuation\">(<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">OVER<\/span> <span class=\"token punctuation\">(<\/span><br \/>\n                    <span class=\"token keyword\">PARTITION<\/span> <span class=\"token keyword\">BY<\/span> user_id <span class=\"token keyword\">ORDER<\/span> <span class=\"token keyword\">BY<\/span> login_date<span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">DAY<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">AS<\/span> grp<br \/>\n  <span class=\"token keyword\">FROM<\/span> <span class=\"token punctuation\">(<\/span><span class=\"token keyword\">SELECT<\/span> <span class=\"token keyword\">DISTINCT<\/span> user_id<span class=\"token punctuation\">,<\/span> login_date <span class=\"token keyword\">FROM<\/span> users_login<span class=\"token punctuation\">)<\/span> d  <span class=\"token comment\">&#8212; \u5148\u53bb\u91cd<\/span><br \/>\n<span class=\"token punctuation\">)<\/span> t<br \/>\n<span class=\"token keyword\">GROUP<\/span> <span class=\"token keyword\">BY<\/span> user_id<span class=\"token punctuation\">,<\/span> grp<br \/>\n<span class=\"token keyword\">HAVING<\/span> <span class=\"token function\">COUNT<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">*<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&gt;&#061;<\/span> <span class=\"token number\">3<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token comment\">&#8212; grp \u76f8\u540c\u7684\u884c\u5373\u4e00\u6bb5\u8fde\u7eed\u767b\u5f55&#xff1b;\u8981 \u2265N \u5929\u5c31\u6539 HAVING \u6761\u4ef6<\/span><\/p>\n<hr \/>\n<h3>\u4e94\u3001\u7d22\u5f15&#xff1a;\u4ece B&#043; \u6811\u5230 EXPLAIN \u5b9e\u6218<\/h3>\n<p>\u7d22\u5f15\u662f MySQL \u6027\u80fd\u95ee\u9898\u7684\u7b2c\u4e00\u73b0\u573a&#xff1a;\u4e5d\u6210\u6162 SQL \u7684\u75c5\u56e0\u548c\u836f\u65b9\u90fd\u5728\u7d22\u5f15\u4e0a\u3002\u8fd9\u4e00\u7ae0\u4ece\u300c\u4e3a\u4ec0\u4e48\u504f\u504f\u662f B&#043; \u6811\u300d\u8bb2\u8d77&#xff0c;\u4e00\u8def\u5230 EXPLAIN \u5b9e\u6218\u6392\u969c&#xff0c;\u662f\u5168\u6587\u6700\u503c\u5f97\u53cd\u590d\u770b\u7684\u4e00\u7ae0\u3002<\/p>\n<h4>5.1 \u4e3a\u4ec0\u4e48\u662f B&#043; \u6811&#xff1a;\u4e00\u9053\u6392\u9664\u9898<\/h4>\n<p>\u56de\u5fc6\u7d22\u5f15\u7ed3\u6784&#xff0c;\u6700\u5feb\u7684\u529e\u6cd5\u662f\u628a\u5019\u9009\u65b9\u6848\u6328\u4e2a\u6392\u9664&#xff1a;<\/p>\n<ul>\n<li>\u54c8\u5e0c\u8868&#xff1a;\u7b49\u503c\u67e5\u8be2 O(1) \u5f88\u9999&#xff0c;\u4f46\u5b8c\u5168\u4e0d\u652f\u6301\u8303\u56f4\u67e5\u8be2\u548c\u6392\u5e8f\u2014\u2014WHERE id &gt; 100 \u76f4\u63a5\u6b47\u83dc&#xff1b;<\/li>\n<li>\u4e8c\u53c9\u641c\u7d22\u6811&#xff1a;\u6570\u636e\u9012\u589e\u63d2\u5165\u4f1a\u9000\u5316\u6210\u94fe\u8868&#xff0c;O(n)&#xff1b;<\/li>\n<li>\u7ea2\u9ed1\u6811\/AVL&#xff1a;\u89e3\u51b3\u4e86\u9000\u5316&#xff0c;\u4f46\u4e8c\u53c9\u610f\u5473\u7740\u300c\u767e\u4e07\u6570\u636e 20&#043; \u5c42\u300d&#xff0c;\u6bcf\u5c42\u90fd\u662f\u4e00\u6b21\u78c1\u76d8 IO&#xff1b;<\/li>\n<li>B \u6811&#xff1a;\u591a\u53c9\u77ee\u80d6&#xff0c;\u65b9\u5411\u5bf9\u4e86&#xff1b;\u4f46\u6bcf\u4e2a\u8282\u70b9\u90fd\u5b58\u6574\u884c\u6570\u636e&#xff0c;\u4e00\u9875 16KB \u88c5\u4e0d\u4e86\u51e0\u4e2a\u952e&#xff0c;\u6811\u8fd8\u662f\u504f\u9ad8&#xff1b;<\/li>\n<li>B&#043; \u6811&#xff1a;\u975e\u53f6\u5b50\u8282\u70b9\u53ea\u5b58\u300c\u952e &#043; \u6307\u9488\u300d&#xff0c;\u53f6\u5b50\u8282\u70b9\u5b58\u6570\u636e\u5e76\u4e14\u6309\u5e8f\u7ec4\u6210\u53cc\u5411\u94fe\u8868\u2014\u2014\u77ee\u3001\u4e14\u8303\u56f4\u67e5\u8be2\u987a\u85e4\u6478\u74dc\u3002<\/li>\n<\/ul>\n<p>  #mermaid-svg-iAP2EhnRiZiIZRBv{font-family:\\&#8221;trebuchet ms\\&#8221;,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-iAP2EhnRiZiIZRBv .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-iAP2EhnRiZiIZRBv .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-iAP2EhnRiZiIZRBv .error-icon{fill:#552222;}#mermaid-svg-iAP2EhnRiZiIZRBv .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-iAP2EhnRiZiIZRBv .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-iAP2EhnRiZiIZRBv .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-iAP2EhnRiZiIZRBv .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-iAP2EhnRiZiIZRBv .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-iAP2EhnRiZiIZRBv .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-iAP2EhnRiZiIZRBv .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-iAP2EhnRiZiIZRBv .marker{fill:#333333;stroke:#333333;}#mermaid-svg-iAP2EhnRiZiIZRBv .marker.cross{stroke:#333333;}#mermaid-svg-iAP2EhnRiZiIZRBv svg{font-family:\\&#8221;trebuchet ms\\&#8221;,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-iAP2EhnRiZiIZRBv p{margin:0;}#mermaid-svg-iAP2EhnRiZiIZRBv .label{font-family:\\&#8221;trebuchet ms\\&#8221;,verdana,arial,sans-serif;color:#333;}#mermaid-svg-iAP2EhnRiZiIZRBv .cluster-label text{fill:#333;}#mermaid-svg-iAP2EhnRiZiIZRBv .cluster-label span{color:#333;}#mermaid-svg-iAP2EhnRiZiIZRBv .cluster-label span p{background-color:transparent;}#mermaid-svg-iAP2EhnRiZiIZRBv .label text,#mermaid-svg-iAP2EhnRiZiIZRBv span{fill:#333;color:#333;}#mermaid-svg-iAP2EhnRiZiIZRBv .node rect,#mermaid-svg-iAP2EhnRiZiIZRBv .node circle,#mermaid-svg-iAP2EhnRiZiIZRBv .node ellipse,#mermaid-svg-iAP2EhnRiZiIZRBv .node polygon,#mermaid-svg-iAP2EhnRiZiIZRBv .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-iAP2EhnRiZiIZRBv .rough-node .label text,#mermaid-svg-iAP2EhnRiZiIZRBv .node .label text,#mermaid-svg-iAP2EhnRiZiIZRBv .image-shape .label,#mermaid-svg-iAP2EhnRiZiIZRBv .icon-shape .label{text-anchor:middle;}#mermaid-svg-iAP2EhnRiZiIZRBv .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-iAP2EhnRiZiIZRBv .rough-node .label,#mermaid-svg-iAP2EhnRiZiIZRBv .node .label,#mermaid-svg-iAP2EhnRiZiIZRBv .image-shape .label,#mermaid-svg-iAP2EhnRiZiIZRBv .icon-shape .label{text-align:center;}#mermaid-svg-iAP2EhnRiZiIZRBv .node.clickable{cursor:pointer;}#mermaid-svg-iAP2EhnRiZiIZRBv .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-iAP2EhnRiZiIZRBv .arrowheadPath{fill:#333333;}#mermaid-svg-iAP2EhnRiZiIZRBv .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-iAP2EhnRiZiIZRBv .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-iAP2EhnRiZiIZRBv .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-iAP2EhnRiZiIZRBv .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-iAP2EhnRiZiIZRBv .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-iAP2EhnRiZiIZRBv .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-iAP2EhnRiZiIZRBv .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-iAP2EhnRiZiIZRBv .cluster text{fill:#333;}#mermaid-svg-iAP2EhnRiZiIZRBv .cluster span{color:#333;}#mermaid-svg-iAP2EhnRiZiIZRBv div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\\&#8221;trebuchet ms\\&#8221;,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-iAP2EhnRiZiIZRBv .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-iAP2EhnRiZiIZRBv rect.text{fill:none;stroke-width:0;}#mermaid-svg-iAP2EhnRiZiIZRBv .icon-shape,#mermaid-svg-iAP2EhnRiZiIZRBv .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-iAP2EhnRiZiIZRBv .icon-shape p,#mermaid-svg-iAP2EhnRiZiIZRBv .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-iAP2EhnRiZiIZRBv .icon-shape .label rect,#mermaid-svg-iAP2EhnRiZiIZRBv .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-iAP2EhnRiZiIZRBv .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-iAP2EhnRiZiIZRBv .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-iAP2EhnRiZiIZRBv :root{&#8211;mermaid-font-family:\\&#8221;trebuchet ms\\&#8221;,verdana,arial,sans-serif;}<\/p>\n<p>         <span class=\"edgeLabel\"><\/span><\/p>\n<p>         <span class=\"edgeLabel\"><\/span><\/p>\n<p>         <span class=\"edgeLabel\"><\/span><\/p>\n<p>         <span class=\"edgeLabel\"><\/p>\n<p>\u53cc\u5411\u94fe\u8868<\/p>\n<p><\/span><\/p>\n<p>         <span class=\"edgeLabel\"><\/p>\n<p>\u53cc\u5411\u94fe\u8868<\/p>\n<p><\/span><\/p>\n<p>         <span class=\"nodeLabel\"><\/p>\n<p>\u6839\u8282\u70b9&#xff08;\u5185\u90e8\u9875&#xff09;\u53ea\u5b58\u952e\u548c\u6307\u9488&#xff1a;15 \u00b7 35<\/p>\n<p><\/span><\/p>\n<p>         <span class=\"nodeLabel\"><\/p>\n<p>\u53f6\u5b50\u9875\u24605 \u00b7 10 \u00b7 15<\/p>\n<p><\/span><\/p>\n<p>         <span class=\"nodeLabel\"><\/p>\n<p>\u53f6\u5b50\u9875\u246120 \u00b7 25 \u00b7 30<\/p>\n<p><\/span><\/p>\n<p>         <span class=\"nodeLabel\"><\/p>\n<p>\u53f6\u5b50\u9875\u246240 \u00b7 45 \u00b7 50<\/p>\n<p><\/span><\/p>\n<p>\u8457\u540d\u7684\u300c2000 \u4e07\u884c\u53ea\u9700 3 \u5c42\u300d\u63a8\u5bfc&#xff1a;\u4e3b\u952e BIGINT 8 \u5b57\u8282 &#043; \u9875\u6307\u9488 6 \u5b57\u8282 &#061; 14 \u5b57\u8282&#xff0c;\u4e00\u9875 16KB \u7ea6 1170 \u4e2a\u8def\u724c&#xff1b;\u5047\u8bbe\u6bcf\u884c\u6570\u636e 1KB&#xff0c;\u4e00\u9875\u53f6\u5b50\u5b58 16 \u884c\u3002\u4e8e\u662f 3 \u5c42\u6811\u5bb9\u91cf &#061; 1170 \u00d7 1170 \u00d7 16 \u2248 2190 \u4e07\u884c\u3002\u6839\u548c\u4e2d\u95f4\u5c42\u5e38\u5e74\u9a7b\u7559\u5185\u5b58&#xff0c;\u4e00\u6b21\u4e3b\u952e\u67e5\u8be2\u901a\u5e38\u53ea\u6709 1 \u6b21\u78c1\u76d8 IO\u3002<\/p>\n<h4>5.2 \u805a\u7c07\u7d22\u5f15\u4e0e\u4e8c\u7ea7\u7d22\u5f15&#xff1a;\u56de\u8868\u7684\u7531\u6765<\/h4>\n<ul>\n<li>\u805a\u7c07\u7d22\u5f15&#xff08;\u4e3b\u952e\u7d22\u5f15&#xff09;&#xff1a;\u53f6\u5b50\u8282\u70b9\u5b58\u6574\u884c\u6570\u636e\u3002InnoDB \u8868\u672c\u8eab\u5c31\u662f\u300c\u6309\u4e3b\u952e\u7ec4\u7ec7\u300d\u7684 B&#043; \u6811&#xff0c;\u6240\u4ee5\u4e5f\u53eb\u7d22\u5f15\u7ec4\u7ec7\u8868&#xff1b;\u8868\u6ca1\u6709\u4e3b\u952e\u65f6&#xff0c;InnoDB \u4f1a\u6311\u4e00\u4e2a\u552f\u4e00\u975e\u7a7a\u7d22\u5f15\u9876\u66ff&#xff0c;\u518d\u6ca1\u6709\u5c31\u751f\u6210\u9690\u85cf\u7684 row_id\u3002<\/li>\n<li>\u4e8c\u7ea7\u7d22\u5f15&#xff08;\u666e\u901a\u7d22\u5f15&#xff09;&#xff1a;\u53f6\u5b50\u8282\u70b9\u5b58\u300c\u7d22\u5f15\u5217\u7684\u503c &#043; \u4e3b\u952e\u503c\u300d&#xff0c;\u4e0d\u5b58\u884c\u6570\u636e\u2014\u2014\u4e0d\u662f\u4e0d\u60f3\u5b58&#xff0c;\u662f\u884c\u6570\u636e\u4f1a\u968f\u9875\u5206\u88c2\u79fb\u52a8&#xff0c;\u5b58\u5730\u5740\u4f1a\u5931\u6548&#xff0c;\u5b58\u7a33\u5b9a\u7684\u4e3b\u952e\u503c\u624d\u53ef\u9760\u3002<\/li>\n<\/ul>\n<p>\u4e8e\u662f\u4e00\u6b21\u4e8c\u7ea7\u7d22\u5f15\u67e5\u8be2\u7684\u5b8c\u6574\u8def\u5f84\u662f&#xff1a;\u5148\u5728\u4e8c\u7ea7\u7d22\u5f15\u6811\u627e\u5230\u4e3b\u952e\u503c&#xff0c;\u518d\u62ff\u4e3b\u952e\u503c\u53bb\u805a\u7c07\u7d22\u5f15\u6811\u67e5\u4e00\u904d\u6574\u884c\u2014\u2014\u7b2c\u4e8c\u6b21\u67e5\u627e\u5c31\u53eb\u56de\u8868\u3002\u5982\u679c\u547d\u4e2d\u884c\u6570\u5f88\u5927&#xff08;\u6bd4\u5982 10 \u4e07\u884c&#xff09;&#xff0c;\u5c31\u662f 10 \u4e07\u6b21\u56de\u8868&#xff0c;\u8fd9\u5c31\u662f\u5f88\u591a\u300c\u660e\u660e\u6709\u7d22\u5f15\u8fd8\u662f\u6162\u300d\u7684\u771f\u76f8\u3002<\/p>\n<p>\u8bb0\u5fc6\u951a\u70b9&#xff1a;\u4e3b\u952e\u4e3a\u4ec0\u4e48\u63a8\u8350\u81ea\u589e\u3002 \u81ea\u589e\u4e3b\u952e\u6c38\u8fdc\u8ffd\u52a0\u5230 B&#043; \u6811\u6700\u53f3\u4fa7&#xff0c;\u9875\u5199\u6ee1\u624d\u5f00\u65b0\u9875&#xff0c;\u987a\u5e8f\u53cb\u597d&#xff1b;UUID \u8fd9\u7c7b\u968f\u673a\u4e3b\u952e\u4f1a\u8ba9\u65b0\u6570\u636e\u968f\u673a\u63d2\u5165&#xff0c;\u9891\u7e41\u9875\u5206\u88c2\u3001\u5185\u5b58\u547d\u4e2d\u7387\u4e0b\u964d\u3001\u6811\u8d8a\u6765\u8d8a\u9ad8\u3002\u975e\u8981\u7528\u5206\u5e03\u5f0f ID&#xff0c;\u7528\u8d8b\u52bf\u9012\u589e\u7684\u96ea\u82b1 ID\u3002<\/p>\n<h4>5.3 \u8986\u76d6\u7d22\u5f15\u4e0e\u7d22\u5f15\u4e0b\u63a8&#xff1a;\u7701\u6389\u56de\u8868\u7684\u4e24\u677f\u65a7<\/h4>\n<p><span class=\"token comment\">&#8212; \u8986\u76d6\u7d22\u5f15&#xff1a;SELECT \u7684\u5217\u5168\u90e8\u5305\u542b\u5728\u7d22\u5f15\u91cc \u2192 \u4e0d\u7528\u56de\u8868&#xff0c;EXPLAIN \u7684 Extra \u663e\u793a Using index<\/span><br \/>\n<span class=\"token keyword\">SELECT<\/span> id<span class=\"token punctuation\">,<\/span> category_id <span class=\"token keyword\">FROM<\/span> products <span class=\"token keyword\">WHERE<\/span> category_id <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">3<\/span><span class=\"token punctuation\">;<\/span>  <span class=\"token comment\">&#8212; idx_category(category_id)<\/span><\/p>\n<p><span class=\"token comment\">&#8212; \u7d22\u5f15\u4e0b\u63a8&#xff08;ICP&#xff0c;5.6&#043;&#xff09;&#xff1a;\u628a WHERE \u91cc\u80fd\u7528\u7d22\u5f15\u5224\u65ad\u7684\u6761\u4ef6&#034;\u4e0b\u63a8&#034;\u7ed9\u5b58\u50a8\u5f15\u64ce\u5148\u8fc7\u6ee4<\/span><br \/>\n<span class=\"token comment\">&#8212; \u5047\u8bbe\u7d22\u5f15 idx_name_age(name, age)&#xff1a;<\/span><br \/>\n<span class=\"token keyword\">SELECT<\/span> <span class=\"token operator\">*<\/span> <span class=\"token keyword\">FROM<\/span> users <span class=\"token keyword\">WHERE<\/span> name <span class=\"token operator\">LIKE<\/span> <span class=\"token string\">&#039;\u5f20%&#039;<\/span> <span class=\"token operator\">AND<\/span> age <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">10<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token comment\">&#8212; \u6ca1\u6709 ICP&#xff1a;\u5f15\u64ce\u6309 &#039;\u5f20%&#039; \u627e\u5230 1000 \u884c\u5168\u90e8\u56de\u8868&#xff0c;Server \u5c42\u518d\u6309 age&#061;10 \u8fc7\u6ee4\u5269 10 \u884c<\/span><br \/>\n<span class=\"token comment\">&#8212; \u6709 ICP&#xff1a;  \u5f15\u64ce\u5728\u7d22\u5f15\u91cc\u5c31\u628a age !&#061; 10 \u7684 990 \u884c\u6254\u6389&#xff0c;\u53ea\u56de\u8868 10 \u884c<\/span><\/p>\n<h4>5.4 \u6700\u5de6\u524d\u7f00\u539f\u5219&#xff1a;\u8054\u5408\u7d22\u5f15\u7684\u7075\u9b42<\/h4>\n<p>\u8054\u5408\u7d22\u5f15 (a, b, c) \u7684\u6392\u5e8f\u89c4\u5219\u662f\u300c\u5148\u6309 a \u6392&#xff0c;a \u76f8\u540c\u6309 b \u6392&#xff0c;\u518d\u76f8\u540c\u6309 c \u6392\u300d\u2014\u2014\u7535\u8bdd\u7c3f\u5148\u59d3\u540e\u540d\u3002\u6240\u4ee5\u67e5\u8be2\u6761\u4ef6\u5fc5\u987b\u4ece\u6700\u5de6\u5217\u5f00\u59cb\u8fde\u7eed\u547d\u4e2d&#xff1a;<\/p>\n<table>\n<tr>WHERE \u6761\u4ef6\u7d22\u5f15\u4f7f\u7528\u60c5\u51b5\u539f\u56e0<\/tr>\n<tbody>\n<tr>\n<td>a &#061; 1<\/td>\n<td>\u7528\u4e0a a<\/td>\n<td>\u4ece\u6700\u5de6\u5217\u5f00\u59cb<\/td>\n<\/tr>\n<tr>\n<td>a &#061; 1 AND b &#061; 2<\/td>\n<td>\u7528\u4e0a a\u3001b<\/td>\n<td>\u8fde\u7eed\u547d\u4e2d<\/td>\n<\/tr>\n<tr>\n<td>a &#061; 1 AND b &#061; 2 AND c &#061; 3<\/td>\n<td>\u5168\u7528\u4e0a<\/td>\n<td>\u5b8c\u7f8e\u547d\u4e2d<\/td>\n<\/tr>\n<tr>\n<td>a &#061; 1 AND b &gt; 2 AND c &#061; 3<\/td>\n<td>\u53ea\u7528 a\u3001b<\/td>\n<td>b \u662f\u8303\u56f4\u67e5\u8be2&#xff0c;c \u65ad\u4e86<\/td>\n<\/tr>\n<tr>\n<td>b &#061; 2 AND c &#061; 3<\/td>\n<td>\u7528\u4e0d\u4e0a<\/td>\n<td>\u7f3a\u4e86\u6700\u5de6\u5217 a<\/td>\n<\/tr>\n<tr>\n<td>a &#061; 1 AND c &#061; 3<\/td>\n<td>\u53ea\u7528 a<\/td>\n<td>b \u7f3a\u5e2d&#xff0c;c \u63a5\u4e0d\u4e0a&#xff08;8.0.13&#043; \u90e8\u5206\u573a\u666f\u6709 Skip Scan&#xff0c;\u522b\u4f9d\u8d56&#xff09;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<ul>\n<li>\u8bbe\u8ba1\u53e3\u8bc0&#xff1a;\u7b49\u503c\u6761\u4ef6\u7684\u5217\u653e\u524d\u9762&#xff0c;\u8303\u56f4\u6761\u4ef6\u7684\u5217\u653e\u6700\u540e&#xff1b;\u533a\u5206\u5ea6&#xff08;\u4e0d\u91cd\u590d\u6bd4\u4f8b&#xff09;\u9ad8\u7684\u5217\u653e\u524d\u9762&#xff1b;\u591a\u79df\u6237\u7cfb\u7edf\u628a tenant_id \u653e\u6700\u524d&#xff1b;<\/li>\n<li>\u5df2\u5efa (a,b) \u5c31\u4e0d\u5fc5\u5355\u5efa (a)&#xff1a;\u6700\u5de6\u524d\u7f00\u8ba9\u5b83\u517c\u804c\u4e86 a \u7684\u7d22\u5f15\u2014\u2014\u300c\u51cf\u5c11\u5197\u4f59\u7d22\u5f15\u300d\u662f DDL \u8bc4\u5ba1\u7684\u56fa\u5b9a\u52a8\u4f5c\u3002<\/li>\n<\/ul>\n<h4>5.5 \u7d22\u5f15\u5931\u6548\u573a\u666f\u6e05\u5355&#xff08;\u9010\u6761\u5bf9\u7167&#xff09;<\/h4>\n<p><span class=\"token comment\">&#8212; \u2460 \u7d22\u5f15\u5217\u53c2\u4e0e\u8fd0\u7b97\u6216\u5957\u51fd\u6570<\/span><br \/>\n<span class=\"token keyword\">WHERE<\/span> <span class=\"token keyword\">YEAR<\/span><span class=\"token punctuation\">(<\/span>created_at<span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">2026<\/span>           <span class=\"token comment\">&#8212; \u2717 \u7ed9\u5217\u5957\u4e86\u51fd\u6570&#xff0c;\u7d22\u5f15\u62a5\u5e9f<\/span><br \/>\n<span class=\"token keyword\">WHERE<\/span> created_at <span class=\"token operator\">&gt;&#061;<\/span> <span class=\"token string\">&#039;2026-01-01&#039;<\/span><br \/>\n  <span class=\"token operator\">AND<\/span> created_at <span class=\"token operator\">&lt;<\/span>  <span class=\"token string\">&#039;2027-01-01&#039;<\/span>        <span class=\"token comment\">&#8212; \u2713 \u6539\u5199\u6210\u8303\u56f4\u6761\u4ef6<\/span><\/p>\n<p><span class=\"token comment\">&#8212; \u2461 \u9690\u5f0f\u7c7b\u578b\u8f6c\u6362&#xff08;\u5b57\u7b26\u4e32\u5217\u7528\u6570\u5b57\u67e5&#xff09;<\/span><br \/>\n<span class=\"token keyword\">WHERE<\/span> order_no <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">20260925001<\/span>            <span class=\"token comment\">&#8212; \u2717 \u7b49\u4ef7\u4e8e\u7ed9 order_no \u5957 CAST \u51fd\u6570<\/span><br \/>\n<span class=\"token keyword\">WHERE<\/span> order_no <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#039;20260925001&#039;<\/span>          <span class=\"token comment\">&#8212; \u2713 \u7c7b\u578b\u5bf9\u9f50<\/span><\/p>\n<p><span class=\"token comment\">&#8212; \u2462 \u524d\u5bfc\u6a21\u7cca\u5339\u914d<\/span><br \/>\n<span class=\"token keyword\">WHERE<\/span> name <span class=\"token operator\">LIKE<\/span> <span class=\"token string\">&#039;%\u624b\u673a%&#039;<\/span>                 <span class=\"token comment\">&#8212; \u2717 \u5f00\u5934\u4e0d\u786e\u5b9a&#xff0c;B&#043; \u6811\u6ca1\u6cd5\u5b9a\u4f4d<\/span><br \/>\n<span class=\"token keyword\">WHERE<\/span> name <span class=\"token operator\">LIKE<\/span> <span class=\"token string\">&#039;\u82f9\u679c%&#039;<\/span>                  <span class=\"token comment\">&#8212; \u2713 \u76f8\u5f53\u4e8e\u8303\u56f4\u67e5\u8be2 [\u82f9\u679c, \u82f9\u679c&#043;1)<\/span><\/p>\n<p><span class=\"token comment\">&#8212; \u2463 OR \u8fde\u63a5\u4e86\u6ca1\u6709\u7d22\u5f15\u7684\u5217<\/span><br \/>\n<span class=\"token keyword\">WHERE<\/span> category_id <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">3<\/span> <span class=\"token operator\">OR<\/span> subtitle <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#039;x&#039;<\/span> <span class=\"token comment\">&#8212; \u2717 subtitle \u65e0\u7d22\u5f15 \u2192 \u6574\u6761 SQL \u5168\u8868\u626b\u63cf<\/span><\/p>\n<p><span class=\"token comment\">&#8212; \u2464 \u8054\u5408\u7d22\u5f15\u4e0d\u6ee1\u8db3\u6700\u5de6\u524d\u7f00&#xff08;\u89c1 5.4&#xff09;<\/span><\/p>\n<p><span class=\"token comment\">&#8212; \u2465 \u4f18\u5316\u5668\u300c\u4e3b\u52a8\u653e\u5f03\u300d&#xff1a;\u9884\u4f30\u547d\u4e2d\u884c\u6570\u592a\u591a&#xff0c;\u56de\u8868\u6210\u672c &gt; \u5168\u8868\u626b\u63cf<\/span><br \/>\n<span class=\"token comment\">&#8212;     &#xff08;\u4e25\u683c\u8bf4\u8fd9\u4e0d\u53eb\u5931\u6548&#xff0c;\u662f\u6210\u672c\u4f30\u7b97&#xff1b;FORCE INDEX \u53ef\u5f3a\u5236&#xff0c;\u4f46\u5148\u60f3\u6e05\u695a\u8981\u4e0d\u8981&#xff09;<\/span><\/p>\n<p>\u7ea0\u504f&#xff1a;\u8001\u6587\u7ae0\u7684\u4e24\u5927\u8bef\u4f20\u3002\u300c!&#061; \u548c NOT IN \u4e00\u5b9a\u5931\u6548\u300d\u2014\u2014\u4e0d\u5bf9&#xff0c;MySQL \u4f1a\u6309\u6210\u672c\u4f30\u7b97\u51b3\u5b9a\u8d70 range \u8fd8\u662f\u5168\u8868&#xff0c;\u5f88\u591a\u65f6\u5019\u7167\u6837\u8d70\u7d22\u5f15&#xff1b;\u300cIS NULL \u7528\u4e0d\u4e86\u7d22\u5f15\u300d\u2014\u2014\u4e5f\u4e0d\u5bf9&#xff0c;NULL \u503c\u540c\u6837\u5b58\u5728 B&#043; \u6811\u91cc\u3002\u5224\u65ad\u6807\u51c6\u6c38\u8fdc\u662f EXPLAIN&#xff0c;\u4e0d\u662f\u53e3\u8bc0\u3002<\/p>\n<h4>5.6 EXPLAIN&#xff1a;\u6162 SQL \u7684\u542c\u8bca\u5668<\/h4>\n<p><span class=\"token keyword\">EXPLAIN<\/span> <span class=\"token keyword\">SELECT<\/span> <span class=\"token operator\">*<\/span> <span class=\"token keyword\">FROM<\/span> orders <span class=\"token keyword\">WHERE<\/span> user_id <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">88<\/span> <span class=\"token operator\">AND<\/span> <span class=\"token keyword\">status<\/span> <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">1<\/span><br \/>\n<span class=\"token keyword\">ORDER<\/span> <span class=\"token keyword\">BY<\/span> created_at <span class=\"token keyword\">DESC<\/span> <span class=\"token keyword\">LIMIT<\/span> <span class=\"token number\">20<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<table>\n<tr>\u5b57\u6bb5\u770b\u4ec0\u4e48<\/tr>\n<tbody>\n<tr>\n<td>id<\/td>\n<td>\u6267\u884c\u5e8f\u53f7&#xff0c;\u76f8\u540c\u4e3a\u4e00\u7ec4\u4ece\u4e0a\u5f80\u4e0b\u6267\u884c&#xff1b;\u5b50\u67e5\u8be2\/UNION \u4f1a\u4ea7\u751f\u65b0 id&#xff0c;\u5927\u7ed3\u679c\u96c6\u5728\u540e<\/td>\n<\/tr>\n<tr>\n<td>type<\/td>\n<td>\u8bbf\u95ee\u7c7b\u578b&#xff0c;\u6027\u80fd\u4ece\u597d\u5230\u5dee&#xff08;\u89c1\u4e0b\u65b9\u68af\u5ea6&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>key \/ key_len<\/td>\n<td>\u5b9e\u9645\u7528\u4e86\u54ea\u4e2a\u7d22\u5f15\u3001\u7528\u4e86\u51e0\u5217&#xff08;\u5224\u65ad\u8054\u5408\u7d22\u5f15\u7528\u5230\u7b2c\u51e0\u5217&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>rows<\/td>\n<td>\u9884\u4f30\u626b\u63cf\u884c\u6570&#xff0c;\u6570\u91cf\u7ea7\u6bd4\u7cbe\u786e\u503c\u91cd\u8981<\/td>\n<\/tr>\n<tr>\n<td>Extra<\/td>\n<td>\u9644\u52a0\u52a8\u4f5c&#xff0c;\u91cd\u70b9\u76ef Using filesort \/ Using temporary<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>type \u68af\u5ea6&#xff08;\u4ece\u597d\u5230\u5dee&#xff09;&#xff1a;const \u2192 eq_ref \u2192 ref \u2192 range \u2192 index \u2192 ALL\u3002\u65e5\u5e38\u6807\u51c6&#xff1a;\u81f3\u5c11 range&#xff0c;\u7b49\u503c\u67e5\u8be2\u5e94\u5230 ref&#xff0c;JOIN \u88ab\u9a71\u52a8\u8868\u5e94\u5230 eq_ref&#xff1b;\u51fa\u73b0 ALL&#xff08;\u5168\u8868\u626b\u63cf&#xff09;\u5c31\u8981\u8b66\u60d5\u3002<\/p>\n<ul>\n<li>const&#xff1a;\u4e3b\u952e\/\u552f\u4e00\u952e\u7b49\u503c&#xff0c;\u6700\u591a\u4e00\u884c&#xff1b;<\/li>\n<li>eq_ref&#xff1a;JOIN \u65f6\u88ab\u9a71\u52a8\u8868\u8d70\u4e3b\u952e\u6216\u552f\u4e00\u7d22\u5f15&#xff1b;<\/li>\n<li>ref&#xff1a;\u666e\u901a\u4e8c\u7ea7\u7d22\u5f15\u7b49\u503c&#xff1b;<\/li>\n<li>range&#xff1a;\u7d22\u5f15\u8303\u56f4&#xff08;BETWEEN\u3001&gt;\u3001IN \u7b49&#xff09;&#xff1b;<\/li>\n<li>index&#xff1a;\u626b\u6574\u68f5\u7d22\u5f15\u6811&#xff08;\u5e38\u89c1\u4e8e\u8986\u76d6\u7d22\u5f15\u6216 MIN\/MAX&#xff09;&#xff1b;<\/li>\n<li>ALL&#xff1a;\u5168\u8868\u626b\u63cf\u3002<\/li>\n<\/ul>\n<p>Extra \u9ad8\u9891\u503c&#xff1a;Using index \u8986\u76d6\u7d22\u5f15&#xff08;\u597d&#xff09;&#xff1b;Using index condition \u7d22\u5f15\u4e0b\u63a8&#xff08;\u597d&#xff09;&#xff1b;Using filesort \u9700\u8981\u989d\u5916\u6392\u5e8f&#xff08;\u4f18\u5316\u4fe1\u53f7&#xff09;&#xff1b;Using temporary \u5efa\u4e86\u4e34\u65f6\u8868&#xff08;GROUP BY \/ DISTINCT \u5e38\u89c1&#xff0c;\u91cd\u70b9\u4f18\u5316&#xff09;\u3002<\/p>\n<h4>5.7 \u5b9e\u6218&#xff1a;\u8ba2\u5355\u5217\u8868\u9875\u6162\u67e5\u8be2\u4f18\u5316\u5168\u8fc7\u7a0b<\/h4>\n<p>\u573a\u666f&#xff1a;\u7528\u6237\u4e2d\u5fc3\u300c\u6211\u7684\u8ba2\u5355\u300d\u9875&#xff0c;\u7ebf\u4e0a\u5e73\u5747 1.8s\u3002SQL \u4e0e\u4f18\u5316\u524d EXPLAIN&#xff1a;<\/p>\n<p><span class=\"token keyword\">SELECT<\/span> <span class=\"token operator\">*<\/span> <span class=\"token keyword\">FROM<\/span> orders<br \/>\n<span class=\"token keyword\">WHERE<\/span> user_id <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">88<\/span> <span class=\"token operator\">AND<\/span> <span class=\"token keyword\">status<\/span> <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">1<\/span><br \/>\n<span class=\"token keyword\">ORDER<\/span> <span class=\"token keyword\">BY<\/span> created_at <span class=\"token keyword\">DESC<\/span><br \/>\n<span class=\"token keyword\">LIMIT<\/span> <span class=\"token number\">20<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">&#8212; \u4f18\u5316\u524d&#xff08;\u8868\u4e0a\u53ea\u6709 idx_user_created(user_id, created_at)&#xff09;&#xff1a;<\/span><br \/>\n<span class=\"token comment\">&#8212; type: ref | key: idx_user_created | rows: 8972 | Extra: Using index condition<\/span><br \/>\n<span class=\"token comment\">&#8212; \u8bca\u65ad&#xff1a;status \u4e0d\u5728\u7d22\u5f15\u91cc \u2192 8972 \u884c\u5168\u90e8\u56de\u8868\u518d\u8fc7\u6ee4&#xff0c;\u884c\u6570\u8d8a\u591a\u8d8a\u6162<\/span><\/p>\n<p>\u5206\u6790&#xff1a;\u67e5\u8be2\u6761\u4ef6\u662f user_id \u7b49\u503c &#043; status \u7b49\u503c &#043; created_at \u6392\u5e8f\u3002\u6309\u300c\u7b49\u503c\u5217\u5728\u524d\u3001\u8303\u56f4\/\u6392\u5e8f\u5217\u5728\u540e\u300d\u91cd\u5efa\u8054\u5408\u7d22\u5f15&#xff1a;<\/p>\n<p><span class=\"token keyword\">ALTER<\/span> <span class=\"token keyword\">TABLE<\/span> orders <span class=\"token keyword\">ADD<\/span> <span class=\"token keyword\">INDEX<\/span> idx_user_status_created <span class=\"token punctuation\">(<\/span>user_id<span class=\"token punctuation\">,<\/span> <span class=\"token keyword\">status<\/span><span class=\"token punctuation\">,<\/span> created_at<span class=\"token punctuation\">)<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">ALTER<\/span> <span class=\"token keyword\">TABLE<\/span> orders <span class=\"token keyword\">DROP<\/span> <span class=\"token keyword\">INDEX<\/span> idx_user_created<span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">&#8212; \u4f18\u5316\u540e&#xff1a;<\/span><br \/>\n<span class=\"token comment\">&#8212; type: ref | key: idx_user_status_created | rows: 41 | Extra: Backward index scan<\/span><br \/>\n<span class=\"token comment\">&#8212; \u4e09\u5217\u5168\u90e8\u547d\u4e2d&#xff1a;\u5148\u6309 user_id&#043;status \u7cbe\u786e\u5b9a\u4f4d&#xff0c;created_at \u5728\u7d22\u5f15\u91cc\u5929\u7136\u6709\u5e8f \u2192 \u514d\u6392\u5e8f<\/span><br \/>\n<span class=\"token comment\">&#8212; \u7ebf\u4e0a\u5b9e\u6d4b 1.8s \u2192 12ms<\/span><\/p>\n<p>\u8bb0\u5fc6\u951a\u70b9&#xff1a;\u7d22\u5f15\u4f18\u5316\u4e09\u6b65\u6cd5\u3002 \u2460 EXPLAIN \u770b type\/key\/rows\/Extra \u56db\u4ef6\u5957 \u2192 \u2461 \u5224\u65ad\u300c\u6761\u4ef6\u5217\u6709\u6ca1\u6709\u90fd\u8fdb\u7d22\u5f15\u3001\u6392\u5e8f\u80fd\u4e0d\u80fd\u514d\u6389\u300d\u2192 \u2462 \u6309\u300c\u7b49\u503c\u5728\u524d\u3001\u8303\u56f4\u5728\u540e\u300d\u8c03\u6574\u8054\u5408\u7d22\u5f15\u5217\u987a\u5e8f\u3002\u4e5d\u6210\u6162 SQL \u8d70\u4e0d\u5b8c\u8fd9\u4e09\u6b65\u5c31\u89e3\u51b3\u4e86\u3002<\/p>\n<hr \/>\n<h3>\u516d\u3001\u4e8b\u52a1\u4e0e MVCC<\/h3>\n<p>\u8f6c\u8d26\u662f\u6700\u7ecf\u5178\u7684\u5f00\u573a&#xff1a;A \u6263 100\u3001B \u52a0 100&#xff0c;\u4e24\u6b65\u5fc5\u987b\u540c\u751f\u5171\u6b7b\u3002\u4e8b\u52a1\u89e3\u51b3\u7684\u6b63\u662f\u300c\u591a\u4e2a\u64cd\u4f5c\u6253\u5305\u6210\u4e00\u4e2a\u4e0d\u53ef\u5206\u5272\u7684\u5355\u5143\u300d&#xff0c;\u800c MVCC \u662f InnoDB \u5b9e\u73b0\u300c\u8bfb\u5199\u4e0d\u4e92\u76f8\u963b\u585e\u300d\u7684\u72ec\u95e8\u529f\u592b\u3002<\/p>\n<h4>6.1 ACID&#xff1a;\u56db\u4e2a\u7279\u6027\u5404\u9760\u4ec0\u4e48\u5b9e\u73b0<\/h4>\n<table>\n<tr>\u7279\u6027\u542b\u4e49InnoDB \u9760\u4ec0\u4e48<\/tr>\n<tbody>\n<tr>\n<td>\u539f\u5b50\u6027 Atomicity<\/td>\n<td>\u8981\u4e48\u5168\u505a\u3001\u8981\u4e48\u5168\u4e0d\u505a<\/td>\n<td>undo log&#xff08;\u56de\u6eda\u65e5\u5fd7&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u4e00\u81f4\u6027 Consistency<\/td>\n<td>\u6570\u636e\u4ece\u4e00\u4e2a\u5408\u6cd5\u72b6\u6001\u5230\u53e6\u4e00\u4e2a\u5408\u6cd5\u72b6\u6001<\/td>\n<td>\u5b83\u662f\u76ee\u7684&#xff0c;\u7531 A\u3001I\u3001D \u5171\u540c\u4fdd\u969c &#043; \u5e94\u7528\u5c42\u7ea6\u675f<\/td>\n<\/tr>\n<tr>\n<td>\u9694\u79bb\u6027 Isolation<\/td>\n<td>\u5e76\u53d1\u4e8b\u52a1\u4e92\u4e0d\u5e72\u6270<\/td>\n<td>MVCC &#043; \u9501<\/td>\n<\/tr>\n<tr>\n<td>\u6301\u4e45\u6027 Durability<\/td>\n<td>\u63d0\u4ea4\u4e86\u5c31\u6c38\u4e45\u751f\u6548&#xff0c;\u5b95\u673a\u4e5f\u4e0d\u4e22<\/td>\n<td>redo log&#xff08;WAL&#xff09;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&#xff08;\u8fd9\u4e00\u5217\u5c31\u662f\u7b2c 7\u30018 \u7ae0\u7684\u76ee\u5f55\u3002&#xff09;<\/p>\n<h4>6.2 \u5e76\u53d1\u4e09\u95ee\u9898\u4e0e\u56db\u4e2a\u9694\u79bb\u7ea7\u522b<\/h4>\n<p>\u4e24\u4e2a\u4e8b\u52a1\u5e76\u53d1\u8dd1&#xff0c;\u4f1a\u51fa\u73b0\u4e09\u79cd\u300c\u810f\u300d\u73b0\u8c61&#xff1a;\u810f\u8bfb&#xff08;\u8bfb\u5230\u4e86\u522b\u4eba\u6ca1\u63d0\u4ea4\u7684\u6570\u636e&#xff09;\u3001\u4e0d\u53ef\u91cd\u590d\u8bfb&#xff08;\u540c\u4e00\u4e8b\u52a1\u91cc\u4e24\u6b21\u8bfb\u540c\u4e00\u884c&#xff0c;\u503c\u53d8\u4e86&#xff09;\u3001\u5e7b\u8bfb&#xff08;\u540c\u4e00\u4e8b\u52a1\u91cc\u4e24\u6b21\u6309\u540c\u6837\u6761\u4ef6\u67e5&#xff0c;\u884c\u6570\u53d8\u4e86&#xff09;\u3002\u9694\u79bb\u7ea7\u522b\u5c31\u662f\u300c\u5141\u8bb8\u54ea\u79cd\u73b0\u8c61\u300d\u7684\u53d6\u820d&#xff1a;<\/p>\n<table>\n<tr>\u9694\u79bb\u7ea7\u522b\u810f\u8bfb\u4e0d\u53ef\u91cd\u590d\u8bfb\u5e7b\u8bfb<\/tr>\n<tbody>\n<tr>\n<td>READ UNCOMMITTED \u8bfb\u672a\u63d0\u4ea4<\/td>\n<td>\u53ef\u80fd<\/td>\n<td>\u53ef\u80fd<\/td>\n<td>\u53ef\u80fd<\/td>\n<\/tr>\n<tr>\n<td>READ COMMITTED \u8bfb\u5df2\u63d0\u4ea4<\/td>\n<td>\u4e0d\u53ef\u80fd<\/td>\n<td>\u53ef\u80fd<\/td>\n<td>\u53ef\u80fd<\/td>\n<\/tr>\n<tr>\n<td>REPEATABLE-READ \u53ef\u91cd\u590d\u8bfb&#xff08;\u9ed8\u8ba4&#xff09;<\/td>\n<td>\u4e0d\u53ef\u80fd<\/td>\n<td>\u4e0d\u53ef\u80fd<\/td>\n<td>\u90e8\u5206\u89e3\u51b3&#xff08;\u89c1 6.5&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>SERIALIZABLE \u4e32\u884c\u5316<\/td>\n<td>\u4e0d\u53ef\u80fd<\/td>\n<td>\u4e0d\u53ef\u80fd<\/td>\n<td>\u4e0d\u53ef\u80fd&#xff08;\u8bfb\u4e5f\u52a0\u9501&#xff0c;\u5e76\u53d1\u6700\u5dee&#xff09;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u5b9e\u6218\u6f14\u7ec3&#xff1a;\u8f6c\u8d26\u4e8b\u52a1 &#043; \u9694\u79bb\u7ea7\u522b<\/p>\n<p><span class=\"token keyword\">SELECT<\/span> &#064;<span class=\"token variable\">&#064;transaction_isolation<\/span><span class=\"token punctuation\">;<\/span>          <span class=\"token comment\">&#8212; \u67e5\u770b\u5f53\u524d\u9694\u79bb\u7ea7\u522b&#xff0c;\u9ed8\u8ba4 REPEATABLE-READ<\/span><br \/>\n<span class=\"token keyword\">SET<\/span> <span class=\"token keyword\">SESSION<\/span> <span class=\"token keyword\">TRANSACTION<\/span> <span class=\"token keyword\">ISOLATION<\/span> <span class=\"token keyword\">LEVEL<\/span> <span class=\"token keyword\">READ<\/span> <span class=\"token keyword\">COMMITTED<\/span><span class=\"token punctuation\">;<\/span>  <span class=\"token comment\">&#8212; \u53ea\u6539\u5f53\u524d\u4f1a\u8bdd<\/span><\/p>\n<p><span class=\"token comment\">&#8212; \u8f6c\u8d26&#xff1a;\u4e24\u4e2a\u7ec8\u7aef&#xff08;\u4f1a\u8bdd A \u6263\u6b3e\u3001\u4f1a\u8bdd B \u89c2\u5bdf&#xff09;\u6f14\u793a<\/span><br \/>\n<span class=\"token keyword\">BEGIN<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">UPDATE<\/span> accounts <span class=\"token keyword\">SET<\/span> balance <span class=\"token operator\">&#061;<\/span> balance <span class=\"token operator\">&#8211;<\/span> <span class=\"token number\">100<\/span> <span class=\"token keyword\">WHERE<\/span> id <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">;<\/span>  <span class=\"token comment\">&#8212; A&#xff1a;\u53ea\u6539\u4e0d\u63d0\u4ea4<\/span><br \/>\n<span class=\"token keyword\">UPDATE<\/span> accounts <span class=\"token keyword\">SET<\/span> balance <span class=\"token operator\">&#061;<\/span> balance <span class=\"token operator\">&#043;<\/span> <span class=\"token number\">100<\/span> <span class=\"token keyword\">WHERE<\/span> id <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">2<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">COMMIT<\/span><span class=\"token punctuation\">;<\/span>   <span class=\"token comment\">&#8212; \u6216 ROLLBACK; \u64a4\u9500\u4e00\u5207<\/span><br \/>\n<span class=\"token comment\">&#8212; \u4f1a\u8bdd B \u5728 A \u63d0\u4ea4\u524d\u67e5\u8be2&#xff1a;RU \u7ea7\u522b\u80fd\u8bfb\u5230\u672a\u63d0\u4ea4\u6570\u636e&#xff08;\u810f\u8bfb&#xff09;&#xff1b;RC\/RR \u8bfb\u5230\u7684\u4ecd\u662f\u65e7\u503c<\/span><\/p>\n<p><span class=\"token comment\">&#8212; SAVEPOINT&#xff1a;\u4e8b\u52a1\u91cc\u7684&#034;\u5b58\u6863\u70b9&#034;<\/span><br \/>\n<span class=\"token keyword\">BEGIN<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">UPDATE<\/span> products <span class=\"token keyword\">SET<\/span> stock <span class=\"token operator\">&#061;<\/span> stock <span class=\"token operator\">&#8211;<\/span> <span class=\"token number\">1<\/span> <span class=\"token keyword\">WHERE<\/span> id <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">SAVEPOINT<\/span> sp1<span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">UPDATE<\/span> products <span class=\"token keyword\">SET<\/span> stock <span class=\"token operator\">&#061;<\/span> stock <span class=\"token operator\">&#8211;<\/span> <span class=\"token number\">1<\/span> <span class=\"token keyword\">WHERE<\/span> id <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">2<\/span><span class=\"token punctuation\">;<\/span>  <span class=\"token comment\">&#8212; \u8fd9\u6b65\u505a\u9519\u4e86<\/span><br \/>\n<span class=\"token keyword\">ROLLBACK<\/span> <span class=\"token keyword\">TO<\/span> sp1<span class=\"token punctuation\">;<\/span>  <span class=\"token comment\">&#8212; \u53ea\u64a4\u9500\u7b2c\u4e8c\u6b65&#xff0c;\u7b2c\u4e00\u6b65\u4fdd\u7559<\/span><br \/>\n<span class=\"token keyword\">COMMIT<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<h4>6.3 MVCC \u4e09\u4ef6\u5957&#xff1a;\u9690\u85cf\u5217\u3001\u7248\u672c\u94fe\u3001ReadView<\/h4>\n<p>RR \u7ea7\u522b\u4e0b\u300c\u522b\u4eba\u6539\u4e86\u4f46\u6ca1\u63d0\u4ea4&#xff0c;\u6211\u8bfb\u5230\u7684\u8fd8\u662f\u65e7\u503c\u300d\u2014\u2014\u4e0d\u662f\u9501\u7b49\u5f85&#xff0c;\u662f **MVCC&#xff08;\u591a\u7248\u672c\u5e76\u53d1\u63a7\u5236&#xff09;**\u5728\u6084\u6084\u5de5\u4f5c\u3002\u5b83\u7531\u4e09\u4ef6\u5957\u7ec4\u6210&#xff1a;<\/p>\n<ul>\n<li>\u9690\u85cf\u5217&#xff1a;\u6bcf\u884c\u6570\u636e\u90fd\u6709\u4e24\u4e2a\u9690\u5f62\u5b57\u6bb5 DB_TRX_ID&#xff08;\u6700\u540e\u4fee\u6539\u5b83\u7684\u4e8b\u52a1 ID&#xff09;\u548c DB_ROLL_PTR&#xff08;\u56de\u6eda\u6307\u9488&#xff0c;\u6307\u5411 undo log \u91cc\u7684\u4e0a\u4e00\u7248\u672c&#xff09;&#xff1b;<\/li>\n<li>undo log \u7248\u672c\u94fe&#xff1a;\u6bcf\u6b21\u4fee\u6539\u90fd\u628a\u65e7\u7248\u672c\u5b58\u8fdb undo log&#xff0c;\u4e00\u884c\u6570\u636e\u7684\u5386\u53f2\u7248\u672c\u4e32\u6210\u94fe\u8868&#xff1b;<\/li>\n<li>ReadView&#xff08;\u8bfb\u89c6\u56fe&#xff09;&#xff1a;\u4e8b\u52a1\u53d1\u8d77\u5feb\u7167\u8bfb\u65f6\u751f\u6210\u7684\u300c\u540d\u5355\u300d&#xff0c;\u8bb0\u5f55\u6b64\u523b\u54ea\u4e9b\u4e8b\u52a1\u6d3b\u8dc3\u3001\u54ea\u4e9b\u5df2\u63d0\u4ea4\u3002<\/li>\n<\/ul>\n<p>  #mermaid-svg-6dnQ4a4ygnKdUD0D{font-family:\\&#8221;trebuchet ms\\&#8221;,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-6dnQ4a4ygnKdUD0D .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-6dnQ4a4ygnKdUD0D .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-6dnQ4a4ygnKdUD0D .error-icon{fill:#552222;}#mermaid-svg-6dnQ4a4ygnKdUD0D .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-6dnQ4a4ygnKdUD0D .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-6dnQ4a4ygnKdUD0D .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-6dnQ4a4ygnKdUD0D .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-6dnQ4a4ygnKdUD0D .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-6dnQ4a4ygnKdUD0D .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-6dnQ4a4ygnKdUD0D .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-6dnQ4a4ygnKdUD0D .marker{fill:#333333;stroke:#333333;}#mermaid-svg-6dnQ4a4ygnKdUD0D .marker.cross{stroke:#333333;}#mermaid-svg-6dnQ4a4ygnKdUD0D svg{font-family:\\&#8221;trebuchet ms\\&#8221;,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-6dnQ4a4ygnKdUD0D p{margin:0;}#mermaid-svg-6dnQ4a4ygnKdUD0D .label{font-family:\\&#8221;trebuchet ms\\&#8221;,verdana,arial,sans-serif;color:#333;}#mermaid-svg-6dnQ4a4ygnKdUD0D .cluster-label text{fill:#333;}#mermaid-svg-6dnQ4a4ygnKdUD0D .cluster-label span{color:#333;}#mermaid-svg-6dnQ4a4ygnKdUD0D .cluster-label span p{background-color:transparent;}#mermaid-svg-6dnQ4a4ygnKdUD0D .label text,#mermaid-svg-6dnQ4a4ygnKdUD0D span{fill:#333;color:#333;}#mermaid-svg-6dnQ4a4ygnKdUD0D .node rect,#mermaid-svg-6dnQ4a4ygnKdUD0D .node circle,#mermaid-svg-6dnQ4a4ygnKdUD0D .node ellipse,#mermaid-svg-6dnQ4a4ygnKdUD0D .node polygon,#mermaid-svg-6dnQ4a4ygnKdUD0D .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-6dnQ4a4ygnKdUD0D .rough-node .label text,#mermaid-svg-6dnQ4a4ygnKdUD0D .node .label text,#mermaid-svg-6dnQ4a4ygnKdUD0D .image-shape .label,#mermaid-svg-6dnQ4a4ygnKdUD0D .icon-shape .label{text-anchor:middle;}#mermaid-svg-6dnQ4a4ygnKdUD0D .node .katex path{fill:#000;stroke:#000;stroke-width:1px;}#mermaid-svg-6dnQ4a4ygnKdUD0D .rough-node .label,#mermaid-svg-6dnQ4a4ygnKdUD0D .node .label,#mermaid-svg-6dnQ4a4ygnKdUD0D .image-shape .label,#mermaid-svg-6dnQ4a4ygnKdUD0D .icon-shape .label{text-align:center;}#mermaid-svg-6dnQ4a4ygnKdUD0D .node.clickable{cursor:pointer;}#mermaid-svg-6dnQ4a4ygnKdUD0D .root .anchor path{fill:#333333!important;stroke-width:0;stroke:#333333;}#mermaid-svg-6dnQ4a4ygnKdUD0D .arrowheadPath{fill:#333333;}#mermaid-svg-6dnQ4a4ygnKdUD0D .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-6dnQ4a4ygnKdUD0D .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-6dnQ4a4ygnKdUD0D .edgeLabel{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-6dnQ4a4ygnKdUD0D .edgeLabel p{background-color:rgba(232,232,232, 0.8);}#mermaid-svg-6dnQ4a4ygnKdUD0D .edgeLabel rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-6dnQ4a4ygnKdUD0D .labelBkg{background-color:rgba(232, 232, 232, 0.5);}#mermaid-svg-6dnQ4a4ygnKdUD0D .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-6dnQ4a4ygnKdUD0D .cluster text{fill:#333;}#mermaid-svg-6dnQ4a4ygnKdUD0D .cluster span{color:#333;}#mermaid-svg-6dnQ4a4ygnKdUD0D div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:\\&#8221;trebuchet ms\\&#8221;,verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-6dnQ4a4ygnKdUD0D .flowchartTitleText{text-anchor:middle;font-size:18px;fill:#333;}#mermaid-svg-6dnQ4a4ygnKdUD0D rect.text{fill:none;stroke-width:0;}#mermaid-svg-6dnQ4a4ygnKdUD0D .icon-shape,#mermaid-svg-6dnQ4a4ygnKdUD0D .image-shape{background-color:rgba(232,232,232, 0.8);text-align:center;}#mermaid-svg-6dnQ4a4ygnKdUD0D .icon-shape p,#mermaid-svg-6dnQ4a4ygnKdUD0D .image-shape p{background-color:rgba(232,232,232, 0.8);padding:2px;}#mermaid-svg-6dnQ4a4ygnKdUD0D .icon-shape .label rect,#mermaid-svg-6dnQ4a4ygnKdUD0D .image-shape .label rect{opacity:0.5;background-color:rgba(232,232,232, 0.8);fill:rgba(232,232,232, 0.8);}#mermaid-svg-6dnQ4a4ygnKdUD0D .label-icon{display:inline-block;height:1em;overflow:visible;vertical-align:-0.125em;}#mermaid-svg-6dnQ4a4ygnKdUD0D .node .label-icon path{fill:currentColor;stroke:revert;stroke-width:revert;}#mermaid-svg-6dnQ4a4ygnKdUD0D :root{&#8211;mermaid-font-family:\\&#8221;trebuchet ms\\&#8221;,verdana,arial,sans-serif;}<\/p>\n<p>         <span class=\"edgeLabel\"><\/p>\n<p>roll_pointer<\/p>\n<p><\/span><\/p>\n<p>         <span class=\"edgeLabel\"><\/p>\n<p>roll_pointer<\/p>\n<p><\/span><\/p>\n<p>         <span class=\"nodeLabel\"><\/p>\n<p>\u5f53\u524d\u7248\u672cbalance &#061; 900trx_id &#061; 300<\/p>\n<p><\/span><\/p>\n<p>         <span class=\"nodeLabel\"><\/p>\n<p>undo \u7248\u672c 2balance &#061; 1000trx_id &#061; 200<\/p>\n<p><\/span><\/p>\n<p>         <span class=\"nodeLabel\"><\/p>\n<p>undo \u7248\u672c 1&#xff08;\u521d\u59cb&#xff09;balance &#061; 1000trx_id &#061; 100<\/p>\n<p><\/span><\/p>\n<p>\u53ef\u89c1\u6027\u5224\u65ad\u89c4\u5219&#xff08;\u7b80\u5316\u8bb0\u5fc6\u7248&#xff09;\u2014\u2014\u62ff\u7248\u672c\u7684 trx_id \u548c ReadView \u6bd4\u5927\u5c0f&#xff1a;<\/p>\n<li>\u7b49\u4e8e\u300c\u6211\u81ea\u5df1\u300d\u7684 trx_id \u2192 \u53ef\u89c1&#xff08;\u81ea\u5df1\u6539\u7684\u5f53\u7136\u80fd\u770b&#xff09;&#xff1b;<\/li>\n<li>\u5c0f\u4e8e\u6d3b\u8dc3\u4e8b\u52a1\u4e0b\u9650 \u2192 \u53ef\u89c1&#xff08;\u6211\u62cd\u7167\u524d\u65e9\u5c31\u63d0\u4ea4\u4e86&#xff09;&#xff1b;<\/li>\n<li>\u5927\u4e8e\u7b49\u4e8e\u6d3b\u8dc3\u4e8b\u52a1\u4e0a\u9650 \u2192 \u4e0d\u53ef\u89c1&#xff08;\u6211\u62cd\u7167\u4e4b\u540e\u624d\u5f00\u7684\u4e8b\u52a1&#xff09;&#xff1b;<\/li>\n<li>\u843d\u5728\u533a\u95f4\u5185&#xff1a;\u5728\u6d3b\u8dc3\u540d\u5355\u91cc \u2192 \u4e0d\u53ef\u89c1&#xff08;\u8fd8\u6ca1\u63d0\u4ea4&#xff09;&#xff1b;\u4e0d\u5728\u540d\u5355\u91cc \u2192 \u53ef\u89c1&#xff08;\u5df2\u63d0\u4ea4&#xff09;\u3002<\/li>\n<p>\u4e0d\u53ef\u89c1\u5c31\u987a\u7740 roll_pointer \u8d70\u5230\u4e0a\u4e00\u7248\u672c&#xff0c;\u91cd\u590d\u5224\u65ad&#xff0c;\u76f4\u5230\u627e\u5230\u53ef\u89c1\u7248\u672c\u6216\u94fe\u5c3e\u3002\u4e00\u53e5\u53e3\u8bc0&#xff1a;\u300c\u5c0f\u4e8e\u4e0b\u9650\u53ef\u89c1&#xff0c;\u8d85\u8fc7\u4e0a\u9650\u4e0d\u53ef\u89c1&#xff0c;\u4e2d\u95f4\u67e5\u540d\u5355\u300d\u3002<\/p>\n<p>\u8bb0\u5fc6\u951a\u70b9&#xff1a;RC \u548c RR \u7684\u672c\u8d28\u533a\u522b\u53ea\u6709\u4e00\u53e5\u8bdd\u3002 RC&#xff1a;\u6bcf\u6b21\u5feb\u7167\u8bfb\u90fd\u751f\u6210\u65b0 ReadView \u2192 \u80fd\u770b\u5230\u522b\u7684\u4e8b\u52a1\u65b0\u63d0\u4ea4\u7684\u6570\u636e \u2192 \u4e0d\u53ef\u91cd\u590d\u8bfb\u3002 RR&#xff1a;\u53ea\u5728\u4e8b\u52a1\u7b2c\u4e00\u6b21\u67e5\u8be2\u65f6\u751f\u6210 ReadView&#xff0c;\u4e4b\u540e\u4e00\u76f4\u590d\u7528 \u2192 \u6574\u4e2a\u4e8b\u52a1\u770b\u5230\u7684\u4e16\u754c\u5b9a\u683c\u5728\u5f00\u5934 \u2192 \u53ef\u91cd\u590d\u8bfb\u3002 \u6240\u6709\u9694\u79bb\u7ea7\u522b\u5dee\u5f02&#xff0c;\u90fd\u662f\u8fd9\u4e00\u53e5\u8bdd\u7684\u63a8\u8bba\u3002<\/p>\n<h4>6.4 \u5feb\u7167\u8bfb\u4e0e\u5f53\u524d\u8bfb<\/h4>\n<ul>\n<li>\u5feb\u7167\u8bfb&#xff1a;\u666e\u901a SELECT&#xff0c;\u8d70 MVCC&#xff0c;\u4e0d\u52a0\u9501&#xff0c;\u8bfb\u7684\u662f\u5feb\u7167&#xff1b;<\/li>\n<li>\u5f53\u524d\u8bfb&#xff1a;SELECT &#8230; FOR UPDATE\u3001SELECT &#8230; FOR SHARE&#xff08;8.0 \u5199\u6cd5&#xff0c;\u65e7\u7248 LOCK IN SHARE MODE&#xff09;\u4ee5\u53ca\u6240\u6709 UPDATE \/ DELETE\u2014\u2014\u8bfb\u7684\u662f\u6700\u65b0\u7248\u672c&#xff0c;\u5e76\u52a0\u9501\u3002<\/li>\n<\/ul>\n<p>\u4e3a\u4ec0\u4e48 UPDATE \u5fc5\u987b\u300c\u5f53\u524d\u8bfb\u300d&#xff1f;\u5982\u679c\u4e0d\u8bfb\u6700\u65b0\u503c&#xff0c;\u4e24\u4e2a\u4e8b\u52a1\u57fa\u4e8e\u540c\u4e00\u4e2a\u65e7\u5feb\u7167\u5404\u81ea &#043;1&#xff0c;\u7ed3\u679c\u4f1a\u4e22\u4e00\u6b21\u66f4\u65b0\u3002\u6539\u6570\u636e\u6c38\u8fdc\u4ee5\u6700\u65b0\u7248\u672c\u4e3a\u51c6\u3002<\/p>\n<h4>6.5 RR \u5230\u5e95\u6709\u6ca1\u6709\u89e3\u51b3\u5e7b\u8bfb<\/h4>\n<p>\u6807\u51c6\u7b54\u6848&#xff1a;InnoDB \u7684 RR \u5728\u5f88\u5927\u7a0b\u5ea6\u4e0a\u89e3\u51b3\u4e86\u5e7b\u8bfb&#xff0c;\u4f46\u6ca1\u5835\u6b7b&#xff1a;<\/p>\n<ul>\n<li>\u5feb\u7167\u8bfb&#xff1a;\u9760 MVCC&#xff0c;\u4e8b\u52a1\u5185\u591a\u6b21\u67e5\u8be2\u7ed3\u679c\u4e00\u81f4&#xff0c;\u5929\u7136\u65e0\u5e7b\u8bfb&#xff1b;<\/li>\n<li>\u5f53\u524d\u8bfb&#xff1a;\u9760**\u4e34\u952e\u9501&#xff08;Next-Key Lock&#xff09;**\u9501\u4f4f\u300c\u5df2\u6709\u8bb0\u5f55 &#043; \u8bb0\u5f55\u95f4\u7684\u95f4\u9699\u300d&#xff0c;\u522b\u7684\u4e8b\u52a1\u63d2\u4e0d\u8fdb\u6765&#xff08;\u7b2c 7 \u7ae0\u5c55\u5f00&#xff09;&#xff1b;<\/li>\n<li>\u6f0f\u6d1e\u573a\u666f&#xff1a;\u4e8b\u52a1\u5148\u5feb\u7167\u8bfb&#xff08;\u672a\u52a0\u9501&#xff09;&#xff0c;\u522b\u7684\u4e8b\u52a1\u63d2\u5165\u4e00\u884c\u5e76\u63d0\u4ea4&#xff0c;\u672c\u4e8b\u52a1\u518d\u5f53\u524d\u8bfb\u2014\u2014\u8fd9\u6279\u300c\u5e7b\u5f71\u884c\u300d\u5c31\u51fa\u73b0\u4e86\u3002\u8981\u5f7b\u5e95\u907f\u514d&#xff0c;\u7b2c\u4e00\u6b21\u67e5\u8be2\u5c31\u7528 FOR UPDATE\u3002<\/li>\n<\/ul>\n<hr \/>\n<h3>\u4e03\u3001\u9501&#xff1a;\u4ece MDL \u5230\u95f4\u9699\u9501<\/h3>\n<p>\u9501\u662f\u9694\u79bb\u6027\u7684\u53e6\u4e00\u534a\u3002MVCC \u8ba9\u300c\u8bfb\u300d\u4e0d\u963b\u585e&#xff0c;\u9501\u8d1f\u8d23\u300c\u5199\u4e0e\u5199\u300d\u6392\u961f\u3002\u8fd9\u4e00\u7ae0\u6309\u7c92\u5ea6\u4ece\u5927\u5230\u5c0f\u8fc7\u4e00\u904d&#xff0c;\u6700\u540e\u7528\u6b7b\u9501\u590d\u73b0\u548c\u300c\u8d85\u5356\u300d\u4e09\u6bb5\u5f0f\u6536\u5c3e\u2014\u2014\u90fd\u662f\u771f\u5b9e\u7ebf\u4e0a\u4f1a\u75bc\u7684\u6848\u4f8b\u3002<\/p>\n<h4>7.1 \u9501\u7684\u5168\u666f&#xff1a;\u4e00\u5f20\u8868\u770b\u5168<\/h4>\n<table>\n<tr>\u7c92\u5ea6\u9501\u4e00\u53e5\u8bdd\u804c\u8d23<\/tr>\n<tbody>\n<tr>\n<td>\u5168\u5c40<\/td>\n<td>FTWRL \u5168\u5c40\u8bfb\u9501<\/td>\n<td>\u6574\u5e93\u53ea\u8bfb&#xff0c;\u903b\u8f91\u5907\u4efd\u7528<\/td>\n<\/tr>\n<tr>\n<td>\u8868\u7ea7<\/td>\n<td>LOCK TABLES \u8868\u9501<\/td>\n<td>\u663e\u5f0f\u9501\u8868&#xff0c;\u884c\u9501\u65f6\u4ee3\u57fa\u672c\u4e0d\u7528<\/td>\n<\/tr>\n<tr>\n<td>\u8868\u7ea7<\/td>\n<td>MDL \u5143\u6570\u636e\u9501<\/td>\n<td>DDL \u4e0e DML \u4e92\u65a5\u7684\u9690\u5f62\u4fdd\u9556&#xff0c;\u6700\u5bb9\u6613\u51fa\u7ebf\u4e0a\u4e8b\u6545<\/td>\n<\/tr>\n<tr>\n<td>\u8868\u7ea7<\/td>\n<td>\u610f\u5411\u9501 IS \/ IX<\/td>\n<td>\u884c\u9501\u524d\u5148\u5728\u8868\u4e0a\u300c\u6302\u53f7\u300d&#xff0c;\u907f\u514d\u9010\u884c\u68c0\u67e5\u51b2\u7a81<\/td>\n<\/tr>\n<tr>\n<td>\u8868\u7ea7<\/td>\n<td>AUTO-INC \u9501<\/td>\n<td>\u4fdd\u8bc1\u81ea\u589e\u503c\u5206\u914d\u4e0d\u91cd\u53f7<\/td>\n<\/tr>\n<tr>\n<td>\u884c\u7ea7<\/td>\n<td>Record Lock \u8bb0\u5f55\u9501<\/td>\n<td>\u9501\u4f4f\u7d22\u5f15\u4e0a\u7684\u5355\u6761\u8bb0\u5f55<\/td>\n<\/tr>\n<tr>\n<td>\u884c\u7ea7<\/td>\n<td>Gap Lock \u95f4\u9699\u9501<\/td>\n<td>\u9501\u4f4f\u4e24\u6761\u8bb0\u5f55\u4e4b\u95f4\u7684\u7a7a\u9699&#xff0c;\u4e13\u9632\u63d2\u5165<\/td>\n<\/tr>\n<tr>\n<td>\u884c\u7ea7<\/td>\n<td>Next-Key Lock \u4e34\u952e\u9501<\/td>\n<td>\u8bb0\u5f55\u9501 &#043; \u95f4\u9699\u9501&#xff0c;RR \u9632\u5e7b\u8bfb\u7684\u4e3b\u529b<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>7.2 \u5168\u5c40\u9501&#xff1a;\u5907\u4efd\u7684\u6b63\u786e\u59ff\u52bf<\/h4>\n<p>FLUSH <span class=\"token keyword\">TABLES<\/span> <span class=\"token keyword\">WITH<\/span> <span class=\"token keyword\">READ<\/span> <span class=\"token keyword\">LOCK<\/span><span class=\"token punctuation\">;<\/span>  <span class=\"token comment\">&#8212; \u5168\u5e93\u53ea\u8bfb&#xff1a;MyISAM \u65f6\u4ee3\u7684\u5907\u4efd\u65b9\u6cd5<\/span><br \/>\n<span class=\"token keyword\">UNLOCK<\/span> <span class=\"token keyword\">TABLES<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">&#8212; InnoDB \u65f6\u4ee3&#xff1a;\u9760 MVCC \u5feb\u7167\u62ff\u5230\u4e00\u81f4\u6027\u89c6\u56fe&#xff0c;\u5b8c\u5168\u4e0d\u963b\u585e\u4e1a\u52a1<\/span><br \/>\nmysqldump <span class=\"token comment\">&#8211;single-transaction -uroot -p mall &gt; mall_$(date &#043;%F).sql<\/span><\/p>\n<h4>7.3 MDL \u9501&#xff1a;\u6700\u9690\u853d\u7684\u7ebf\u4e0a\u4e8b\u6545<\/h4>\n<p>\u5bf9\u8868\u505a\u589e\u5220\u6539\u67e5\u65f6\u81ea\u52a8\u52a0 MDL\u8bfb\u9501&#xff0c;\u505a ALTER \u65f6\u9700\u8981 MDL\u5199\u9501&#xff0c;\u8bfb\u5199\u4e92\u65a5\u3002\u7ecf\u5178\u7ffb\u8f66\u73b0\u573a&#xff1a;<\/p>\n<p>\u4f1a\u8bdd A&#xff1a;BEGIN; SELECT * FROM users WHERE id &#061; 1;   &#8212; \u62ff\u5230 MDL \u8bfb\u9501&#xff0c;\u4e8b\u52a1\u6302\u7740\u6ca1\u63d0\u4ea4<br \/>\n\u4f1a\u8bdd B&#xff1a;ALTER TABLE users ADD COLUMN vip INT;      &#8212; \u9700\u8981 MDL \u5199\u9501 \u2192 \u6392\u961f\u7b49 A<br \/>\n\u4f1a\u8bdd C&#xff1a;SELECT * FROM users WHERE id &#061; 2;          &#8212; \u53ea\u8981 MDL \u8bfb\u9501&#xff0c;\u5374\u88ab B \u6321\u5728\u540e\u9762<br \/>\n&#8212; \u7ed3\u679c&#xff1a;\u8fd9\u5f20\u8868\u7684\u6240\u6709\u540e\u7eed\u67e5\u8be2\u5168\u90e8\u5806\u79ef&#xff0c;\u4e1a\u52a1&#034;\u5047\u6b7b&#034;<\/p>\n<ul>\n<li>\u9884\u9632\u4e00&#xff1a;DDL \u524d\u5148\u67e5\u957f\u4e8b\u52a1&#xff0c;\u6709\u6302\u7740\u7684\u4e8b\u52a1\u5c31\u522b\u52a8\u8868&#xff1a;SELECT * FROM information_schema.innodb_trx ORDER BY trx_started;<\/li>\n<li>\u9884\u9632\u4e8c&#xff1a;\u628a MDL \u7b49\u5f85\u4e0a\u9650\u8c03\u5c0f&#xff0c;\u522b\u7528\u9ed8\u8ba4\u503c&#xff08;\u9ed8\u8ba4\u7ea6\u4e00\u5e74&#xff09;&#xff1a;SET GLOBAL lock_wait_timeout &#061; 10;\u2014\u2014\u62ff\u4e0d\u5230\u5199\u9501\u5c31\u5931\u8d25\u64a4\u9000&#xff0c;\u800c\u4e0d\u662f\u628a\u8868\u5835\u6b7b&#xff1b;<\/li>\n<li>\u9884\u9632\u4e09&#xff1a;\u4f4e\u5cf0\u671f\u505a DDL&#xff0c;\u5e76\u5148\u5728\u9884\u53d1\u73af\u5883\u6f14\u7ec3\u3002<\/li>\n<\/ul>\n<h4>7.4 \u884c\u9501\u4e09\u5144\u5f1f&#xff1a;\u52a0\u5728\u7d22\u5f15\u4e0a\u7684\u9501<\/h4>\n<p>\u5047\u8bbe\u8868\u91cc\u6709 id \u4e3a 1\u30015\u300110\u300115 \u56db\u884c&#xff08;id \u6709\u7d22\u5f15&#xff09;&#xff0c;RR \u7ea7\u522b\u4e0b\u7684\u52a0\u9501\u89c4\u5219&#xff1a;<\/p>\n<table>\n<tr>\u8bed\u53e5\u573a\u666f\u52a0\u4ec0\u4e48\u9501\u9501\u4f4f\u7684\u8303\u56f4<\/tr>\n<tbody>\n<tr>\n<td>\u552f\u4e00\u7d22\u5f15\u7b49\u503c&#xff0c;\u547d\u4e2d&#xff08;id &#061; 10&#xff09;<\/td>\n<td>\u8bb0\u5f55\u9501<\/td>\n<td>\u53ea\u6709 id&#061;10 \u8fd9\u4e00\u884c<\/td>\n<\/tr>\n<tr>\n<td>\u552f\u4e00\u7d22\u5f15\u7b49\u503c&#xff0c;\u672a\u547d\u4e2d&#xff08;id &#061; 7&#xff09;<\/td>\n<td>\u95f4\u9699\u9501<\/td>\n<td>(5, 10) \u4e4b\u95f4\u7684\u7a7a\u9699&#xff0c;\u7981\u6b62\u63d2\u5165<\/td>\n<\/tr>\n<tr>\n<td>\u666e\u901a\u7d22\u5f15\u7b49\u503c&#xff08;idx \u4e0a age &#061; 10&#xff09;<\/td>\n<td>\u4e34\u952e\u9501 &#043; \u95f4\u9699\u9501<\/td>\n<td>(5, 10] \u53ca\u4e0b\u4e00\u4e2a\u95f4\u9699&#xff0c;\u9632\u5e7b\u8bfb<\/td>\n<\/tr>\n<tr>\n<td>\u8303\u56f4\u6761\u4ef6&#xff08;id &gt; 10&#xff09;<\/td>\n<td>\u4e34\u952e\u9501<\/td>\n<td>(10, 15]\u3001(15, &#043;\u221e) \u4e00\u8def\u9501\u8fc7\u53bb<\/td>\n<\/tr>\n<tr>\n<td>\u6761\u4ef6\u5217\u6ca1\u6709\u7d22\u5f15<\/td>\n<td>\u5168\u90e8\u8bb0\u5f55\u52a0\u4e34\u952e\u9501<\/td>\n<td>\u6548\u679c \u2248 \u9501\u5168\u8868&#xff08;\u6700\u5371\u9669&#xff09;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u6613\u9519\u70b9&#xff1a;\u884c\u9501\u52a0\u5728\u7d22\u5f15\u4e0a&#xff0c;\u7d22\u5f15\u5931\u6548\u7b49\u4e8e\u9501\u5168\u8868\u3002 UPDATE orders SET status &#061; 4 WHERE order_no &#061; &#039;X1&#039;;\u2014\u2014order_no \u6ca1\u7d22\u5f15\u65f6&#xff0c;InnoDB \u5168\u8868\u626b\u63cf&#xff0c;\u628a\u6bcf\u4e00\u884c\u90fd\u9501\u4f4f&#xff0c;\u5176\u4ed6\u4e8b\u52a1\u7684\u4efb\u4f55\u66f4\u65b0\u5168\u90e8\u6392\u961f\u3002\u300c\u5220\u4e86\u4e00\u4e2a\u6ca1\u8d70\u7d22\u5f15\u7684 WHERE\u300d\u548c\u300c\u9501\u5168\u8868\u300d\u662f\u540c\u4e00\u4ef6\u4e8b&#xff0c;\u7ebf\u4e0a\u5927\u6279\u91cf UPDATE \u524d\u5148 EXPLAIN \u4e00\u904d\u3002<\/p>\n<h4>7.5 \u6b7b\u9501\u590d\u73b0\u4e0e\u6392\u67e5<\/h4>\n<p>&#8212; \u4f1a\u8bdd A                                &#8212; \u4f1a\u8bdd B<br \/>\nBEGIN;                                    BEGIN;<br \/>\nUPDATE products SET stock &#061; stock-1 WHERE id &#061; 1;<br \/>\n                                          UPDATE products SET stock &#061; stock-1 WHERE id &#061; 2;<br \/>\nUPDATE products SET stock &#061; stock-1 WHERE id &#061; 2;             &#8212; A \u7b49 B \u653e id&#061;2<br \/>\n                                          UPDATE products SET stock &#061; stock-1 WHERE id &#061; 1;  &#8212; B \u7b49 A \u653e id&#061;1<br \/>\n                                          &#8212; ERROR 1213 (40001): Deadlock found &#8230;&#xff08;B \u88ab\u9009\u4e3a\u727a\u7272\u8005\u56de\u6eda&#xff09;<\/p>\n<p><span class=\"token keyword\">SHOW<\/span> <span class=\"token keyword\">ENGINE<\/span> <span class=\"token keyword\">INNODB<\/span> <span class=\"token keyword\">STATUS<\/span>\\\\G<br \/>\n<span class=\"token comment\">&#8212; \u627e &#034;LATEST DETECTED DEADLOCK&#034; \u6bb5&#xff1a;\u4e24\u4e2a\u4e8b\u52a1\u5404\u6301\u6709\u4ec0\u4e48\u9501\u3001\u5728\u7b49\u4ec0\u4e48\u9501\u3001\u6700\u540e\u6267\u884c\u7684 SQL<\/span><\/p>\n<ul>\n<li>InnoDB \u9ed8\u8ba4\u5f00\u542f\u6b7b\u9501\u68c0\u6d4b&#xff08;innodb_deadlock_detect&#061;ON&#xff09;&#xff0c;\u53d1\u73b0\u56de\u73af\u7acb\u5373\u56de\u6eda\u4ee3\u4ef7\u5c0f\u7684\u4e00\u65b9&#xff0c;\u901a\u5e38\u6beb\u79d2\u7ea7\u89e3\u5f00&#xff1b;<\/li>\n<li>\u62ff\u4e0d\u5230\u9501\u7684\u7b49\u5f85\u7531 innodb_lock_wait_timeout&#xff08;\u9ed8\u8ba4 50s&#xff09;\u63a7\u5236&#xff0c;\u8d85\u65f6\u62a5 ERROR 1205&#xff1b;<\/li>\n<li>\u9632\u6b7b\u9501\u56db\u62db&#xff1a;\u591a\u884c\u66f4\u65b0\u56fa\u5b9a\u987a\u5e8f&#xff08;\u5982\u6309\u4e3b\u952e\u5347\u5e8f&#xff09;\u3001\u4e8b\u52a1\u5c3d\u91cf\u77ed\u3001\u7528\u6761\u4ef6\u66f4\u65b0\u4ee3\u66ff\u300c\u5148\u67e5\u540e\u6539\u300d\u3001\u70ed\u70b9\u884c\u7528\u961f\u5217\/Redis \u524a\u5cf0\u800c\u4e0d\u662f\u786c\u603c\u6570\u636e\u5e93\u3002<\/li>\n<\/ul>\n<h4>7.6 \u5b9e\u6218&#xff1a;\u8d85\u5356\u95ee\u9898\u7684\u4e09\u79cd\u89e3\u6cd5<\/h4>\n<p>\u79d2\u6740\u6263\u5e93\u5b58&#xff0c;\u540c\u4e00\u4e2a\u5546\u54c1 1000 \u4e2a\u8bf7\u6c42\u540c\u65f6\u6765&#xff0c;\u5e93\u5b58\u53ea\u6709 100\u2014\u2014\u4e09\u6bb5\u6f14\u8fdb&#xff1a;<\/p>\n<p><span class=\"token comment\">&#8212; \u7248\u672c 1&#xff1a;\u5148\u67e5\u540e\u6539&#xff08;\u5fc5\u8d85\u5356&#xff09;<\/span><br \/>\n<span class=\"token comment\">&#8212; stock &#061; SELECT stock FROM products WHERE id &#061; 1;  \u2192 \u5e94\u7528\u5224\u65ad stock &gt;&#061; 1<\/span><br \/>\n<span class=\"token comment\">&#8212; UPDATE products SET stock &#061; stock &#8211; 1 WHERE id &#061; 1;<\/span><br \/>\n<span class=\"token comment\">&#8212; \u5e76\u53d1\u4e0b\u4e24\u4e2a\u8bf7\u6c42\u540c\u65f6\u8bfb\u5230 stock&#061;1\u3001\u540c\u65f6\u901a\u8fc7\u5224\u65ad \u2192 \u5356\u51fa 2 \u4ef6&#xff0c;\u5e93\u5b58\u53ea\u6709 1<\/span><\/p>\n<p><span class=\"token comment\">&#8212; \u7248\u672c 2&#xff1a;\u6761\u4ef6\u66f4\u65b0&#xff08;\u63a8\u8350&#xff0c;\u672c\u8d28\u662f\u4e50\u89c2\u9501\u601d\u60f3&#xff09;<\/span><br \/>\n<span class=\"token keyword\">UPDATE<\/span> products <span class=\"token keyword\">SET<\/span> stock <span class=\"token operator\">&#061;<\/span> stock <span class=\"token operator\">&#8211;<\/span> <span class=\"token number\">1<\/span> <span class=\"token keyword\">WHERE<\/span> id <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">1<\/span> <span class=\"token operator\">AND<\/span> stock <span class=\"token operator\">&gt;&#061;<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token comment\">&#8212; affected_rows &#061; 0 \u2192 \u6ca1\u62a2\u5230&#xff0c;\u8fd4\u56de&#034;\u5df2\u552e\u7f44&#034;&#xff1b;\u5224\u65ad\u8fdb\u5165 WHERE&#xff0c;\u9760\u884c\u9501\u4fdd\u8bc1\u539f\u5b50\u6027<\/span><\/p>\n<p><span class=\"token comment\">&#8212; \u7248\u672c 3&#xff1a;\u60b2\u89c2\u9501 SELECT &#8230; FOR UPDATE&#xff08;\u9002\u5408\u8fd8\u8981\u8054\u52a8\u591a\u8868\u7684\u590d\u6742\u6263\u51cf&#xff09;<\/span><br \/>\n<span class=\"token keyword\">BEGIN<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">SELECT<\/span> stock <span class=\"token keyword\">FROM<\/span> products <span class=\"token keyword\">WHERE<\/span> id <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">1<\/span> <span class=\"token keyword\">FOR<\/span> <span class=\"token keyword\">UPDATE<\/span><span class=\"token punctuation\">;<\/span>  <span class=\"token comment\">&#8212; \u9501\u4f4f&#xff0c;\u522b\u4eba\u8fdb\u4e0d\u6765<\/span><br \/>\n<span class=\"token comment\">&#8212; &#8230; \u5e94\u7528\u5c42\u4e1a\u52a1\u6821\u9a8c\u3001\u5199\u8ba2\u5355\u3001\u5199\u660e\u7ec6 &#8230;<\/span><br \/>\n<span class=\"token keyword\">UPDATE<\/span> products <span class=\"token keyword\">SET<\/span> stock <span class=\"token operator\">&#061;<\/span> stock <span class=\"token operator\">&#8211;<\/span> <span class=\"token number\">1<\/span> <span class=\"token keyword\">WHERE<\/span> id <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">COMMIT<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">&#8212; \u4e50\u89c2\u9501\u901a\u7528\u5f62\u6001&#xff1a;version \u53f7<\/span><br \/>\n<span class=\"token keyword\">UPDATE<\/span> docs <span class=\"token keyword\">SET<\/span> content <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#039;\u65b0\u5185\u5bb9&#039;<\/span><span class=\"token punctuation\">,<\/span> version <span class=\"token operator\">&#061;<\/span> version <span class=\"token operator\">&#043;<\/span> <span class=\"token number\">1<\/span><br \/>\n<span class=\"token keyword\">WHERE<\/span> id <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">1<\/span> <span class=\"token operator\">AND<\/span> version <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">5<\/span><span class=\"token punctuation\">;<\/span>  <span class=\"token comment\">&#8212; \u547d\u4e2d 0 \u884c &#061; \u522b\u4eba\u5148\u6539\u4e86&#xff0c;\u91cd\u8bfb\u91cd\u8bd5<\/span><\/p>\n<p>\u8bb0\u5fc6\u951a\u70b9&#xff1a;\u4ec0\u4e48\u65f6\u5019\u7528\u54ea\u79cd\u9501\u3002 \u51b2\u7a81\u5c11 \u2192 \u4e50\u89c2\u9501&#xff08;version \/ \u6761\u4ef6\u66f4\u65b0&#xff09;&#xff0c;\u65e0\u9501\u9ad8\u6027\u80fd&#xff0c;\u5931\u8d25\u91cd\u8bd5&#xff1b;\u51b2\u7a81\u6fc0\u70c8 \u2192 \u60b2\u89c2\u9501&#xff08;FOR UPDATE&#xff09;&#xff0c;\u4e00\u6b21\u9501\u5230\u4f4d\u907f\u514d\u53cd\u590d\u91cd\u8bd5\u6253\u7206 CPU&#xff1b;\u79d2\u6740\u7ea7\u70ed\u70b9 \u2192 \u6570\u636e\u5e93\u9501\u90fd\u4e0d\u5408\u9002&#xff0c;\u4e0a Redis \u9884\u6263 &#043; MQ \u5f02\u6b65\u843d\u5e93\u3002<\/p>\n<hr \/>\n<h3>\u516b\u3001\u65e5\u5fd7\u7cfb\u7edf&#xff1a;redo \/ undo \/ binlog<\/h3>\n<p>\u7b2c 6 \u7ae0\u8868\u8bf4\u8fc7&#xff1a;\u539f\u5b50\u6027\u9760 undo\u3001\u6301\u4e45\u6027\u9760 redo\u3002\u8fd9\u4e00\u7ae0\u628a\u4e09\u672c\u65e5\u5fd7\u5f7b\u5e95\u8bb2\u900f&#xff0c;\u518d\u56de\u7b54\u90a3\u4e2a\u7ecf\u5178\u95ee\u9898\u2014\u2014\u4e3a\u4ec0\u4e48\u66f4\u65b0\u4e00\u6761\u6570\u636e\u8981\u5199\u4e24\u6b21\u65e5\u5fd7&#xff08;\u4e24\u9636\u6bb5\u63d0\u4ea4&#xff09;\u3002<\/p>\n<h4>8.1 \u4e09\u672c\u65e5\u5fd7\u5404\u7ba1\u4ec0\u4e48<\/h4>\n<table>\n<tr>\u7ef4\u5ea6redo logundo logbinlog<\/tr>\n<tbody>\n<tr>\n<td>\u6240\u5c5e\u5c42<\/td>\n<td>InnoDB<\/td>\n<td>InnoDB<\/td>\n<td>Server \u5c42&#xff08;\u6240\u6709\u5f15\u64ce\u90fd\u6709&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>\u6838\u5fc3\u4f5c\u7528<\/td>\n<td>\u5d29\u6e83\u6062\u590d&#xff0c;\u4fdd\u6301\u4e45\u6027<\/td>\n<td>\u56de\u6eda &#043; MVCC \u7248\u672c\u94fe<\/td>\n<td>\u4e3b\u4ece\u590d\u5236\u3001\u6570\u636e\u5f52\u6863\u6062\u590d<\/td>\n<\/tr>\n<tr>\n<td>\u8bb0\u5f55\u5185\u5bb9<\/td>\n<td>\u7269\u7406\u65e5\u5fd7&#xff1a;\u300c\u67d0\u9875\u67d0\u4f4d\u7f6e\u6539\u6210\u4e86\u4ec0\u4e48\u300d<\/td>\n<td>\u903b\u8f91\u65e5\u5fd7&#xff1a;\u53cd\u5411\u64cd\u4f5c&#xff08;INSERT \u8bb0 DELETE&#xff09;<\/td>\n<td>\u903b\u8f91\u65e5\u5fd7&#xff1a;SQL \u8bed\u53e5\u6216\u884c\u53d8\u66f4\u955c\u50cf<\/td>\n<\/tr>\n<tr>\n<td>\u5199\u5165\u65b9\u5f0f<\/td>\n<td>\u56fa\u5b9a\u5927\u5c0f&#xff0c;\u5faa\u73af\u5199<\/td>\n<td>\u968f\u4e8b\u52a1\u4ea7\u751f&#xff0c;\u63d0\u4ea4\u540e\u53ef\u6e05\u7406<\/td>\n<td>\u8ffd\u52a0\u5199&#xff0c;\u5199\u6ee1\u6362\u4e0b\u4e00\u4e2a\u6587\u4ef6<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>8.2 redo log&#xff1a;WAL \u7684\u7cbe\u9ad3<\/h4>\n<p>\u4e3a\u4ec0\u4e48\u6539\u6570\u636e\u4e0d\u76f4\u63a5\u5237\u76d8&#xff1f;\u6570\u636e\u9875\u5728\u78c1\u76d8\u4e0a\u968f\u673a\u5206\u6563&#xff0c;\u5237\u9875\u662f\u968f\u673a IO&#xff1b;\u800c\u65e5\u5fd7\u662f\u987a\u5e8f\u8ffd\u52a0&#xff0c;\u5feb\u51e0\u4e2a\u6570\u91cf\u7ea7\u3002\u6240\u4ee5 InnoDB \u7684\u9009\u62e9\u662f&#xff1a;\u5148\u5728\u5185\u5b58&#xff08;Buffer Pool&#xff09;\u6539\u9875\u3001\u540c\u65f6\u628a\u300c\u8fd9\u4e00\u9875\u6539\u4e86\u4ec0\u4e48\u300d\u987a\u5e8f\u5199\u8fdb redo log&#xff0c;\u810f\u9875\u7531\u540e\u53f0\u7ebf\u7a0b\u6162\u6162\u5237\u3002\u8fd9\u5957\u987a\u5e8f\u5199\u6362\u968f\u673a\u5199\u7684\u601d\u8def\u5c31\u662f WAL&#xff08;Write-Ahead Logging&#xff09;\u3002<\/p>\n<ul>\n<li>\u5faa\u73af\u5199&#xff1a;redo log \u662f\u56fa\u5b9a\u5927\u5c0f\u7684\u73af&#xff08;\u5982 4 \u4e2a\u6587\u4ef6\u5404 1GB&#xff09;&#xff0c;write pos \u8ffd\u7740 checkpoint \u8dd1&#xff1b;\u5199\u6ee1\u5c31\u5fc5\u987b\u505c\u4e0b\u6765\u63a8\u8fdb checkpoint&#xff08;\u5f3a\u5236\u5237\u810f\u9875&#xff09;&#xff0c;\u6b64\u65f6\u4e1a\u52a1\u66f4\u65b0\u4f1a\u88ab\u963b\u585e\u2014\u2014\u7ebf\u4e0a\u5076\u53d1\u7684\u300cMySQL \u5361\u4e00\u4e0b\u300d&#xff0c;\u67e5\u67e5 redo log \u5927\u5c0f&#xff1b;<\/li>\n<li>\u843d\u76d8\u65f6\u673a\u7531 innodb_flush_log_at_trx_commit \u63a7\u5236&#xff1a;<\/li>\n<\/ul>\n<table>\n<tr>\u503c\u884c\u4e3a\u98ce\u9669<\/tr>\n<tbody>\n<tr>\n<td>0<\/td>\n<td>\u6bcf\u79d2\u5237\u76d8\u4e00\u6b21<\/td>\n<td>\u5d29\u6e83\u4e22\u6700\u540e\u4e00\u79d2\u7684\u4e8b\u52a1<\/td>\n<\/tr>\n<tr>\n<td>1&#xff08;\u9ed8\u8ba4&#xff09;<\/td>\n<td>\u6bcf\u6b21\u63d0\u4ea4\u90fd fsync \u843d\u76d8<\/td>\n<td>\u6700\u5b89\u5168&#xff0c;IO \u538b\u529b\u6700\u5927<\/td>\n<\/tr>\n<tr>\n<td>2<\/td>\n<td>\u63d0\u4ea4\u5199\u5230 OS \u7f13\u5b58&#xff0c;\u6bcf\u79d2 fsync<\/td>\n<td>MySQL \u6302\u4e86\u4e0d\u4e22&#xff0c;\u4e3b\u673a\u65ad\u7535\u4e22\u4e00\u79d2<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>8.3 undo log&#xff1a;\u56de\u6eda\u7684\u5e95\u6c14<\/h4>\n<p>\u6bcf\u6b21\u4fee\u6539\u524d\u5148\u8bb0\u300c\u53cd\u5411\u64cd\u4f5c\u300d&#xff1a;INSERT \u8bb0\u4e00\u6761 DELETE&#xff0c;UPDATE \u8bb0\u65e7\u503c&#xff0c;DELETE \u8bb0\u6574\u884c\u3002\u4e24\u4e2a\u7528\u9014&#xff1a;\u4e00\u662f ROLLBACK \u65f6\u9010\u6761\u6267\u884c\u53cd\u5411\u64cd\u4f5c&#xff1b;\u4e8c\u662f\u4f5c\u4e3a MVCC \u7684\u7248\u672c\u94fe&#xff08;\u7b2c 6 \u7ae0\u56fe&#xff09;\u3002\u957f\u4e8b\u52a1\u7684\u9690\u85cf\u6210\u672c\u5c31\u5728\u8fd9&#xff1a;\u7248\u672c\u94fe\u4e0d\u80fd\u6e05\u7406&#xff0c;undo \u8d8a\u79ef\u8d8a\u957f&#xff0c;\u56de\u6eda\u6bb5\u81a8\u80c0\u2014\u2014\u53c8\u4e00\u4e2a\u300c\u522b\u5f00\u957f\u4e8b\u52a1\u300d\u7684\u7406\u7531\u3002<\/p>\n<h4>8.4 binlog&#xff1a;\u590d\u5236\u4e0e\u5f52\u6863\u7684\u751f\u547d\u7ebf<\/h4>\n<table>\n<tr>\u683c\u5f0f\u8bb0\u5f55\u5185\u5bb9\u4f18\u70b9\u98ce\u9669<\/tr>\n<tbody>\n<tr>\n<td>statement<\/td>\n<td>SQL \u539f\u6587<\/td>\n<td>\u65e5\u5fd7\u91cf\u5c0f<\/td>\n<td>now()\u3001uuid()\u3001limit \u987a\u5e8f\u90fd\u53ef\u80fd\u5bfc\u81f4\u4e3b\u4ece\u4e0d\u4e00\u81f4<\/td>\n<\/tr>\n<tr>\n<td>row&#xff08;\u9ed8\u8ba4&#xff09;<\/td>\n<td>\u6bcf\u884c\u53d8\u66f4\u7684\u524d\u540e\u955c\u50cf<\/td>\n<td>\u7cbe\u786e&#xff0c;\u4e3b\u4ece\u5f3a\u4e00\u81f4<\/td>\n<td>\u6279\u91cf\u66f4\u65b0 100 \u4e07\u884c\u5c31\u662f 100 \u4e07\u6761\u8bb0\u5f55&#xff0c;\u65e5\u5fd7\u66b4\u6da8<\/td>\n<\/tr>\n<tr>\n<td>mixed<\/td>\n<td>\u4e00\u822c statement&#xff0c;\u6709\u98ce\u9669\u81ea\u52a8\u5207 row<\/td>\n<td>\u6298\u4e2d<\/td>\n<td>\u5207\u6362\u903b\u8f91\u590d\u6742&#xff0c;\u6392\u67e5\u95ee\u9898\u7ed5<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><span class=\"token keyword\">SHOW<\/span> BINLOG EVENTS <span class=\"token operator\">IN<\/span> <span class=\"token string\">&#039;binlog.000123&#039;<\/span> <span class=\"token keyword\">LIMIT<\/span> <span class=\"token number\">10<\/span><span class=\"token punctuation\">;<\/span>  <span class=\"token comment\">&#8212; \u4eba\u8089\u770b\u4e00\u773c binlog \u5185\u5bb9<\/span><br \/>\n<span class=\"token keyword\">SHOW<\/span> MASTER <span class=\"token keyword\">STATUS<\/span><span class=\"token punctuation\">;<\/span>                              <span class=\"token comment\">&#8212; \u5f53\u524d\u5199\u5165\u5230\u54ea\u4e2a binlog \u6587\u4ef6\u3001\u4f4d\u7f6e<\/span><\/p>\n<h4>8.5 \u4e24\u9636\u6bb5\u63d0\u4ea4&#xff1a;\u4e3a\u4ec0\u4e48\u66f4\u65b0\u8981\u5199\u4e24\u6b21\u65e5\u5fd7<\/h4>\n<p>redo log&#xff08;InnoDB&#xff09;\u548c binlog&#xff08;Server&#xff09;\u662f\u4e24\u4e2a\u72ec\u7acb\u7684\u65e5\u5fd7\u7cfb\u7edf\u3002\u5047\u8bbe\u4e0d\u7528\u4e24\u9636\u6bb5\u3001\u7b80\u5355\u5730\u300c\u5148 redo \u540e binlog\u300d&#xff0c;\u4e2d\u95f4\u5d29\u6e83&#xff1a;\u6062\u590d\u540e\u4e3b\u5e93\u6709\u8fd9\u6761\u6570\u636e&#xff08;redo \u5df2\u843d\u76d8&#xff09;&#xff0c;binlog \u91cc\u5374\u6ca1\u6709 \u2192 \u4ece\u5e93\u6c38\u8fdc\u5c11\u8fd9\u6761 \u2192 \u4e3b\u4ece\u4e0d\u4e00\u81f4\u3002\u53cd\u8fc7\u6765\u5148 binlog \u540e redo&#xff0c;\u5219\u4ece\u5e93\u591a\u4e00\u6761\u3002\u4e24\u9636\u6bb5\u63d0\u4ea4\u5c31\u662f\u8ba9\u4e24\u672c\u65e5\u5fd7\u300c\u8981\u4e48\u90fd\u7b97\u6570&#xff0c;\u8981\u4e48\u90fd\u4e0d\u7b97\u300d&#xff1a;<\/p>\n<p>     InnoDB<\/p>\n<p>     \u6267\u884c\u5668<\/p>\n<p>      InnoDB<\/p>\n<p>      \u6267\u884c\u5668<\/p>\n<p>  #mermaid-svg-1aTCO8TcWnj9kMl7{font-family:\\&#8221;trebuchet ms\\&#8221;,verdana,arial,sans-serif;font-size:16px;fill:#333;}@keyframes edge-animation-frame{from{stroke-dashoffset:0;}}@keyframes dash{to{stroke-dashoffset:0;}}#mermaid-svg-1aTCO8TcWnj9kMl7 .edge-animation-slow{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 50s linear infinite;stroke-linecap:round;}#mermaid-svg-1aTCO8TcWnj9kMl7 .edge-animation-fast{stroke-dasharray:9,5!important;stroke-dashoffset:900;animation:dash 20s linear infinite;stroke-linecap:round;}#mermaid-svg-1aTCO8TcWnj9kMl7 .error-icon{fill:#552222;}#mermaid-svg-1aTCO8TcWnj9kMl7 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-1aTCO8TcWnj9kMl7 .edge-thickness-normal{stroke-width:1px;}#mermaid-svg-1aTCO8TcWnj9kMl7 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-1aTCO8TcWnj9kMl7 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-1aTCO8TcWnj9kMl7 .edge-thickness-invisible{stroke-width:0;fill:none;}#mermaid-svg-1aTCO8TcWnj9kMl7 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-1aTCO8TcWnj9kMl7 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-1aTCO8TcWnj9kMl7 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-1aTCO8TcWnj9kMl7 .marker.cross{stroke:#333333;}#mermaid-svg-1aTCO8TcWnj9kMl7 svg{font-family:\\&#8221;trebuchet ms\\&#8221;,verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-1aTCO8TcWnj9kMl7 p{margin:0;}#mermaid-svg-1aTCO8TcWnj9kMl7 .actor{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-1aTCO8TcWnj9kMl7 text.actor&gt;tspan{fill:black;stroke:none;}#mermaid-svg-1aTCO8TcWnj9kMl7 .actor-line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-1aTCO8TcWnj9kMl7 .innerArc{stroke-width:1.5;stroke-dasharray:none;}#mermaid-svg-1aTCO8TcWnj9kMl7 .messageLine0{stroke-width:1.5;stroke-dasharray:none;stroke:#333;}#mermaid-svg-1aTCO8TcWnj9kMl7 .messageLine1{stroke-width:1.5;stroke-dasharray:2,2;stroke:#333;}#mermaid-svg-1aTCO8TcWnj9kMl7 #arrowhead path{fill:#333;stroke:#333;}#mermaid-svg-1aTCO8TcWnj9kMl7 .sequenceNumber{fill:white;}#mermaid-svg-1aTCO8TcWnj9kMl7 #sequencenumber{fill:#333;}#mermaid-svg-1aTCO8TcWnj9kMl7 #crosshead path{fill:#333;stroke:#333;}#mermaid-svg-1aTCO8TcWnj9kMl7 .messageText{fill:#333;stroke:none;}#mermaid-svg-1aTCO8TcWnj9kMl7 .labelBox{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-1aTCO8TcWnj9kMl7 .labelText,#mermaid-svg-1aTCO8TcWnj9kMl7 .labelText&gt;tspan{fill:black;stroke:none;}#mermaid-svg-1aTCO8TcWnj9kMl7 .loopText,#mermaid-svg-1aTCO8TcWnj9kMl7 .loopText&gt;tspan{fill:black;stroke:none;}#mermaid-svg-1aTCO8TcWnj9kMl7 .loopLine{stroke-width:2px;stroke-dasharray:2,2;stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);}#mermaid-svg-1aTCO8TcWnj9kMl7 .note{stroke:#aaaa33;fill:#fff5ad;}#mermaid-svg-1aTCO8TcWnj9kMl7 .noteText,#mermaid-svg-1aTCO8TcWnj9kMl7 .noteText&gt;tspan{fill:black;stroke:none;}#mermaid-svg-1aTCO8TcWnj9kMl7 .activation0{fill:#f4f4f4;stroke:#666;}#mermaid-svg-1aTCO8TcWnj9kMl7 .activation1{fill:#f4f4f4;stroke:#666;}#mermaid-svg-1aTCO8TcWnj9kMl7 .activation2{fill:#f4f4f4;stroke:#666;}#mermaid-svg-1aTCO8TcWnj9kMl7 .actorPopupMenu{position:absolute;}#mermaid-svg-1aTCO8TcWnj9kMl7 .actorPopupMenuPanel{position:absolute;fill:#ECECFF;box-shadow:0px 8px 16px 0px rgba(0,0,0,0.2);filter:drop-shadow(3px 5px 2px rgb(0 0 0 \/ 0.4));}#mermaid-svg-1aTCO8TcWnj9kMl7 .actor-man line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;}#mermaid-svg-1aTCO8TcWnj9kMl7 .actor-man circle,#mermaid-svg-1aTCO8TcWnj9kMl7 line{stroke:hsl(259.6261682243, 59.7765363128%, 87.9019607843%);fill:#ECECFF;stroke-width:2px;}#mermaid-svg-1aTCO8TcWnj9kMl7 :root{&#8211;mermaid-font-family:\\&#8221;trebuchet ms\\&#8221;,verdana,arial,sans-serif;}<\/p>\n<p>   UPDATE orders SET status &#061; 1 WHERE id &#061; 10<\/p>\n<p>   \u5199 undo log&#xff08;\u8bb0\u5f55\u65e7\u503c&#xff09;<\/p>\n<p>   Buffer Pool \u4e2d\u4fee\u6539\u6570\u636e\u9875&#xff08;\u810f\u9875&#xff09;<\/p>\n<p>   \u8fd4\u56de\u5f71\u54cd\u884c\u6570<\/p>\n<p>   COMMIT<\/p>\n<p>   \u2460 redo log \u5199\u5165&#xff0c;\u72b6\u6001 &#061; prepare<\/p>\n<p>   \u53ef\u4ee5\u63d0\u4ea4<\/p>\n<p>   \u2461 binlog \u5199\u5165\u5e76\u843d\u76d8<\/p>\n<p>   \u2462 \u4e8b\u52a1\u63d0\u4ea4<\/p>\n<p>   redo log \u72b6\u6001 &#061; commit&#xff0c;\u5b8c\u6210<\/p>\n<p>\u8bb0\u5fc6\u951a\u70b9&#xff1a;\u5d29\u6e83\u6062\u590d\u7684\u88c1\u51b3\u89c4\u5219\u3002 \u91cd\u542f\u540e\u626b\u63cf redo log&#xff1a;\u72b6\u6001\u662f commit \u2192 \u76f4\u63a5\u63d0\u4ea4&#xff1b;\u72b6\u6001\u662f prepare \u2192 \u53bb\u67e5 binlog&#xff1a;\u8fd9\u6761\u4e8b\u52a1\u7684 binlog \u5b8c\u6574&#xff08;\u6709\u5bf9\u5e94 XID&#xff09;\u2192 \u63d0\u4ea4&#xff1b;\u4e0d\u5b8c\u6574 \u2192 \u56de\u6eda\u3002\u4e00\u53e5\u8bdd&#xff1a;\u300credo \u8bf4\u6539\u4e86&#xff0c;\u8fd8\u5f97 binlog \u62cd\u677f\u300d\u3002<\/p>\n<p>\u4e0e\u4e24\u9636\u6bb5\u63d0\u4ea4\u914d\u5957\u7684\u8fd8\u6709 binlog \u7684\u843d\u76d8\u53c2\u6570 sync_binlog&#xff1a;0&#xff08;\u4ea4\u7ed9 OS&#xff0c;\u53ef\u80fd\u4e22&#xff09;\u30011&#xff08;\u6bcf\u6b21\u63d0\u4ea4 fsync&#xff0c;\u6700\u5b89\u5168&#xff09;\u3001N&#xff08;\u6512 N \u4e2a\u4e8b\u52a1\u518d fsync&#xff09;\u3002\u751f\u4ea7\u6807\u51c6\u914d\u7f6e\u662f\u300c\u53cc 1\u300d&#xff1a;innodb_flush_log_at_trx_commit &#061; 1 &#043; sync_binlog &#061; 1\u2014\u2014\u94b1\u76f8\u5173\u7684\u7cfb\u7edf\u522b\u7701\u8fd9\u4e2a IO\u3002<\/p>\n<hr \/>\n<h3>\u4e5d\u3001\u6027\u80fd\u4f18\u5316\u5b9e\u6218<\/h3>\n<p>\u524d\u9762\u516b\u7ae0\u7684\u77e5\u8bc6&#xff0c;\u6700\u7ec8\u90fd\u6c47\u5230\u8fd9\u4e00\u7ae0\u4f7f\u7528\u3002\u4f18\u5316\u7684\u6b63\u786e\u987a\u5e8f\u6c38\u8fdc\u662f&#xff1a;\u5148\u627e\u5230\u6162\u7684 SQL&#xff08;\u6162\u67e5\u8be2\u65e5\u5fd7&#xff09;&#xff0c;\u518d\u770b\u5b83\u4e3a\u4ec0\u4e48\u6162&#xff08;EXPLAIN&#xff09;&#xff0c;\u6700\u540e\u5bf9\u75c7\u4e0b\u836f&#xff08;\u7d22\u5f15\/SQL \u6539\u5199\/\u53c2\u6570\/\u67b6\u6784&#xff09;\u3002\u53cd\u8fc7\u6765\u300c\u51ed\u611f\u89c9\u52a0\u7d22\u5f15\u300d\u662f\u65b0\u4eba\u6700\u5e38\u89c1\u7684\u5f2f\u8def\u3002<\/p>\n<h4>9.1 \u7b2c\u4e00\u6b65&#xff1a;\u8ba9\u6162 SQL \u81ea\u5df1\u73b0\u8eab<\/h4>\n<p><span class=\"token comment\">&#8212; \u5f00\u542f\u6162\u67e5\u8be2\u65e5\u5fd7&#xff1a;\u8bb0\u5f55\u6240\u6709\u8d85\u8fc7 1 \u79d2\u7684 SQL&#xff08;\u4ee5\u53ca\u6ca1\u8d70\u7d22\u5f15\u7684\u5168\u8868\u626b\u63cf&#xff09;<\/span><br \/>\n<span class=\"token keyword\">SET<\/span> <span class=\"token keyword\">GLOBAL<\/span> slow_query_log <span class=\"token operator\">&#061;<\/span> <span class=\"token keyword\">ON<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token keyword\">SET<\/span> <span class=\"token keyword\">GLOBAL<\/span> long_query_time <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">1<\/span><span class=\"token punctuation\">;<\/span>  <span class=\"token comment\">&#8212; \u7ebf\u4e0a\u4e00\u822c\u4ece 1s \u8d77\u6b65&#xff0c;\u9010\u6b65\u6536\u7d27\u5230 0.5 \/ 0.2<\/span><\/p>\n<p><span class=\"token comment\">&#8212; \u627e\u5230\u65e5\u5fd7\u4f4d\u7f6e<\/span><br \/>\n<span class=\"token keyword\">SHOW<\/span> VARIABLES <span class=\"token operator\">LIKE<\/span> <span class=\"token string\">&#039;slow_query_log_file&#039;<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">&#8212; 8.0 \u76f4\u63a5\u67e5 performance_schema&#xff0c;\u805a\u5408\u51fa Top \u6162 SQL<\/span><br \/>\n<span class=\"token keyword\">SELECT<\/span> DIGEST_TEXT                   <span class=\"token keyword\">AS<\/span> sql_pattern<span class=\"token punctuation\">,<\/span><br \/>\n       COUNT_STAR                    <span class=\"token keyword\">AS<\/span> exec_count<span class=\"token punctuation\">,<\/span><br \/>\n       <span class=\"token function\">ROUND<\/span><span class=\"token punctuation\">(<\/span>AVG_TIMER_WAIT<span class=\"token operator\">\/<\/span><span class=\"token number\">1<\/span>e12<span class=\"token punctuation\">,<\/span> <span class=\"token number\">3<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">AS<\/span> avg_sec<span class=\"token punctuation\">,<\/span><br \/>\n       <span class=\"token function\">ROUND<\/span><span class=\"token punctuation\">(<\/span>SUM_TIMER_WAIT<span class=\"token operator\">\/<\/span><span class=\"token number\">1<\/span>e12<span class=\"token punctuation\">,<\/span> <span class=\"token number\">3<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">AS<\/span> total_sec<br \/>\n<span class=\"token keyword\">FROM<\/span> performance_schema<span class=\"token punctuation\">.<\/span>events_statements_summary_by_digest<br \/>\n<span class=\"token keyword\">ORDER<\/span> <span class=\"token keyword\">BY<\/span> SUM_TIMER_WAIT <span class=\"token keyword\">DESC<\/span><br \/>\n<span class=\"token keyword\">LIMIT<\/span> <span class=\"token number\">5<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token comment\">&#8212; \u6309 total_sec \u6392\u5e8f&#xff1a;\u603b\u8017\u65f6\u6700\u591a\u7684\u5148\u6cbb\u7406&#xff1b;\u6309 avg_sec \u6392\u5e8f&#xff1a;\u5355\u6b21\u6700\u75bc\u7684\u5148\u6cbb\u7406<\/span><\/p>\n<h4>9.2 \u770b\u6574\u4f53\u5065\u5eb7\u5ea6<\/h4>\n<p><span class=\"token keyword\">SHOW<\/span> <span class=\"token keyword\">GLOBAL<\/span> <span class=\"token keyword\">STATUS<\/span> <span class=\"token operator\">LIKE<\/span> <span class=\"token string\">&#039;Threads_running&#039;<\/span><span class=\"token punctuation\">;<\/span>     <span class=\"token comment\">&#8212; \u5f53\u524d\u6d3b\u8dc3\u7ebf\u7a0b&#xff0c;\u98d9\u9ad8 &#061; \u5e76\u53d1\u538b\u529b\u6216\u9501\u7b49\u5f85<\/span><br \/>\n<span class=\"token keyword\">SHOW<\/span> <span class=\"token keyword\">GLOBAL<\/span> <span class=\"token keyword\">STATUS<\/span> <span class=\"token operator\">LIKE<\/span> <span class=\"token string\">&#039;Innodb_row_lock%&#039;<\/span><span class=\"token punctuation\">;<\/span>    <span class=\"token comment\">&#8212; \u884c\u9501\u7edf\u8ba1<\/span><br \/>\n<span class=\"token comment\">&#8212; Innodb_row_lock_waits \/ Innodb_row_lock_time_avg \u9ad8 \u2192 \u7b2c 7 \u7ae0\u7684\u9501\u95ee\u9898<\/span><\/p>\n<p><span class=\"token comment\">&#8212; Buffer Pool \u547d\u4e2d\u7387&#xff1a;\u4f4e\u4e8e 99% \u8bf4\u660e\u5185\u5b58\u4e0d\u591f\u6216\u5b58\u5728\u5168\u8868\u626b\u63cf<\/span><br \/>\n<span class=\"token keyword\">SHOW<\/span> <span class=\"token keyword\">GLOBAL<\/span> <span class=\"token keyword\">STATUS<\/span> <span class=\"token operator\">LIKE<\/span> <span class=\"token string\">&#039;Innodb_buffer_pool_read%&#039;<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token comment\">&#8212; \u547d\u4e2d\u7387 &#061; 1 &#8211; Innodb_buffer_pool_reads \/ Innodb_buffer_pool_read_requests<\/span><\/p>\n<h4>9.3 \u5b9e\u6218\u6848\u4f8b\u4e00&#xff1a;\u6df1\u5206\u9875\u62d6\u57ae\u540e\u53f0\u5bfc\u51fa<\/h4>\n<p>\u75c7\u72b6&#xff1a;\u8fd0\u8425\u540e\u53f0\u5bfc\u51fa\u8ba2\u5355&#xff0c;\u7ffb\u5230\u51e0\u767e\u9875\u540e\u6bcf\u9875\u8981 5s&#043;&#xff0c;\u671f\u95f4\u6574\u5e93 RT \u4e0a\u5347\u3002<\/p>\n<p><span class=\"token comment\">&#8212; \u539f\u59cb SQL&#xff1a;LIMIT 500000, 20 \u2014\u2014 \u7d22\u5f15\u80fd\u5b9a\u4f4d\u8d77\u70b9&#xff0c;\u4f46\u4f18\u5316\u5668\u4ecd\u8981\u6570\u8fc750\u4e07\u884c\u5e76\u56de\u8868<\/span><br \/>\n<span class=\"token keyword\">SELECT<\/span> <span class=\"token operator\">*<\/span> <span class=\"token keyword\">FROM<\/span> orders <span class=\"token keyword\">ORDER<\/span> <span class=\"token keyword\">BY<\/span> id <span class=\"token keyword\">LIMIT<\/span> <span class=\"token number\">500000<\/span><span class=\"token punctuation\">,<\/span> <span class=\"token number\">20<\/span><span class=\"token punctuation\">;<\/span><\/p>\n<p><span class=\"token comment\">&#8212; \u5904\u65b9&#xff08;\u6e38\u6807\u7ffb\u9875&#xff0c;4.6 \u8282\u8bb2\u8fc7&#xff09;&#xff1a;WHERE id &gt; \u4e0a\u9875\u6700\u540eid&#xff0c;\u4e00\u6b65\u8df3\u5230\u4f4d<\/span><br \/>\n<span class=\"token keyword\">SELECT<\/span> <span class=\"token operator\">*<\/span> <span class=\"token keyword\">FROM<\/span> orders <span class=\"token keyword\">WHERE<\/span> id <span class=\"token operator\">&gt;<\/span> <span class=\"token number\">500000<\/span> <span class=\"token keyword\">ORDER<\/span> <span class=\"token keyword\">BY<\/span> id <span class=\"token keyword\">LIMIT<\/span> <span class=\"token number\">20<\/span><span class=\"token punctuation\">;<\/span>  <span class=\"token comment\">&#8212; 5s \u2192 8ms<\/span><br \/>\n<span class=\"token comment\">&#8212; \u5bfc\u51fa\u573a\u666f\u6ca1\u6709&#034;\u4e0a\u4e00\u9875&#034;\u6309\u94ae&#xff0c;\u5929\u7136\u9002\u5408\u6e38\u6807&#xff1b;\u9875\u9762\u8df3\u8f6c\u573a\u666f\u7528\u5ef6\u8fdf\u5173\u8054\u515c\u5e95<\/span><\/p>\n<h4>9.4 \u5b9e\u6218\u6848\u4f8b\u4e8c&#xff1a;\u4e00\u6761\u7edf\u8ba1 SQL \u62d6\u57ae\u6574\u4e2a\u670d\u52a1<\/h4>\n<p>\u75c7\u72b6&#xff1a;\u665a\u9ad8\u5cf0\u8ba2\u5355\u521b\u5efa\u5168\u90e8\u8d85\u65f6&#xff0c;\u4f46\u8ba2\u5355\u8868 QPS \u5e76\u4e0d\u9ad8\u3002<\/p>\n<p><span class=\"token comment\">&#8212; \u7f6a\u9b41&#xff1a;\u8fd0\u8425\u5199\u4e86\u6761\u7edf\u8ba1 SQL&#xff0c;WHERE \u91cc\u7684\u5217\u5957\u4e86\u51fd\u6570<\/span><br \/>\n<span class=\"token keyword\">SELECT<\/span> <span class=\"token function\">COUNT<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">*<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">FROM<\/span> orders <span class=\"token keyword\">WHERE<\/span> <span class=\"token keyword\">DATE<\/span><span class=\"token punctuation\">(<\/span>created_at<span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&#061;<\/span> <span class=\"token string\">&#039;2026-09-24&#039;<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token comment\">&#8212; EXPLAIN: type&#061;ALL, rows&#061;\u5168\u8868 \u2192 \u5168\u8868\u626b\u63cf<\/span><\/p>\n<p><span class=\"token comment\">&#8212; \u66f4\u7cdf\u7684\u662f\u540c\u4e00\u6761\u8fde\u63a5\u968f\u540e\u6267\u884c\u4e86&#xff1a;<\/span><br \/>\n<span class=\"token keyword\">UPDATE<\/span> orders <span class=\"token keyword\">SET<\/span> <span class=\"token keyword\">status<\/span> <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">4<\/span> <span class=\"token keyword\">WHERE<\/span> <span class=\"token keyword\">DATE<\/span><span class=\"token punctuation\">(<\/span>created_at<span class=\"token punctuation\">)<\/span> <span class=\"token operator\">&lt;<\/span> <span class=\"token string\">&#039;2026-09-01&#039;<\/span><span class=\"token punctuation\">;<\/span><br \/>\n<span class=\"token comment\">&#8212; \u6ca1\u8d70\u7d22\u5f15\u7684 UPDATE &#061; \u5168\u8868\u9010\u884c\u52a0\u9501&#xff08;7.4 \u8282&#xff09;\u2192 \u6240\u6709\u8ba2\u5355\u66f4\u65b0\u6392\u961f \u2192 \u4e1a\u52a1\u96ea\u5d29<\/span><\/p>\n<p><span class=\"token comment\">&#8212; \u5904\u65b9&#xff1a;\u6539\u6210\u8303\u56f4\u6761\u4ef6&#xff0c;\u7d22\u5f15\u590d\u6d3b<\/span><br \/>\n<span class=\"token keyword\">SELECT<\/span> <span class=\"token function\">COUNT<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">*<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">FROM<\/span> orders<br \/>\n<span class=\"token keyword\">WHERE<\/span> created_at <span class=\"token operator\">&gt;&#061;<\/span> <span class=\"token string\">&#039;2026-09-24&#039;<\/span> <span class=\"token operator\">AND<\/span> created_at <span class=\"token operator\">&lt;<\/span> <span class=\"token string\">&#039;2026-09-25&#039;<\/span><span class=\"token punctuation\">;<\/span>  <span class=\"token comment\">&#8212; \u8d70 idx_status_created<\/span><br \/>\n<span class=\"token keyword\">UPDATE<\/span> orders <span class=\"token keyword\">SET<\/span> <span class=\"token keyword\">status<\/span> <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">4<\/span><br \/>\n<span class=\"token keyword\">WHERE<\/span> created_at <span class=\"token operator\">&lt;<\/span> <span class=\"token string\">&#039;2026-09-01&#039;<\/span><span class=\"token punctuation\">;<\/span>                                  <span class=\"token comment\">&#8212; \u8d70\u7d22\u5f15&#xff0c;\u53ea\u9501\u65e7\u884c<\/span><br \/>\n<span class=\"token comment\">&#8212; \u6559\u8bad&#xff1a;\u51fd\u6570\u5305\u5217\u4e0d\u4ec5\u6162\u67e5\u8be2&#xff0c;\u6279\u91cf UPDATE \u65f6\u8fd8\u4f1a\u5347\u7ea7\u6210\u9501\u5168\u8868<\/span><\/p>\n<h4>9.5 \u5b9e\u6218\u6848\u4f8b\u4e09&#xff1a;COUNT(*) \u592a\u6162\u600e\u4e48\u6551<\/h4>\n<p>\u75c7\u72b6&#xff1a;\u5546\u54c1\u5217\u8868\u9875\u8981\u663e\u793a\u603b\u6570&#xff0c;SELECT COUNT(*) FROM products \u8981 2s&#xff08;200 \u4e07\u884c&#xff09;\u3002<\/p>\n<p><span class=\"token comment\">&#8212; \u8ba4\u6e05\u4e8b\u5b9e&#xff1a;InnoDB \u7684 COUNT(*) \u5fc5\u987b\u771f\u6570&#xff08;MVCC \u4e0b\u6bcf\u4e2a\u4e8b\u52a1\u53ef\u89c1\u884c\u6570\u4e0d\u540c&#xff09;&#xff0c;\u6ca1\u6709\u6377\u5f84<\/span><br \/>\n<span class=\"token comment\">&#8212; \u4f46\u53ef\u4ee5\u7ed9 COUNT \u52a0\u8fc7\u6ee4\u6761\u4ef6&#xff0c;\u8ba9\u5b83\u8d70\u8986\u76d6\u7d22\u5f15\u514d\u56de\u8868&#xff1a;<\/span><br \/>\n<span class=\"token keyword\">SELECT<\/span> <span class=\"token function\">COUNT<\/span><span class=\"token punctuation\">(<\/span><span class=\"token operator\">*<\/span><span class=\"token punctuation\">)<\/span> <span class=\"token keyword\">FROM<\/span> products <span class=\"token keyword\">WHERE<\/span> category_id <span class=\"token operator\">&#061;<\/span> <span class=\"token number\">3<\/span><span class=\"token punctuation\">;<\/span>  <span class=\"token comment\">&#8212; \u8d70 idx_category&#xff0c;\u53ea\u626b\u7d22\u5f15\u6811<\/span><\/p>\n<p><span class=\"token comment\">&#8212; \u9875\u9762\u4e0d\u8981\u6c42\u7cbe\u786e\u65f6\u7684\u4e09\u5c42\u65b9\u6848&#xff1a;<\/span><br \/>\n<span class=\"token comment\">&#8212; \u2460 \u663e\u793a&#034;\u7ea6 N \u4e07&#034;&#xff1a;explain \u7684 rows \u4f30\u7b97\u503c\u76f4\u63a5\u62ff\u6765\u7528<\/span><br \/>\n<span class=\"token keyword\">EXPLAIN<\/span> <span class=\"token keyword\">SELECT<\/span> <span class=\"token operator\">*<\/span> <span class=\"token keyword\">FROM<\/span> products<span class=\"token punctuation\">;<\/span>  <span class=\"token comment\">&#8212; rows \u5217 \u2248 \u884c\u6570&#xff0c;\u6beb\u79d2\u7ea7<\/span><br \/>\n<span class=\"token comment\">&#8212; \u2461 \u8ba1\u6570\u8868&#xff1a;\u8ba1\u6570\u9891\u7e41\u7684\u573a\u666f&#xff0c;\u5355\u72ec\u7ef4\u62a4 counter \u8868\u6216 Redis \u8ba1\u6570<\/span><br \/>\n<span class=\"token comment\">&#8212; \u2462 \u6c47\u603b\u8868&#xff1a;\u79bb\u7ebf\/\u5b9a\u65f6\u4efb\u52a1\u6bcf 5 \u5206\u949f\u805a\u5408\u4e00\u6b21\u5199\u8fdb stats \u8868&#xff0c;\u524d\u7aef\u8bfb\u5b83<\/span><\/p>\n<h4>9.6 \u53c2\u6570\u8c03\u4f18\u901f\u67e5&#xff08;8.0 \u9ed8\u8ba4\u914d\u7f6e\u57fa\u7840\u4e0a&#xff09;<\/h4>\n<table>\n<tr>\u53c2\u6570\u9ed8\u8ba4\u5efa\u8bae\u4e3a\u4ec0\u4e48<\/tr>\n<tbody>\n<tr>\n<td>innodb_buffer_pool_size<\/td>\n<td>128M<\/td>\n<td>\u7269\u7406\u5185\u5b58\u7684 50%~70%<\/td>\n<td>\u6570\u636e\u9875\u7f13\u5b58&#xff0c;\u547d\u4e2d\u7387\u662f\u4e00\u5207\u6027\u80fd\u7684\u5730\u57fa<\/td>\n<\/tr>\n<tr>\n<td>innodb_flush_log_at_trx_commit<\/td>\n<td>1<\/td>\n<td>1&#xff08;\u6838\u5fc3&#xff09;\/ 2&#xff08;\u53ef\u5bb9\u5fcd\u79d2\u7ea7\u4e22\u5931&#xff09;<\/td>\n<td>\u7b2c 8 \u7ae0<\/td>\n<\/tr>\n<tr>\n<td>sync_binlog<\/td>\n<td>1<\/td>\n<td>1<\/td>\n<td>\u4e0e\u4e0a\u6761\u7ec4\u6210\u300c\u53cc 1\u300d&#xff0c;\u4e3b\u4ece\u4e00\u81f4\u6027\u57fa\u77f3<\/td>\n<\/tr>\n<tr>\n<td>max_connections<\/td>\n<td>151<\/td>\n<td>\u6309\u8fde\u63a5\u6c60 \u00d7 \u5b9e\u4f8b\u6570\u8bc4\u4f30<\/td>\n<td>\u4e0d\u662f\u8d8a\u5927\u8d8a\u597d&#xff1a;\u6bcf\u4e2a\u8fde\u63a5\u8017\u5185\u5b58&#xff0c;\u8fc7\u591a\u4e0a\u4e0b\u6587\u5207\u6362<\/td>\n<\/tr>\n<tr>\n<td>innodb_log_file_size&#xff08;redo&#xff09;<\/td>\n<td>48M<\/td>\n<td>1~4G<\/td>\n<td>\u592a\u5c0f\u9891\u7e41\u89e6\u53d1 checkpoint \u5237\u810f&#xff0c;\u4e1a\u52a1\u5361\u987f<\/td>\n<\/tr>\n<tr>\n<td>slow_query_log &#043; long_query_time<\/td>\n<td>\u5173<\/td>\n<td>\u5f00 &#043; 1s \u8d77\u6b65<\/td>\n<td>\u6ca1\u6709\u6162\u65e5\u5fd7\u7684\u7ebf\u4e0a\u5e93\u7b49\u4e8e\u76f2\u98de<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>9.7 \u4f18\u5316\u5fc3\u6cd5&#xff1a;\u4e00\u5f20\u5730\u56fe<\/h4>\n<p>\u8bb0\u5fc6\u951a\u70b9&#xff1a;\u6027\u80fd\u4f18\u5316\u7684\u4f18\u5148\u7ea7\u3002 SQL \u4e0e\u7d22\u5f15&#xff08;\u6210\u672c\u6700\u4f4e\u6536\u76ca\u6700\u5927&#xff09;\u2192 \u8868\u7ed3\u6784&#xff08;\u5197\u4f59\/\u62c6\u5206\/\u51b7\u70ed\u5206\u79bb&#xff09;\u2192 \u53c2\u6570&#xff08;Buffer Pool\u3001\u65e5\u5fd7&#xff09;\u2192 \u67b6\u6784&#xff08;\u8bfb\u5199\u5206\u79bb\u3001\u7f13\u5b58\u3001\u5206\u5e93\u5206\u8868&#xff09;\u3002 \u524d\u4e24\u5c42\u89e3\u51b3 90% \u7684\u95ee\u9898&#xff0c;\u6c38\u8fdc\u4ece\u5b83\u4eec\u5f00\u59cb&#xff1b;\u5206\u5e93\u5206\u8868\u662f\u6700\u540e\u7684\u5927\u62db&#xff0c;\u4e0d\u662f\u7b2c\u4e00\u53cd\u5e94\u3002<\/p>\n<hr \/>\n<h3>\u5341\u3001\u901f\u67e5\u624b\u518c\u4e0e\u590d\u4e60\u8def\u7ebf<\/h3>\n<p>\u6700\u540e\u4e00\u7ae0\u662f\u300c\u8003\u524d\u51b2\u523a\u300d&#xff1a;20 \u4e2a\u9ad8\u9891\u95ee\u9898\u7684\u4e00\u53e5\u8bdd\u7b54\u6848&#xff0c;\u9002\u5408\u9762\u8bd5\u524d\u6216\u6392\u969c\u65f6\u5feb\u901f\u5524\u9192\u8bb0\u5fc6\u3002\u7b54\u6848\u91cc\u62ec\u53f7\u5185\u7684\u7ae0\u8282\u53f7&#xff0c;\u5c31\u662f\u9700\u8981\u5c55\u5f00\u56de\u5fc6\u7684\u5165\u53e3\u3002<\/p>\n<h4>10.1 20 \u95ee\u901f\u7b54<\/h4>\n<table>\n<tr>\u95ee\u9898\u4e00\u53e5\u8bdd\u7b54\u6848<\/tr>\n<tbody>\n<tr>\n<td>1. MySQL 8.0 \u4e3a\u4ec0\u4e48\u5220\u4e86\u67e5\u8be2\u7f13\u5b58&#xff1f;<\/td>\n<td>\u8868\u66f4\u65b0\u5373\u5931\u6548\u3001\u547d\u4e2d\u7387\u6781\u4f4e&#xff0c;\u5f97\u4e0d\u507f\u5931&#xff08;1.2&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>2. InnoDB \u548c MyISAM \u7684\u672c\u8d28\u533a\u522b&#xff1f;<\/td>\n<td>\u4e8b\u52a1 &#043; \u884c\u7ea7\u9501 &#043; \u805a\u7c07\u7d22\u5f15 &#043; \u5d29\u6e83\u6062\u590d&#xff08;2.1&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>3. \u91d1\u989d\u5b57\u6bb5\u7528\u4ec0\u4e48\u7c7b\u578b&#xff1f;<\/td>\n<td>DECIMAL(10,2) \u6216\u5206\u5355\u4f4d BIGINT&#xff0c;\u7981\u6d6e\u70b9&#xff08;2.2&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>4. utf8 \u548c utf8mb4 \u7684\u533a\u522b&#xff1f;<\/td>\n<td>utf8 \u662f\u6700\u591a 3 \u5b57\u8282\u7684\u9609\u5272\u7248&#xff0c;\u5b58\u4e0d\u4e86 emoji&#xff08;2.6&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>5. \u4e3a\u4ec0\u4e48\u4e3b\u952e\u63a8\u8350\u81ea\u589e\u6574\u578b&#xff1f;<\/td>\n<td>\u987a\u5e8f\u63d2\u5165\u4e0d\u9875\u5206\u88c2\u3001\u6811\u77ee\u3001\u4e8c\u7ea7\u7d22\u5f15\u53f6\u5b50\u5c0f&#xff08;5.2&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>6. B&#043; \u6811\u548c B \u6811\u7684\u533a\u522b&#xff1f;<\/td>\n<td>\u975e\u53f6\u5b50\u8282\u70b9\u4e0d\u5b58\u6570\u636e&#xff08;\u66f4\u77ee&#xff09;&#043; \u53f6\u5b50\u5c42\u6709\u5e8f\u94fe\u8868&#xff08;\u8303\u56f4\u67e5\u8be2&#xff09;&#xff08;5.1&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>7. \u4ec0\u4e48\u662f\u56de\u8868&#xff1f;\u600e\u4e48\u907f\u514d&#xff1f;<\/td>\n<td>\u4e8c\u7ea7\u7d22\u5f15\u67e5\u5230\u4e3b\u952e\u518d\u67e5\u805a\u7c07\u7d22\u5f15\u53d6\u6574\u884c&#xff1b;\u8986\u76d6\u7d22\u5f15\u907f\u514d&#xff08;5.2 \/ 5.3&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>8. \u8054\u5408\u7d22\u5f15 (a,b,c)&#xff0c;WHERE b&#061;2 \u8d70\u7d22\u5f15\u5417&#xff1f;<\/td>\n<td>\u4e0d\u8d70&#xff0c;\u7f3a\u6700\u5de6\u5217 a&#xff08;5.4&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>9. LIKE \u2018%xx%\u2019 \u4e3a\u4ec0\u4e48\u4e0d\u8d70\u7d22\u5f15&#xff1f;<\/td>\n<td>\u524d\u5bfc\u4e0d\u786e\u5b9a&#xff0c;B&#043; \u6811\u65e0\u6cd5\u5b9a\u4f4d\u8d77\u70b9&#xff1b;\u2018xx%\u2019 \u76f8\u5f53\u4e8e\u8303\u56f4\u67e5\u8be2&#xff08;5.5&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>10. EXPLAIN \u91cc type&#061;ALL \u8bf4\u660e\u4ec0\u4e48&#xff1f;<\/td>\n<td>\u5168\u8868\u626b\u63cf&#xff0c;\u4f18\u5148\u6392\u67e5\u7d22\u5f15\u7f3a\u5931\u6216\u5931\u6548&#xff08;5.6&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>11. ACID \u5404\u9760\u4ec0\u4e48\u5b9e\u73b0&#xff1f;<\/td>\n<td>A \u9760 undo\u3001D \u9760 redo\u3001I \u9760 MVCC&#043;\u9501\u3001C \u662f\u524d\u4e09\u8005\u7684\u76ee\u7684&#xff08;6.1&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>12. RR \u548c RC \u7684\u672c\u8d28\u533a\u522b&#xff1f;<\/td>\n<td>ReadView \u751f\u6210\u65f6\u673a&#xff1a;RR \u7b2c\u4e00\u6b21\u67e5\u8be2\u751f\u6210\u540e\u590d\u7528&#xff0c;RC \u6bcf\u6b21\u67e5\u8be2\u91cd\u65b0\u751f\u6210&#xff08;6.3&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>13. MVCC \u662f\u600e\u4e48\u5b9e\u73b0\u7684&#xff1f;<\/td>\n<td>\u9690\u85cf\u5217&#xff08;trx_id\/roll_ptr&#xff09;&#043; undo \u7248\u672c\u94fe &#043; ReadView \u53ef\u89c1\u6027\u5224\u65ad&#xff08;6.3&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>14. RR \u5b8c\u5168\u89e3\u51b3\u5e7b\u8bfb\u4e86\u5417&#xff1f;<\/td>\n<td>\u5feb\u7167\u8bfb\u9760 MVCC\u3001\u5f53\u524d\u8bfb\u9760\u4e34\u952e\u9501&#xff1b;\u5148\u5feb\u7167\u8bfb\u540e\u5f53\u524d\u8bfb\u4ecd\u53ef\u80fd\u5e7b\u8bfb&#xff08;6.5&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>15. \u884c\u9501\u52a0\u5728\u4ec0\u4e48\u4e0a\u9762&#xff1f;<\/td>\n<td>\u7d22\u5f15\u4e0a&#xff1b;\u6761\u4ef6\u5217\u65e0\u7d22\u5f15 &#061; \u9501\u5168\u8868&#xff08;7.4&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>16. \u600e\u4e48\u9632\u6b7b\u9501&#xff1f;<\/td>\n<td>\u56fa\u5b9a\u52a0\u9501\u987a\u5e8f &#043; \u77ed\u4e8b\u52a1 &#043; \u6761\u4ef6\u66f4\u65b0 &#043; \u70ed\u70b9\u884c\u51fa\u961f&#xff08;7.5&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>17. \u8d85\u5356\u600e\u4e48\u9632&#xff1f;<\/td>\n<td>\u628a\u5e93\u5b58\u5224\u65ad\u5199\u8fdb UPDATE \u7684 WHERE&#xff08;\u6761\u4ef6\u66f4\u65b0\/\u4e50\u89c2\u9501&#xff09;&#xff08;7.6&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>18. \u4e3a\u4ec0\u4e48\u8981\u4e24\u9636\u6bb5\u63d0\u4ea4&#xff1f;<\/td>\n<td>\u4fdd\u8bc1 redo log \u4e0e binlog \u7684\u539f\u5b50\u6027&#xff0c;\u5426\u5219\u5d29\u6e83\u540e\u4e3b\u4ece\u4e0d\u4e00\u81f4&#xff08;8.5&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>19. \u300c\u53cc 1\u300d\u914d\u7f6e\u662f\u4ec0\u4e48&#xff1f;<\/td>\n<td>innodb_flush_log_at_trx_commit&#061;1 &#043; sync_binlog&#061;1&#xff0c;\u6700\u5b89\u5168\u843d\u76d8&#xff08;8.5&#xff09;<\/td>\n<\/tr>\n<tr>\n<td>20. \u6df1\u5206\u9875\u6162\u600e\u4e48\u4f18\u5316&#xff1f;<\/td>\n<td>\u6e38\u6807\u7ffb\u9875&#xff08;WHERE id &gt; \u4e0a\u9875\u672b\u5c3e&#xff09;\u6216\u5ef6\u8fdf\u5173\u8054&#xff08;4.6 \/ 9.3&#xff09;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>10.2 \u5e38\u7528\u8fd0\u7ef4\u547d\u4ee4\u8d34<\/h4>\n<p><span class=\"token comment\">&#8212; \u770b\u72b6\u6001<\/span><br \/>\n<span class=\"token keyword\">SHOW<\/span> PROCESSLIST<span class=\"token punctuation\">;<\/span>                              <span class=\"token comment\">&#8212; \u5f53\u524d\u8fde\u63a5\u4e0e\u6b63\u5728\u6267\u884c\u7684 SQL<\/span><br \/>\n<span class=\"token keyword\">SHOW<\/span> <span class=\"token keyword\">ENGINE<\/span> <span class=\"token keyword\">INNODB<\/span> <span class=\"token keyword\">STATUS<\/span>\\\\G                    <span class=\"token comment\">&#8212; \u6b7b\u9501\/\u9501\u7b49\u5f85\/\u7f13\u51b2\u6c60\u5168\u666f<\/span><br \/>\n<span class=\"token keyword\">SELECT<\/span> <span class=\"token operator\">*<\/span> <span class=\"token keyword\">FROM<\/span> information_schema<span class=\"token punctuation\">.<\/span>innodb_trx<span class=\"token punctuation\">;<\/span>   <span class=\"token comment\">&#8212; \u6d3b\u8dc3\u4e8b\u52a1&#xff08;DDL \u524d\u5fc5\u67e5&#xff09;<\/span><\/p>\n<p><span class=\"token comment\">&#8212; \u770b\u8868<\/span><br \/>\n<span class=\"token keyword\">SHOW<\/span> <span class=\"token keyword\">TABLE<\/span> <span class=\"token keyword\">STATUS<\/span> <span class=\"token keyword\">FROM<\/span> mall<span class=\"token punctuation\">;<\/span>                   <span class=\"token comment\">&#8212; \u5f15\u64ce\u3001\u884c\u6570\u3001\u5e73\u5747\u884c\u957f<\/span><br \/>\n<span class=\"token keyword\">SHOW<\/span> <span class=\"token keyword\">INDEX<\/span> <span class=\"token keyword\">FROM<\/span> orders<span class=\"token punctuation\">;<\/span>                        <span class=\"token comment\">&#8212; \u7d22\u5f15\u6e05\u5355<\/span><br \/>\n<span class=\"token keyword\">SHOW<\/span> <span class=\"token keyword\">CREATE<\/span> <span class=\"token keyword\">TABLE<\/span> orders\\\\G                     <span class=\"token comment\">&#8212; \u5b8c\u6574\u5efa\u8868\u8bed\u53e5<\/span><\/p>\n<p><span class=\"token comment\">&#8212; \u770b\u6267\u884c<\/span><br \/>\n<span class=\"token keyword\">EXPLAIN<\/span> <span class=\"token keyword\">ANALYZE<\/span> <span class=\"token keyword\">SELECT<\/span> <span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">.<\/span><span class=\"token punctuation\">;<\/span>                    <span class=\"token comment\">&#8212; 8.0 \u7684 ANALYZE \u4f1a\u771f\u7684\u6267\u884c\u5e76\u7ed9\u771f\u5b9e\u8017\u65f6<\/span><\/p>\n<p><span class=\"token comment\">&#8212; \u5907\u4efd\u4e0e\u6062\u590d<\/span><br \/>\nmysqldump <span class=\"token comment\">&#8211;single-transaction -uroot -p mall &gt; mall_backup.sql<\/span><br \/>\nmysql <span class=\"token operator\">&#8211;<\/span>uroot <span class=\"token operator\">&#8211;<\/span>p mall <span class=\"token operator\">&lt;<\/span> mall_backup<span class=\"token punctuation\">.<\/span><span class=\"token keyword\">sql<\/span><\/p>\n<h4>10.3 \u4e09\u8f6e\u590d\u4e60\u8def\u7ebf<\/h4>\n<ul>\n<li>\u7b2c\u4e00\u8f6e \u00b7 \u4e32\u4e3b\u7ebf&#xff08;\u4eca\u5929&#xff09;&#xff1a;\u53ea\u8bfb\u6bcf\u7ae0\u7684\u300c\u8bb0\u5fc6\u951a\u70b9\u300d\u548c\u56db\u5f20\u56fe&#xff0c;\u628a\u300cSQL \u751f\u547d\u5468\u671f \u2192 \u7d22\u5f15 \u2192 MVCC \u2192 \u9501 \u2192 \u65e5\u5fd7\u300d\u8fd9\u6761\u7ebf\u8fde\u8d77\u6765&#xff0c;\u76ee\u6807\u662f\u5408\u4e0a\u9875\u9762\u80fd\u590d\u8ff0\u8fd9\u6761\u7ebf&#xff1b;<\/li>\n<li>\u7b2c\u4e8c\u8f6e \u00b7 \u8dd1\u5b9e\u6218&#xff08;\u672c\u5468&#xff09;&#xff1a;\u628a mall \u4e94\u5f20\u8868\u5efa\u51fa\u6765&#xff0c;\u4eb2\u624b\u8dd1\u7b2c 3\u30014 \u7ae0\u7684\u6bcf\u4e00\u6bb5 SQL&#xff0c;\u505a\u4e00\u904d 5.7 \u8282\u7684\u7d22\u5f15\u4f18\u5316\u5b9e\u9a8c\u548c 7.5 \u8282\u7684\u6b7b\u9501\u590d\u73b0\u2014\u2014\u624b\u8fc7\u4e00\u904d\u9876\u770b\u5341\u904d&#xff1b;<\/li>\n<li>\u7b2c\u4e09\u8f6e \u00b7 \u5237\u901f\u67e5&#xff08;\u9762\u8bd5\u524d\u591c&#xff09;&#xff1a;\u53ea\u770b 10.1 \u7684 20 \u95ee&#xff0c;\u7b54\u4e0d\u4e0a\u6765\u7684\u6309\u62ec\u53f7\u91cc\u7684\u7ae0\u8282\u53f7\u56de\u53bb\u8865\u90a3\u4e00\u6bb5\u3002<\/li>\n<\/ul>\n<p>\u6700\u540e\u4e00\u5757\u62fc\u56fe&#xff1a;\u6240\u6709 MySQL \u77e5\u8bc6\u6700\u7ec8\u6536\u655b\u6210\u4e00\u53e5\u8bdd\u2014\u2014\u4e00\u6761 SQL \u8fdb\u6765&#xff0c;\u670d\u52a1\u5c42\u89e3\u6790\u4f18\u5316&#xff0c;\u5f15\u64ce\u5c42\u7528 B&#043; \u6811\u627e\u6570\u636e&#xff0c;\u9760 MVCC \u8ba9\u8bfb\u5199\u4e0d\u6253\u67b6&#xff0c;\u9760\u9501\u8ba9\u5199\u5199\u6392\u961f&#xff0c;\u9760 redo\/undo\/binlog \u4fdd\u8bc1\u4e0d\u4e22\u4e0d\u9519\u3002\u80fd\u9ed8\u5199\u8fd9\u53e5\u8bdd&#xff0c;\u4f60\u5c31\u6ca1\u5fd8\u3002<\/p>\n<hr \/>\n<p>MySQL \u77e5\u8bc6\u5168\u666f\u56de\u987e \u00b7 \u57fa\u4e8e MySQL 8.0 \/ InnoDB \u00b7 2026-09 \u6574\u7406 \u00b7 \u6b22\u8fce\u6536\u85cf\u590d\u4e60&#xff0c;\u8f6c\u8f7d\u8bf7\u6ce8\u660e\u51fa\u5904<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u7bc7\u7b14\u8bb0\u6cbf\u7740\u300c\u4e00\u6761 SQL \u8bed\u53e5\u5728 MySQL \u91cc\u7684\u5b8c\u6574\u65c5\u7a0b\u300d\u8fd9\u6761\u4e3b\u7ebf&#xff0c;\u628a\u67b6\u6784\u3001\u5b58\u50a8\u5f15\u64ce\u3001\u7d22\u5f15\u3001\u4e8b\u52a1\u3001MVCC\u3001\u9501\u3001\u65e5\u5fd7\u4e0e\u6027\u80fd\u4f18\u5316\u5168\u90e8\u4e32\u8d77\u6765\u2014\u2014\u6bcf\u7ae0\u90fd\u914d\u53ef\u8dd1\u7684\u5b9e\u6218 SQL&#xff08;\u7535\u5546\u5e93 mall&#xff09;&#xff0c;\u7ed3\u5c3e\u9644 20 \u95ee\u901f\u7b54\u3002\u5fd8\u4e86\u6ca1\u5173\u7cfb&#xff0c;\u8ddf\u7740\u4e3b\u7ebf\u8d70\u4e00\u904d&#xff0c;\u77e5\u8bc6\u70b9\u4f1a\u81ea\u5df1\u300c\u957f\u300d\u56de\u8111\u5b50\u91cc\u3002 \u9002\u7528\u7248\u672c&#xff1a;MySQL 8.0 \u00b7 InnoDB \u00b7 10 \u7ae0 \u00b7 40 \u6bb5\u5b9e\u6218 SQL \u00b7 20 \u95ee\u901f\u67e5 \u76ee\u5f55<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[99,114,768,190],"topic":[],"class_list":["post-110006","post","type-post","status-publish","format-standard","hentry","category-server","tag-java","tag-mysql","tag-sql","tag-190"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>\u4ece\u4e00\u6761 SQL \u7684\u751f\u547d\u5468\u671f\u5f00\u59cb\uff0c\u628a MySQL \u4e00\u6b21\u6027\u60f3\u660e\u767d - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/110006.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u4ece\u4e00\u6761 SQL \u7684\u751f\u547d\u5468\u671f\u5f00\u59cb\uff0c\u628a MySQL \u4e00\u6b21\u6027\u60f3\u660e\u767d - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u8fd9\u7bc7\u7b14\u8bb0\u6cbf\u7740\u300c\u4e00\u6761 SQL \u8bed\u53e5\u5728 MySQL \u91cc\u7684\u5b8c\u6574\u65c5\u7a0b\u300d\u8fd9\u6761\u4e3b\u7ebf&#xff0c;\u628a\u67b6\u6784\u3001\u5b58\u50a8\u5f15\u64ce\u3001\u7d22\u5f15\u3001\u4e8b\u52a1\u3001MVCC\u3001\u9501\u3001\u65e5\u5fd7\u4e0e\u6027\u80fd\u4f18\u5316\u5168\u90e8\u4e32\u8d77\u6765\u2014\u2014\u6bcf\u7ae0\u90fd\u914d\u53ef\u8dd1\u7684\u5b9e\u6218 SQL&#xff08;\u7535\u5546\u5e93 mall&#xff09;&#xff0c;\u7ed3\u5c3e\u9644 20 \u95ee\u901f\u7b54\u3002\u5fd8\u4e86\u6ca1\u5173\u7cfb&#xff0c;\u8ddf\u7740\u4e3b\u7ebf\u8d70\u4e00\u904d&#xff0c;\u77e5\u8bc6\u70b9\u4f1a\u81ea\u5df1\u300c\u957f\u300d\u56de\u8111\u5b50\u91cc\u3002 \u9002\u7528\u7248\u672c&#xff1a;MySQL 8.0 \u00b7 InnoDB \u00b7 10 \u7ae0 \u00b7 40 \u6bb5\u5b9e\u6218 SQL \u00b7 20 \u95ee\u901f\u67e5 \u76ee\u5f55\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/110006.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-09-25T07:40:44+00:00\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"34 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/110006.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/110006.html\",\"name\":\"\u4ece\u4e00\u6761 SQL \u7684\u751f\u547d\u5468\u671f\u5f00\u59cb\uff0c\u628a MySQL \u4e00\u6b21\u6027\u60f3\u660e\u767d - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-09-25T07:40:44+00:00\",\"dateModified\":\"2026-09-25T07:40:44+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/110006.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/110006.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/110006.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"\u4ece\u4e00\u6761 SQL \u7684\u751f\u547d\u5468\u671f\u5f00\u59cb\uff0c\u628a MySQL \u4e00\u6b21\u6027\u60f3\u660e\u767d\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u4ece\u4e00\u6761 SQL \u7684\u751f\u547d\u5468\u671f\u5f00\u59cb\uff0c\u628a MySQL \u4e00\u6b21\u6027\u60f3\u660e\u767d - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/110006.html","og_locale":"zh_CN","og_type":"article","og_title":"\u4ece\u4e00\u6761 SQL \u7684\u751f\u547d\u5468\u671f\u5f00\u59cb\uff0c\u628a MySQL \u4e00\u6b21\u6027\u60f3\u660e\u767d - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u8fd9\u7bc7\u7b14\u8bb0\u6cbf\u7740\u300c\u4e00\u6761 SQL \u8bed\u53e5\u5728 MySQL \u91cc\u7684\u5b8c\u6574\u65c5\u7a0b\u300d\u8fd9\u6761\u4e3b\u7ebf&#xff0c;\u628a\u67b6\u6784\u3001\u5b58\u50a8\u5f15\u64ce\u3001\u7d22\u5f15\u3001\u4e8b\u52a1\u3001MVCC\u3001\u9501\u3001\u65e5\u5fd7\u4e0e\u6027\u80fd\u4f18\u5316\u5168\u90e8\u4e32\u8d77\u6765\u2014\u2014\u6bcf\u7ae0\u90fd\u914d\u53ef\u8dd1\u7684\u5b9e\u6218 SQL&#xff08;\u7535\u5546\u5e93 mall&#xff09;&#xff0c;\u7ed3\u5c3e\u9644 20 \u95ee\u901f\u7b54\u3002\u5fd8\u4e86\u6ca1\u5173\u7cfb&#xff0c;\u8ddf\u7740\u4e3b\u7ebf\u8d70\u4e00\u904d&#xff0c;\u77e5\u8bc6\u70b9\u4f1a\u81ea\u5df1\u300c\u957f\u300d\u56de\u8111\u5b50\u91cc\u3002 \u9002\u7528\u7248\u672c&#xff1a;MySQL 8.0 \u00b7 InnoDB \u00b7 10 \u7ae0 \u00b7 40 \u6bb5\u5b9e\u6218 SQL \u00b7 20 \u95ee\u901f\u67e5 \u76ee\u5f55","og_url":"https:\/\/www.wsisp.com\/helps\/110006.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-09-25T07:40:44+00:00","author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"34 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/110006.html","url":"https:\/\/www.wsisp.com\/helps\/110006.html","name":"\u4ece\u4e00\u6761 SQL \u7684\u751f\u547d\u5468\u671f\u5f00\u59cb\uff0c\u628a MySQL \u4e00\u6b21\u6027\u60f3\u660e\u767d - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-09-25T07:40:44+00:00","dateModified":"2026-09-25T07:40:44+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/110006.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/110006.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/110006.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"\u4ece\u4e00\u6761 SQL \u7684\u751f\u547d\u5468\u671f\u5f00\u59cb\uff0c\u628a MySQL \u4e00\u6b21\u6027\u60f3\u660e\u767d"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/110006","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=110006"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/110006\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=110006"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=110006"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=110006"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=110006"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}