{"id":10020675,"date":"2025-03-19T22:52:42","date_gmt":"2025-03-19T14:52:42","guid":{"rendered":"https:\/\/itmax.cn\/?p=10020675"},"modified":"2025-03-19T22:52:42","modified_gmt":"2025-03-19T14:52:42","slug":"%e3%80%8cmysql-%e6%95%b0%e6%8d%ae%e5%ba%93%e4%bc%98%e5%8c%96%e3%80%8d%e9%99%8d%e4%bd%8e%e5%ad%98%e5%82%a8%e4%b8%8e%e6%9f%a5%e8%af%a2%e6%88%90%e6%9c%ac%e7%9a%84%e6%9c%80%e4%bd%b3%e5%ae%9e%e8%b7%b5","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10020675","title":{"rendered":"\u300cMySQL \u6570\u636e\u5e93\u4f18\u5316\u300d\u964d\u4f4e\u5b58\u50a8\u4e0e\u67e5\u8be2\u6210\u672c\u7684\u6700\u4f73\u5b9e\u8df5"},"content":{"rendered":"<h2>\u6458\u8981<\/h2>\n<p>\u6570\u636e\u5e93\u5b58\u50a8\u548c\u67e5\u8be2\u6210\u672c\u9ad8\uff0c\u7279\u522b\u662f\u6570\u636e\u91cf\u5927\u7684\u4f01\u4e1a\u9762\u4e34\u5de8\u5927\u7684\u6311\u6218\u3002\u672c\u7bc7\u6587\u7ae0\u5c06\u63a2\u8ba8\u00a0<strong>\u7d22\u5f15\u4f18\u5316\u3001\u51cf\u5c11\u5197\u4f59\u6570\u636e\u3001\u51b7\u70ed\u5b58\u50a8\u7ba1\u7406<\/strong>\u00a0\u4ee5\u53ca\u00a0<strong>\u7f13\u5b58\uff08Redis\u3001CDN\uff09<\/strong>\u00a0\u5728\u4f18\u5316\u6570\u636e\u5e93\u8bbf\u95ee\u4e2d\u7684\u4f5c\u7528\u3002\u901a\u8fc7\u5b9e\u9645\u793a\u4f8b\uff0c\u5e2e\u52a9\u4f01\u4e1a\u4f18\u5316\u6570\u636e\u5e93\u6027\u80fd\uff0c\u964d\u4f4e\u6210\u672c\u3002<\/p>\n<h2>\u5f15\u8a00<\/h2>\n<p>\u73b0\u4ee3\u5e94\u7528\u7a0b\u5e8f\u4f9d\u8d56\u6570\u636e\u5e93\u6765\u5b58\u50a8\u548c\u67e5\u8be2\u6d77\u91cf\u6570\u636e\u3002\u7136\u800c\uff0c\u6570\u636e\u5e93\u6210\u672c\u5305\u62ec\u00a0<strong>\u5b58\u50a8\u6210\u672c\u3001\u67e5\u8be2\u6210\u672c\u3001\u7d22\u5f15\u7ef4\u62a4\u6210\u672c<\/strong>\u7b49\uff0c\u968f\u7740\u6570\u636e\u589e\u957f\uff0c\u6210\u672c\u4e0d\u65ad\u6500\u5347\u3002\u56e0\u6b64\uff0c\u4f18\u5316\u6570\u636e\u5e93\u81f3\u5173\u91cd\u8981\u3002<\/p>\n<p>\u672c\u6587\u5c06\u4ecb\u7ecd MySQL\u3001PostgreSQL\u3001MongoDB\u3001DynamoDB \u7684\u00a0<strong>\u7d22\u5f15\u4f18\u5316\u7b56\u7565<\/strong>\uff0c\u5982\u4f55\u00a0<strong>\u51cf\u5c11\u5197\u4f59\u6570\u636e<\/strong>\uff0c\u5982\u4f55\u00a0<strong>\u5206\u5c42\u5b58\u50a8\u51b7\u70ed\u6570\u636e<\/strong>\u00a0\u4ee5\u53ca\u00a0<strong>\u5229\u7528 Redis \u548c CDN \u8fdb\u884c\u7f13\u5b58\u4f18\u5316<\/strong>\u3002<\/p>\n<h2>\u7d22\u5f15\u4f18\u5316<\/h2>\n<h3>\u9009\u62e9\u5408\u9002\u7684\u7d22\u5f15<\/h3>\n<p>\u7d22\u5f15\u80fd\u5927\u5e45\u63d0\u9ad8\u67e5\u8be2\u6027\u80fd\uff0c\u4f46\u8fc7\u591a\u7d22\u5f15\u4f1a\u5f71\u54cd\u5199\u5165\u6027\u80fd\u3002\u56e0\u6b64\uff0c\u9700\u8981\u5408\u7406\u9009\u62e9\u7d22\u5f15\u7c7b\u578b\u3002<\/p>\n<h4>MySQL \u7d22\u5f15<\/h4>\n<pre><code class=\"sql\">CREATE INDEX idx_user_email ON users(email);<\/code><\/pre>\n<h4>MongoDB \u7d22\u5f15<\/h4>\n<pre><code class=\"sql\">db.users.createIndex({ email: 1 });<\/code><\/pre>\n<h3>\u907f\u514d\u5197\u4f59\u7d22\u5f15<\/h3>\n<p>\u5197\u4f59\u7d22\u5f15\u4f1a\u5360\u7528\u5b58\u50a8\u5e76\u964d\u4f4e\u66f4\u65b0\u901f\u5ea6\uff0c\u53ef\u4f7f\u7528\u00a0<code>EXPLAIN<\/code>\u00a0\u6216\u00a0<code>ANALYZE<\/code>\u00a0\u8fdb\u884c\u5206\u6790\u3002<\/p>\n<pre><code class=\"sql\">EXPLAIN ANALYZE SELECT * FROM users WHERE email = 'test@example.com';<\/code><\/pre>\n<h2>\u6570\u636e\u5197\u4f59\u4f18\u5316<\/h2>\n<h3>\u89c4\u8303\u5316\uff08Normalization\uff09<\/h3>\n<p>\u6570\u636e\u5e93\u8868\u8bbe\u8ba1\u5e94\u5c3d\u91cf\u907f\u514d\u5197\u4f59\u5b58\u50a8\uff0c\u63d0\u9ad8\u6570\u636e\u4e00\u81f4\u6027\u3002<\/p>\n<h4>\u793a\u4f8b<\/h4>\n<pre><code class=\"sql\">CREATE TABLE orders (\n    id SERIAL PRIMARY KEY,\n    user_id INT REFERENCES users(id),\n    product_id INT REFERENCES products(id)\n);<\/code><\/pre>\n<h3>\u53bb\u91cd\u4e0e\u538b\u7f29<\/h3>\n<p>\u5229\u7528\u00a0<code>UNIQUE<\/code>\u00a0\u7ea6\u675f\u907f\u514d\u91cd\u590d\u6570\u636e\u3002<\/p>\n<pre><code class=\"sql\">ALTER TABLE users ADD CONSTRAINT unique_email UNIQUE (email);<\/code><\/pre>\n<h2>\u51b7\u70ed\u6570\u636e\u7ba1\u7406<\/h2>\n<h3>\u51b7\u5b58\u50a8 vs. \u70ed\u5b58\u50a8<\/h3>\n<ul>\n<li><strong>\u70ed\u5b58\u50a8\uff08Hot Storage\uff09<\/strong>\uff1a\u9002\u7528\u4e8e\u9ad8\u9891\u8bbf\u95ee\u6570\u636e\uff08\u5982\u4ea4\u6613\u6570\u636e\uff09\u3002<\/li>\n<li><strong>\u51b7\u5b58\u50a8\uff08Cold Storage\uff09<\/strong>\uff1a\u9002\u7528\u4e8e\u5386\u53f2\u5f52\u6863\u6570\u636e\uff08\u5982\u65e5\u5fd7\u6570\u636e\uff09\u3002<\/li>\n<\/ul>\n<h3>\u81ea\u52a8\u5f52\u6863\u7b56\u7565<\/h3>\n<h4>PostgreSQL \u5f52\u6863<\/h4>\n<pre><code class=\"sql\">CREATE TABLE orders_archive (LIKE orders);\n\nINSERT INTO orders_archive SELECT * FROM orders WHERE created_at &lt; NOW() - INTERVAL '1 year';\nDELETE FROM orders WHERE created_at &lt; NOW() - INTERVAL '1 year';<\/code><\/pre>\n<h2>\u7f13\u5b58\u4f18\u5316\uff1a\u964d\u4f4e\u6570\u636e\u5e93\u67e5\u8be2\u6210\u672c<\/h2>\n<h3>Redis \u7f13\u5b58\u67e5\u8be2\u7ed3\u679c<\/h3>\n<pre><code class=\"python\">import redis\nimport psycopg2\n\nr = redis.Redis(host='localhost', port=6379, db=0)\nconn = psycopg2.connect(&quot;dbname=test user=postgres&quot;)\ncursor = conn.cursor()\n\ndef get_user(user_id):\n    cached_user = r.get(f'user:{user_id}')\n    if cached_user:\n        return cached_user\n    cursor.execute(&quot;SELECT * FROM users WHERE id = %s&quot;, (user_id,))\n    user = cursor.fetchone()\n    r.setex(f'user:{user_id}', 3600, str(user))  # \u7f13\u5b58 1 \u5c0f\u65f6\n    return user<\/code><\/pre>\n<h3>\u4f7f\u7528 CDN \u7f13\u5b58\u9759\u6001\u8d44\u6e90<\/h3>\n<pre><code class=\"txt\">location \/static\/ {\n    root \/var\/www\/html;\n    expires 30d;\n    add_header Cache-Control &quot;public, max-age=2592000&quot;;\n}<\/code><\/pre>\n<h2>QA \u73af\u8282<\/h2>\n<h3>Q1: \u5982\u4f55\u9009\u62e9\u9002\u5408\u7684\u7d22\u5f15\u7c7b\u578b\uff1f<\/h3>\n<ul>\n<li><strong>B-Tree \u7d22\u5f15<\/strong>\uff1a\u9002\u7528\u4e8e\u5927\u591a\u6570\u67e5\u8be2\u3002<\/li>\n<li><strong>Hash \u7d22\u5f15<\/strong>\uff1a\u9002\u7528\u4e8e\u7b49\u503c\u67e5\u8be2\u3002<\/li>\n<li><strong>\u5168\u6587\u7d22\u5f15<\/strong>\uff1a\u9002\u7528\u4e8e\u6587\u672c\u641c\u7d22\u3002<\/li>\n<\/ul>\n<h3>Q2: \u6570\u636e\u5b58\u50a8\u5982\u4f55\u5212\u5206\u51b7\u70ed\u6570\u636e\uff1f<\/h3>\n<ul>\n<li><strong>\u8bbf\u95ee\u9891\u7e41\u7684\u6570\u636e\u4fdd\u7559\u5728\u4e3b\u6570\u636e\u5e93<\/strong><\/li>\n<li><strong>\u5386\u53f2\u6570\u636e\u8fc1\u79fb\u81f3\u51b7\u5b58\u50a8\uff08\u5982 AWS S3\u3001HDFS\uff09<\/strong><\/li>\n<\/ul>\n<h2>\u603b\u7ed3<\/h2>\n<ul>\n<li><strong>\u7d22\u5f15\u4f18\u5316<\/strong>\u00a0\u53ef\u63d0\u9ad8\u67e5\u8be2\u901f\u5ea6\uff0c\u907f\u514d\u5197\u4f59\u7d22\u5f15\u3002<\/li>\n<li><strong>\u51cf\u5c11\u5197\u4f59\u6570\u636e<\/strong>\uff0c\u91c7\u7528\u6570\u636e\u89c4\u8303\u5316\u548c\u53bb\u91cd\u7b56\u7565\u3002<\/li>\n<li><strong>\u51b7\u70ed\u6570\u636e\u7ba1\u7406<\/strong>\uff0c\u4f18\u5316\u5b58\u50a8\u6210\u672c\u3002<\/li>\n<li><strong>\u7f13\u5b58\uff08Redis\u3001CDN\uff09<\/strong>\u00a0\u964d\u4f4e\u6570\u636e\u5e93\u67e5\u8be2\u538b\u529b\u3002<\/li>\n<\/ul>\n<p>\u2705\u00a0<strong>\u81ea\u52a8\u5316\u6570\u636e\u5e93\u4f18\u5316\u7b56\u7565\uff08AI \u9a71\u52a8\u7d22\u5f15\u4f18\u5316\uff09<\/strong>\u00a0\u2705\u00a0<strong>\u7ed3\u5408\u5927\u6570\u636e\u6280\u672f\uff08\u5982 Apache Spark\uff09\u8fdb\u884c\u51b7\u70ed\u6570\u636e\u7ba1\u7406<\/strong>\u00a0\u2705\u00a0<strong>\u66f4\u591a\u6570\u636e\u5e93\u5f15\u64ce\u4f18\u5316\u63a2\u7d22\uff08\u5982 NewSQL \u65b9\u6848\uff09<\/strong><\/p>\n<h2>\u53c2\u8003\u8d44\u6599<\/h2>\n<ul>\n<li><a href=\"https:\/\/link.segmentfault.com\/?enc=Duge5xYT%2Bwe6iTeZwsVbWA%3D%3D.Hil1Iyv35acT3Ul39jTLwer6IvNqQG3h9KzM1WrnEiE%3D\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">MySQL \u5b98\u65b9\u6587\u6863<\/a><\/li>\n<li><a href=\"https:\/\/link.segmentfault.com\/?enc=FEN48iy9cW7tDi1MfQSFhw%3D%3D.jAc%2F%2BgN96G4RGjy5IVnX0V9MoPQMg1BFxbGYIt9KLqhoqqvi%2FQr0hvUQ18e13h9O\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">PostgreSQL \u5b98\u65b9\u6587\u6863<\/a><\/li>\n<li><a href=\"https:\/\/link.segmentfault.com\/?enc=XekHHqR%2BcBhJiwnoTQlZAQ%3D%3D.lb8mRr0YaJTFkWHfCtPVc5TbYEEvijQ8IZRjntHgoXw%3D\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">MongoDB \u5b98\u65b9\u6587\u6863<\/a><\/li>\n<li><a href=\"https:\/\/link.segmentfault.com\/?enc=HzEAUtVGxSA1XbwS8xN3%2Bw%3D%3D.5qLA56X1kgaDdo0nRpgZ%2FL1f8pL8B18aExBe3ckYhG4%3D\" rel=\"nofollow noopener noreferrer\" target=\"_blank\">Redis \u5b98\u65b9\u6587\u6863<\/a><\/li>\n<\/ul>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000046299174\">https:\/\/segmentfault.com\/a\/1190000046299174<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6458\u8981 \u6570\u636e\u5e93\u5b58\u50a8\u548c\u67e5\u8be2\u6210\u672c\u9ad8\uff0c\u7279\u522b\u662f\u6570\u636e\u91cf\u5927\u7684\u4f01\u4e1a\u9762\u4e34\u5de8\u5927\u7684\u6311\u6218\u3002\u672c\u7bc7\u6587\u7ae0\u5c06\u63a2\u8ba8\u00a0\u7d22\u5f15\u4f18\u5316\u3001\u51cf\u5c11\u5197\u4f59\u6570\u636e\u3001\u51b7\u70ed [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10020675","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10020675","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10020675"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10020675\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10020675"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10020675"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10020675"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}