{"id":6366,"date":"2024-07-18T20:22:21","date_gmt":"2024-07-18T12:22:21","guid":{"rendered":"https:\/\/t.n-years.com\/?p=6366"},"modified":"2024-07-18T20:22:21","modified_gmt":"2024-07-18T12:22:21","slug":"%e6%95%b0%e6%8d%ae%e5%ba%93%e6%93%8d%e4%bd%9c%e6%98%93%e7%8a%af%e9%94%99%e8%af%af%e5%8f%8a%e6%ad%a3%e7%a1%ae%e5%81%9a%e6%b3%95","status":"publish","type":"post","link":"https:\/\/t.n-years.com\/?p=6366","title":{"rendered":"\u6570\u636e\u5e93\u64cd\u4f5c\u6613\u72af\u9519\u8bef\u53ca\u6b63\u786e\u505a\u6cd5"},"content":{"rendered":"<p>\u5728\u540e\u7aef\u5f00\u53d1\u4e2d\uff0c\u6570\u636e\u5e93\u64cd\u4f5c\u662f\u4e00\u4e2a\u5173\u952e\u73af\u8282\uff0c\u8bb8\u591a\u5e38\u89c1\u9519\u8bef\u4f1a\u5f71\u54cd\u7cfb\u7edf\u7684\u6027\u80fd\u3001\u53ef\u9760\u6027\u548c\u5b89\u5168\u6027\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5bb9\u6613\u72af\u7684\u9519\u8bef\u53ca\u5176\u6b63\u786e\u505a\u6cd5\uff1a<\/p>\n<h3>1. \u5ffd\u7565\u7d22\u5f15<\/h3>\n<p><strong>\u9519\u8bef<\/strong>\uff1a<\/p>\n<ul>\n<li>\u5728\u5927\u91cf\u6570\u636e\u8868\u4e0a\u7f3a\u4e4f\u5408\u9002\u7684\u7d22\u5f15\uff0c\u5bfc\u81f4\u67e5\u8be2\u901f\u5ea6\u6162\u3002<\/li>\n<\/ul>\n<p><strong>\u6b63\u786e\u505a\u6cd5<\/strong>\uff1a<\/p>\n<ul>\n<li>\u4e3a\u7ecf\u5e38\u67e5\u8be2\u7684\u5217\u521b\u5efa\u7d22\u5f15\uff0c\u7279\u522b\u662f\u7528\u4e8e <code>WHERE<\/code> \u5b50\u53e5\u3001<code>JOIN<\/code> \u64cd\u4f5c\u548c\u6392\u5e8f\u7684\u5217\u3002<\/li>\n<li>\u5b9a\u671f\u5206\u6790\u548c\u4f18\u5316\u7d22\u5f15\uff0c\u907f\u514d\u8fc7\u591a\u7684\u7d22\u5f15\u5bfc\u81f4\u63d2\u5165\u548c\u66f4\u65b0\u64cd\u4f5c\u53d8\u6162\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b<\/strong>\uff1a<\/p>\n<pre><code class=\"language-sql\">-- \u4e3a name \u5217\u521b\u5efa\u7d22\u5f15\nCREATE INDEX idx_name ON users (name);<\/code><\/pre>\n<h3>2. \u5ffd\u7565\u6570\u636e\u5e93\u8fde\u63a5\u7ba1\u7406<\/h3>\n<p><strong>\u9519\u8bef<\/strong>\uff1a<\/p>\n<ul>\n<li>\u4e0d\u7ba1\u7406\u6570\u636e\u5e93\u8fde\u63a5\u6c60\uff0c\u5bfc\u81f4\u8fde\u63a5\u6cc4\u6f0f\u6216\u8fc7\u591a\u7684\u8fde\u63a5\u6570\u3002<\/li>\n<\/ul>\n<p><strong>\u6b63\u786e\u505a\u6cd5<\/strong>\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528\u6570\u636e\u5e93\u8fde\u63a5\u6c60\uff0c\u63a7\u5236\u8fde\u63a5\u6570\u5e76\u91cd\u7528\u8fde\u63a5\u3002<\/li>\n<li>\u786e\u4fdd\u5728\u5b8c\u6210\u6570\u636e\u5e93\u64cd\u4f5c\u540e\uff0c\u53ca\u65f6\u5173\u95ed\u8fde\u63a5\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b<\/strong>\uff1a<\/p>\n<pre><code class=\"language-python\"># \u4f7f\u7528\u8fde\u63a5\u6c60\uff08Python \u793a\u4f8b\uff09\nfrom sqlalchemy import create_engine\nfrom sqlalchemy.orm import sessionmaker\n\nengine = create_engine(&#039;mysql:\/\/user:password@host\/db&#039;)\nSession = sessionmaker(bind=engine)\n\n# \u83b7\u53d6\u4f1a\u8bdd\u5e76\u4f7f\u7528\nsession = Session()\n# ...\u6267\u884c\u6570\u636e\u5e93\u64cd\u4f5c...\nsession.close()<\/code><\/pre>\n<h3>3. \u5ffd\u7565SQL\u6ce8\u5165\u653b\u51fb<\/h3>\n<p><strong>\u9519\u8bef<\/strong>\uff1a<\/p>\n<ul>\n<li>\u76f4\u63a5\u4f7f\u7528\u7528\u6237\u8f93\u5165\u6784\u5efa SQL \u67e5\u8be2\uff0c\u5bb9\u6613\u53d7\u5230 SQL \u6ce8\u5165\u653b\u51fb\u3002<\/li>\n<\/ul>\n<p><strong>\u6b63\u786e\u505a\u6cd5<\/strong>\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528\u53c2\u6570\u5316\u67e5\u8be2\u6216\u9884\u7f16\u8bd1\u8bed\u53e5\uff0c\u907f\u514d\u76f4\u63a5\u62fc\u63a5\u7528\u6237\u8f93\u5165\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b<\/strong>\uff1a<\/p>\n<pre><code class=\"language-python\"># \u53c2\u6570\u5316\u67e5\u8be2\uff08Python \u793a\u4f8b\uff09\ncursor.execute(&quot;SELECT * FROM users WHERE name = %s&quot;, (username,))<\/code><\/pre>\n<h3>4. \u4e0d\u8fdb\u884c\u4e8b\u52a1\u7ba1\u7406<\/h3>\n<p><strong>\u9519\u8bef<\/strong>\uff1a<\/p>\n<ul>\n<li>\u5728\u591a\u6b65\u6570\u636e\u5e93\u64cd\u4f5c\u4e2d\u672a\u4f7f\u7528\u4e8b\u52a1\uff0c\u53ef\u80fd\u5bfc\u81f4\u6570\u636e\u4e0d\u4e00\u81f4\u3002<\/li>\n<\/ul>\n<p><strong>\u6b63\u786e\u505a\u6cd5<\/strong>\uff1a<\/p>\n<ul>\n<li>\u4f7f\u7528\u4e8b\u52a1\u6765\u786e\u4fdd\u591a\u6b65\u64cd\u4f5c\u7684\u539f\u5b50\u6027\u3002\u5982\u679c\u4efb\u4e00\u6b65\u5931\u8d25\uff0c\u53ef\u4ee5\u56de\u6eda\u6574\u4e2a\u64cd\u4f5c\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b<\/strong>\uff1a<\/p>\n<pre><code class=\"language-sql\">-- \u4f7f\u7528\u4e8b\u52a1\nSTART TRANSACTION;\n\n-- \u591a\u6b65\u64cd\u4f5c\nUPDATE accounts SET balance = balance - 100 WHERE user_id = 1;\nUPDATE accounts SET balance = balance + 100 WHERE user_id = 2;\n\n-- \u63d0\u4ea4\u4e8b\u52a1\nCOMMIT;<\/code><\/pre>\n<h3>5. \u5927\u91cf\u6570\u636e\u64cd\u4f5c\u65f6\u672a\u5206\u6279\u5904\u7406<\/h3>\n<p><strong>\u9519\u8bef<\/strong>\uff1a<\/p>\n<ul>\n<li>\u5728\u4e00\u6b21\u64cd\u4f5c\u4e2d\u5904\u7406\u5927\u91cf\u6570\u636e\uff0c\u53ef\u80fd\u5bfc\u81f4\u5185\u5b58\u4e0d\u8db3\u6216\u8d85\u65f6\u3002<\/li>\n<\/ul>\n<p><strong>\u6b63\u786e\u505a\u6cd5<\/strong>\uff1a<\/p>\n<ul>\n<li>\u5c06\u5927\u6570\u636e\u64cd\u4f5c\u5206\u6210\u5c0f\u6279\u6b21\u5904\u7406\uff0c\u51cf\u5c11\u5355\u6b21\u64cd\u4f5c\u7684\u8d1f\u8f7d\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b<\/strong>\uff1a<\/p>\n<pre><code class=\"language-python\"># \u5206\u6279\u5904\u7406\u6570\u636e\uff08Python \u793a\u4f8b\uff09\nbatch_size = 100\ntotal_records = cursor.execute(&quot;SELECT COUNT(*) FROM large_table&quot;).fetchone()[0]\n\nfor offset in range(0, total_records, batch_size):\n    cursor.execute(&quot;SELECT * FROM large_table LIMIT %s OFFSET %s&quot;, (batch_size, offset))\n    batch = cursor.fetchall()\n    # \u5904\u7406\u6bcf\u6279\u6570\u636e<\/code><\/pre>\n<h3>6. \u5ffd\u89c6\u6570\u636e\u4e00\u81f4\u6027\u548c\u5b8c\u6574\u6027\u7ea6\u675f<\/h3>\n<p><strong>\u9519\u8bef<\/strong>\uff1a<\/p>\n<ul>\n<li>\u672a\u4f7f\u7528\u5916\u952e\u7ea6\u675f\u3001\u552f\u4e00\u6027\u7ea6\u675f\u7b49\uff0c\u5bfc\u81f4\u6570\u636e\u4e0d\u4e00\u81f4\u3002<\/li>\n<\/ul>\n<p><strong>\u6b63\u786e\u505a\u6cd5<\/strong>\uff1a<\/p>\n<ul>\n<li>\u5b9a\u4e49\u5916\u952e\u7ea6\u675f\u3001\u552f\u4e00\u6027\u7ea6\u675f\u548c\u68c0\u67e5\u7ea6\u675f\uff0c\u4ee5\u786e\u4fdd\u6570\u636e\u7684\u5b8c\u6574\u6027\u548c\u4e00\u81f4\u6027\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b<\/strong>\uff1a<\/p>\n<pre><code class=\"language-sql\">-- \u5b9a\u4e49\u5916\u952e\u7ea6\u675f\nALTER TABLE orders\nADD CONSTRAINT fk_customer\nFOREIGN KEY (customer_id) REFERENCES customers(id);\n\n-- \u5b9a\u4e49\u552f\u4e00\u6027\u7ea6\u675f\nALTER TABLE users\nADD CONSTRAINT unique_email UNIQUE (email);<\/code><\/pre>\n<h3>7. \u5ffd\u7565\u5907\u4efd\u548c\u6062\u590d\u7b56\u7565<\/h3>\n<p><strong>\u9519\u8bef<\/strong>\uff1a<\/p>\n<ul>\n<li>\u672a\u5b9a\u671f\u5907\u4efd\u6570\u636e\u5e93\uff0c\u6216\u7f3a\u4e4f\u6709\u6548\u7684\u6062\u590d\u7b56\u7565\uff0c\u5bfc\u81f4\u6570\u636e\u4e22\u5931\u98ce\u9669\u3002<\/li>\n<\/ul>\n<p><strong>\u6b63\u786e\u505a\u6cd5<\/strong>\uff1a<\/p>\n<ul>\n<li>\u5236\u5b9a\u5e76\u6267\u884c\u5b9a\u671f\u5907\u4efd\u7b56\u7565\uff0c\u786e\u4fdd\u5907\u4efd\u6570\u636e\u7684\u5b89\u5168\u6027\u548c\u53ef\u7528\u6027\u3002<\/li>\n<li>\u5b9a\u671f\u6d4b\u8bd5\u5907\u4efd\u6062\u590d\u8fc7\u7a0b\uff0c\u786e\u4fdd\u80fd\u591f\u6709\u6548\u6062\u590d\u6570\u636e\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b<\/strong>\uff1a<\/p>\n<pre><code class=\"language-sh\"># \u4f7f\u7528 mysqldump \u5907\u4efd MySQL \u6570\u636e\u5e93\nmysqldump -u user -p database_name &gt; backup.sql\n\n# \u6062\u590d\u6570\u636e\u5e93\nmysql -u user -p database_name &lt; backup.sql<\/code><\/pre>\n<h3>8. \u5ffd\u7565\u6027\u80fd\u76d1\u63a7\u548c\u4f18\u5316<\/h3>\n<p><strong>\u9519\u8bef<\/strong>\uff1a<\/p>\n<ul>\n<li>\u4e0d\u76d1\u63a7\u6570\u636e\u5e93\u6027\u80fd\uff0c\u672a\u8fdb\u884c\u5fc5\u8981\u7684\u4f18\u5316\uff0c\u5bfc\u81f4\u7cfb\u7edf\u6027\u80fd\u4e0b\u964d\u3002<\/li>\n<\/ul>\n<p><strong>\u6b63\u786e\u505a\u6cd5<\/strong>\uff1a<\/p>\n<ul>\n<li>\u5b9a\u671f\u76d1\u63a7\u6570\u636e\u5e93\u6027\u80fd\uff0c\u5206\u6790\u6162\u67e5\u8be2\u65e5\u5fd7\uff0c\u4f18\u5316\u67e5\u8be2\u548c\u7d22\u5f15\u3002<\/li>\n<li>\u4f7f\u7528\u6027\u80fd\u5206\u6790\u5de5\u5177\uff0c\u5982 MySQL \u7684 <code>EXPLAIN<\/code>\uff0c\u4e86\u89e3\u67e5\u8be2\u7684\u6267\u884c\u8ba1\u5212\u5e76\u8fdb\u884c\u4f18\u5316\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b<\/strong>\uff1a<\/p>\n<pre><code class=\"language-sql\">-- \u4f7f\u7528 EXPLAIN \u5206\u6790\u67e5\u8be2\nEXPLAIN SELECT * FROM users WHERE name = &#039;John&#039;;<\/code><\/pre>\n<h3>\u603b\u7ed3<\/h3>\n<p>\u540e\u7aef\u5f00\u53d1\u4e2d\uff0c\u6b63\u786e\u7684\u6570\u636e\u5e93\u64cd\u4f5c\u662f\u786e\u4fdd\u7cfb\u7edf\u6027\u80fd\u548c\u6570\u636e\u4e00\u81f4\u6027\u7684\u5173\u952e\u3002\u901a\u8fc7\u5408\u7406\u4f7f\u7528\u7d22\u5f15\u3001\u7ba1\u7406\u6570\u636e\u5e93\u8fde\u63a5\u3001\u9884\u9632 SQL \u6ce8\u5165\u3001\u4f7f\u7528\u4e8b\u52a1\u3001\u5206\u6279\u5904\u7406\u5927\u6570\u636e\u3001\u5b9a\u4e49\u6570\u636e\u7ea6\u675f\u3001\u5b9a\u671f\u5907\u4efd\u548c\u6062\u590d\u6570\u636e\u4ee5\u53ca\u76d1\u63a7\u548c\u4f18\u5316\u6027\u80fd\uff0c\u53ef\u4ee5\u907f\u514d\u5e38\u89c1\u9519\u8bef\uff0c\u6784\u5efa\u5065\u58ee\u548c\u9ad8\u6548\u7684\u6570\u636e\u5e93\u7cfb\u7edf\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u540e\u7aef\u5f00\u53d1\u4e2d\uff0c\u6570\u636e\u5e93\u64cd\u4f5c\u662f\u4e00\u4e2a\u5173\u952e\u73af\u8282\uff0c\u8bb8\u591a\u5e38\u89c1\u9519\u8bef\u4f1a\u5f71\u54cd\u7cfb\u7edf\u7684\u6027\u80fd\u3001\u53ef\u9760\u6027\u548c\u5b89\u5168\u6027\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5bb9\u6613\u72af\u7684\u9519\u8bef\u53ca&#8230; <\/p>\n<div class=\"read-more navbutton\"><a href=\"https:\/\/t.n-years.com\/?p=6366\">\u9605\u8bfb\u66f4\u591a<i class=\"fa fa-angle-double-right\"><\/i><\/a><\/div>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[123],"tags":[108],"class_list":["post-6366","post","type-post","status-publish","format-standard","hentry","category-123","tag-108"],"_links":{"self":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6366","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6366"}],"version-history":[{"count":1,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6366\/revisions"}],"predecessor-version":[{"id":6367,"href":"https:\/\/t.n-years.com\/index.php?rest_route=\/wp\/v2\/posts\/6366\/revisions\/6367"}],"wp:attachment":[{"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6366"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6366"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/t.n-years.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6366"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}