{"id":477,"date":"2025-01-12T11:07:26","date_gmt":"2025-01-12T03:07:26","guid":{"rendered":"https:\/\/cococat.top\/?p=477"},"modified":"2025-01-12T11:07:26","modified_gmt":"2025-01-12T03:07:26","slug":"%e6%9d%82%e8%b0%88%ef%bc%9a%e6%95%b0%e6%8d%ae%e5%ba%93%e7%9b%b8%e5%85%b3%e7%bb%8f%e9%aa%8c","status":"publish","type":"post","link":"https:\/\/cococat.top\/index.php\/2025\/01\/12\/%e6%9d%82%e8%b0%88%ef%bc%9a%e6%95%b0%e6%8d%ae%e5%ba%93%e7%9b%b8%e5%85%b3%e7%bb%8f%e9%aa%8c\/","title":{"rendered":"\u6742\u8c08\uff1a\u6570\u636e\u5e93\u76f8\u5173\u7ecf\u9a8c"},"content":{"rendered":"<h2>1. \u4e0d\u8981 <code>select *<\/code><\/h2>\n<ul>\n<li>\u5bf9\u4e8e\u590d\u6742\u7684\u8868\uff0c\u78c1\u76d8 IO \u5927\uff0c\u6d6a\u8d39\u5e26\u5bbd<\/li>\n<li>\u53ef\u80fd\u65e0\u6cd5\u5145\u5206\u5229\u7528\u7d22\u5f15\u3002\u53c2\u8003 <a href=\"https:\/\/cococat.top\/index.php\/2022\/06\/05\/mysql_innodb_learning_note\/\">\u4e4b\u524d\u7684 mysql \u7b14\u8bb0<\/a> \u7684\u7b2c 24 \u8282<\/li>\n<li>\u517c\u5bb9\u4e0d\u4f73\uff0c\u672a\u6765\u65b0\u589e\u5217\u4ee5\u540e\uff0c\u53ef\u80fd\u5f71\u54cd\u5230 sql \u7ed3\u679c\u89e3\u6790\u3002<\/li>\n<\/ul>\n<h2>2. \u591a\u4e2a insert \u7684\u90e8\u5206\u7528\u4e00\u884c sql \u89e3\u51b3<\/h2>\n<p>\u867d\u7136\u4f1a\u5199\u4e00\u4e9b\u6bd4\u8f83\u6076\u5fc3\u7684 sql \u5b57\u7b26\u4e32\u62fc\u63a5\uff0c\u4f46\u662f\u4e5f\u6709\u6536\u76ca\u3002\u8fd9\u91cc\u76f4\u63a5\u53c2\u8003 GPT\uff1a<\/p>\n<ol>\n<li>\n<p>Reduced Overhead<br \/>\nEach SQL statement incurs an overhead for parsing, planning, and network communication between the application and the database. By batching rows in a single INSERT, you reduce this overhead significantly by dealing with it only once.<\/p>\n<\/li>\n<li>\n<p>Fewer Network Round-Trips:<br \/>\nA single batched insert minimizes the number of network round-trips between your application and the database server. Network latency can be a non-trivial component of the total operation time, especially in distributed environments.<\/p>\n<\/li>\n<li>\n<p>Efficient Use of Transactions:<br \/>\nIf you're using multiple separate INSERT statements, and each statement is executed in its own transaction, the overhead of starting and committing multiple transactions can be significant. A single INSERT for multiple rows can be executed within a single transaction, reducing the transaction management overhead.<\/p>\n<\/li>\n<li>\n<p>Bulk Insert Optimizations:<br \/>\nDatabases like MySQL can perform internal optimizations when handling a batch of inserts, such as minimizing index updates or reducing write operations until all data is inserted. This can lead to further performance benefits.<\/p>\n<\/li>\n<li>\n<p>Locking and Concurrency:<br \/>\nFewer transactions mean reduced locking and thus better concurrency and reduced potential for contention, especially in tables that are frequently written to.<\/p>\n<\/li>\n<\/ol>\n<h2>3. \u52a0\u8868\u9501\u7684\u6b63\u786e\u59ff\u52bf<\/h2>\n<p>\u5148\u5173\u95ed autocommit\u3002<\/p>\n<pre><code class=\"language-sql\">SET autocommit=0;\nLOCK TABLES t1 WRITE, t2 READ, ...;\n... do something with tables t1 and t2 here ...\nCOMMIT;\nUNLOCK TABLES;<\/code><\/pre>\n<p>\u8fd9\u4e48\u64cd\u4f5c\u7684\u539f\u56e0\u53c2\u8003 mysql <a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.4\/en\/lock-tables.html#lock-tables-and-transactions\">\u5b98\u65b9\u6587\u6863<\/a> \u3002\u53e6\u5916\uff0c\u5982\u679c\u6761\u4ef6\u5141\u8bb8\uff0c\u4e5f\u53ef\u4ee5\u8003\u8651\u6362\u7528 <a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.4\/en\/locking-functions.html\">locking functions<\/a>\uff0c\u8fd9\u662f\u4e00\u79cd\u4e0d\u7ecf\u8fc7\u5b58\u50a8\u5f15\u64ce\u7684\u9501\uff0c\u66f4\u52a0\u9ad8\u6548\u3002<\/p>\n<h2>4. \u7f13\u5b58\u4e00\u81f4\u6027\u76f8\u5173<\/h2>\n<p>\u51e0\u4e2a\u539f\u5219\uff1a<\/p>\n<ol>\n<li>\u5224\u65ad\u81ea\u5df1\u7684\u4e1a\u52a1\u573a\u666f\u9700\u8981\u5f3a\u4e00\u81f4\u6027\uff08\u6216\u79f0\u5b9e\u65f6\u4e00\u81f4\u6027\uff09\u8fd8\u662f\u6700\u7ec8\u4e00\u81f4\u6027\n<ul>\n<li>\u5f3a\u4e00\u81f4\u6027\uff1a\u7528\u4e8e\u8d22\u52a1\u7b49\u4e25\u683c\u4e1a\u52a1\u3002\u4f9d\u8d56\u4e8e\u5168\u5c40\u552f\u4e00\u9501\uff08\u8868\u9501\u3001\u5206\u5e03\u5f0f\u9501\u7b49\uff09\u5b9e\u73b0\uff0c\u4f1a\u964d\u4f4e\u541e\u5410\u91cf\u3002<\/li>\n<li>\u6700\u7ec8\u4e00\u81f4\u6027\uff1a\u4f9d\u9760\u5404\u79cd\u4fdd\u969c\u7b56\u7565\u6765\u5b9e\u73b0\u5ef6\u8fdf\u7684\u4e00\u81f4\u6027\u3002\u4f8b\u5982\uff0c\u8bfb\u64cd\u4f5c\u65f6\u7684\u5237\u7f13\u5b58\u5c31\u662f\u5929\u7136\u7684\u624b\u6bb5\uff1b\u6b64\u5916\u8fd8\u53ef\u4ee5\u6709\u540e\u53f0\u7684\u4fee\u590d\u8fdb\u7a0b\u7b49\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\u6807\u51c6\u7684 cache-aside \u65b9\u6848\n<ul>\n<li>\u76ee\u7684\uff1a\u6700\u7ec8\u4e00\u81f4\u6027<\/li>\n<li>\u8bfb\u64cd\u4f5c\uff1a\u82e5 cache miss\uff0c\u5219\u8bfb\u5e93\u5e76\u5237\u5165\u7f13\u5b58<\/li>\n<li>\u5199\u64cd\u4f5c\uff1a\u5148\u8bfb\u5e93\uff0c\u7136\u540e<strong>\u5220\u9664<\/strong>\u7f13\u5b58<\/li>\n<li>\u53e6\u5916\uff0c\u53ef\u4ee5\u52a0\u5165\u5ef6\u8fdf\u53cc\u5220\uff0c\u7528\u6765\u89e3\u51b3\u7f13\u5b58\u6070\u597d\u5931\u6548\u65f6\u7684\u5e76\u53d1\u8bfb\u5199\u5e26\u6765\u7684<a href=\"https:\/\/note.dolyw.com\/cache\/00-DataBaseConsistency.html#%E5%85%88%E6%9B%B4%E6%96%B0%E6%95%B0%E6%8D%AE%E5%BA%93-%E5%86%8D%E5%88%A0%E9%99%A4%E7%BC%93%E5%AD%98\">\u4e0d\u4e00\u81f4\u95ee\u9898<\/a>\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>1. \u4e0d\u8981 select * \u5bf9\u4e8e\u590d\u6742\u7684\u8868\uff0c\u78c1\u76d8 IO \u5927\uff0c\u6d6a\u8d39\u5e26\u5bbd \u53ef\u80fd\u65e0\u6cd5\u5145\u5206\u5229\u7528\u7d22\u5f15\u3002\u53c2\u8003 \u4e4b\u524d\u7684 my [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[21],"class_list":["post-477","post","type-post","status-publish","format-standard","hentry","category-10","tag-mysql"],"_links":{"self":[{"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/posts\/477","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/comments?post=477"}],"version-history":[{"count":0,"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/posts\/477\/revisions"}],"wp:attachment":[{"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/media?parent=477"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/categories?post=477"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/tags?post=477"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}