{"id":412,"date":"2024-10-16T23:30:32","date_gmt":"2024-10-16T15:30:32","guid":{"rendered":"https:\/\/cococat.top\/?p=412"},"modified":"2024-10-16T23:30:32","modified_gmt":"2024-10-16T15:30:32","slug":"dev-experiences","status":"publish","type":"post","link":"https:\/\/cococat.top\/index.php\/2024\/10\/16\/dev-experiences\/","title":{"rendered":"\u6742\u8c08\uff1a\u4e00\u4e9b\u7ecf\u9a8c"},"content":{"rendered":"<h2>1. \u4e1a\u52a1\u903b\u8f91\u4e2d\u7684\u5f02\u6b65\u65e5\u5fd7<\/h2>\n<p>\u5728\u5e94\u7528\u7684\u521d\u671f\u7248\u672c\uff0c\u6240\u6709\u65e5\u5fd7\u53ef\u80fd\u90fd\u662f\u901a\u8fc7 <code>tracing<\/code> \u8f93\u51fa\u5230\u5916\u90e8\u6587\u4ef6\u7684\uff0c\u8fd9\u662f\u975e\u5e38\u6b63\u5e38\u7684\u60c5\u51b5\uff1b\u7b49\u5230\u5f00\u59cb\u8fdb\u884c\u53ef\u89c2\u6d4b\u80fd\u529b\u5efa\u8bbe\u540e\uff0c\u5c31\u9700\u8981\u5f15\u5165\u4e00\u4e9b\u65e5\u5fd7\u4e2d\u95f4\u4ef6\uff0c\u6bd4\u5982\u5c06\u4e00\u4e9b\u91cd\u8981\u65e5\u5fd7\u8f93\u51fa\u5230 SLS \u3002\u8fd9\u4e9b\u903b\u8f91\u5f80\u5f80\u4f1a\u7a7f\u63d2\u5728\u6211\u4eec\u4e1a\u52a1\u51fd\u6570\u4e2d\uff0c\u4e8e\u662f\u5c31\u5f15\u51fa\u4e00\u4e2a\u57fa\u672c\u7684\u539f\u5219\uff1a\u65e5\u5fd7\u65e0\u8bba\u5982\u4f55\u4e0d\u80fd\u963b\u585e\u4e1a\u52a1\u672c\u8eab\u3002\uff08\u5bf9\u4e8e\u65e5\u5fd7\u4e4b\u5916\u7684\u975e\u4e1a\u52a1\u903b\u8f91\uff0c\u5176\u5b9e\u4e5f\u662f\u4e00\u6837\u7684\uff09<\/p>\n<p>\u4ece\u53cd\u9762\u4f8b\u5b50\u5f00\u59cb\u8bf4\u8d77\uff1a<\/p>\n<p>\u5047\u8bbe\u6211\u4eec\u6700\u521d\u6709\u8fd9\u6837\u7684\u4e00\u4e2a\u4e1a\u52a1\u51fd\u6570\uff1a<\/p>\n<pre><code class=\"language-rust\">async fn business() {\n    \/\/ ...\n    do_some_biz();\n    let log = generate_important_log();\n    tracing::info!(&quot;[biz] {log}&quot;);\n    do_other_biz();\n    \/\/ ...\n}\n<\/code><\/pre>\n<p>\u8fd9\u4e2a\u7248\u672c\u6ca1\u6709\u4efb\u4f55\u95ee\u9898\u3002\u73b0\u5728\uff0c\u6211\u4eec\u9700\u8981\u63a5\u5165\u67d0\u4e9b\u9ad8\u5927\u4e0a\u7684\u65e5\u5fd7\u4e2d\u95f4\u4ef6\uff0c\u8fd9\u91cc\u4e3a\u4e86\u7b80\u5316\uff0c\u5047\u8bbe\u65e5\u5fd7\u6295\u9012\u65b9\u5f0f\u662f\u4e00\u4e2a http \u63a5\u53e3\uff0c\u90a3\u4e48\u6700\u5bb9\u6613\u60f3\u5230\uff08\u4e5f\u662f\u6700\u7cdf\u7cd5\uff09\u7684\u5199\u6cd5\u5c31\u662f\u76f4\u63a5\u628a <code>tracing<\/code> \u8fd9\u884c\u6362\u6389\uff1a<\/p>\n<pre><code class=\"language-rust\">async fn business() {\n    \/\/ ...\n    do_some_biz();\n    let log = generate_important_log();\n    let response = reqwest::Client::new()\n        .post(&quot;https:\/\/naive-logger.com\/api\/push&quot;)\n        .body(log.to_string())\n        .send()\n        .await?;\n\n    do_other_biz();\n    \/\/ ...\n}\n<\/code><\/pre>\n<p>\u663e\u800c\u6613\u89c1\uff0c\u6211\u4eec\u7684\u7b2c\u4e8c\u6bb5\u4e1a\u52a1\u903b\u8f91 <code>do_other_biz<\/code> \u9700\u8981\u7b49\u5f85\u65e5\u5fd7\u6295\u9012\u5b8c\u6210\u3002\u5982\u679c\u65e5\u5fd7\u670d\u52a1\u51fa\u73b0\u95ee\u9898\u800c\u5bfc\u81f4\u8bf7\u6c42\u8d85\u65f6\uff0c\u90a3\u4e48\u5728\u8fd9\u671f\u95f4\u4e1a\u52a1\u5c31\u4f1a\u88ab\u963b\u585e\u4f4f\u3002<\/p>\n<p>\u4fee\u590d\u8fd9\u4e2a\u95ee\u9898\u7684\u601d\u8def\uff0c\u65e0\u975e\u5c31\u662f\u8ba9\u4e1a\u52a1\u4e0e\u65e5\u5fd7\u903b\u8f91\u5206\u9694\u5f00\u6765\uff0c\u6216\u8005\u8ba9\u6240\u6709\u65e5\u5fd7\u76f8\u5173\u7684\u903b\u8f91\u7b49\u5230\u4e1a\u52a1\u5f7b\u5e95\u5b8c\u6210\u4ee5\u540e\u518d\u8fdb\u884c\u5904\u7406\u3002\u5b9e\u9645\u5199\u6cd5\u91cc\uff0c\u4f7f\u7528\u5206\u9694\u65b9\u6848\u7684\u573a\u666f\u662f\u6bd4\u8f83\u591a\u7684\u3002<\/p>\n<p>\u90a3\u4e48\uff0c\u4f3c\u4e4e\u7b80\u5355\u5730\u628a\u65e5\u5fd7\u903b\u8f91 <code>spawn<\/code> \u51fa\u53bb\u5373\u53ef\uff1f\u6bd4\u5982\u4e0b\u9762\u8fd9\u6837\uff1a<\/p>\n<pre><code class=\"language-rust\">async fn business() {\n    \/\/ ...\n    do_some_biz();\n\n    let log = generate_important_log();\n    tokio::spawn(async {\n        let response = reqwest::Client::new()\n            .post(&quot;https:\/\/naive-logger.com\/api\/push&quot;)\n            .body(log.to_string())\n            .send()\n            .await;\n    });\n\n    do_other_biz();\n    \/\/ ...\n}<\/code><\/pre>\n<p>\u8fd9\u4e2a\u6539\u52a8\u8868\u9762\u4e0a\u770b\u8d77\u6765\u80fd\u89e3\u51b3\u4e1a\u52a1\u88ab\u963b\u585e\u7684\u95ee\u9898\u3002\u4f46\u662f\u5b9e\u9645\u4e0a\uff0c\u8fd9\u79cd\u5199\u6cd5\u4e5f\u662f\u4e0d\u5408\u7406\u7684\uff1a\u5bf9\u4e8e\u4e1a\u52a1\u6d41\u91cf\u5f88\u5927\u7684\u573a\u666f\uff0c\u5982\u679c\u65e5\u5fd7\u6295\u9012\u6a21\u5757\u8d85\u65f6\uff0c\u5219\u4f1a\u51fa\u73b0\u5927\u91cf\u7684 tokio \u5f02\u6b65\u4efb\u52a1\u7d2f\u79ef\uff0c\u6700\u7ec8\u8fd8\u662f\u6709\u53ef\u80fd\u6253\u57ae\u6211\u4eec\u7684\u670d\u52a1\u3002<\/p>\n<p>\u5bf9\u4e8e\u7c7b\u4f3c\u7684\u573a\u666f\uff0c\u6700\u4f73\u7684\u89e3\u51b3\u65b9\u6848\u662f\uff0c\u4e00\u65e6\u5f15\u5165\u4e86\u8fd9\u79cd\u670d\u52a1\uff0c\u6211\u4eec\u7aef\u4fa7\u7684\u65e5\u5fd7\u6a21\u5757\u5c31\u5fc5\u987b\u6539\u6210\u57fa\u4e8e\u6709\u9650\u957f\u5ea6\u961f\u5217\u7684\u7684\u751f\u4ea7\u8005-\u6d88\u8d39\u8005\u6a21\u5f0f\u3002\u73b0\u6210\u7684\u65b9\u6848\u662f\u57fa\u4e8e <code>tokio::sync::mpsc::channel<\/code> \u8fdb\u884c\u5f00\u53d1\uff0c\u8fd9\u4e2a channel \u6709\u7740\u786e\u5b9a\u957f\u5ea6\u7684\u6d88\u606f\u7f13\u51b2\u533a\u3002\u4e0e\u4e4b\u5bf9\u5e94\u7684\u4e0d\u9650\u957f\u5ea6 channel \u5219\u662f <code>tokio::sync::mpsc::unbounded_channel<\/code> \u3002\u6b64\u5916\uff0c\u5f53\u961f\u5217\u6ee1\u5bfc\u81f4\u65e5\u5fd7\u65e0\u6cd5\u6b63\u5e38\u6295\u9012\u65f6\uff0c\u53ef\u4ee5\u5728\u9519\u8bef\u5904\u7406\u4e2d\u589e\u52a0\u843d\u76d8\u7684\u903b\u8f91\u3002<\/p>\n<h2>2. \u5728\u4e1a\u52a1\u7cfb\u7edf\u4e2d\u4f7f\u7528\u8fde\u63a5\u6c60\u65f6\u6ce8\u610f\u8c03\u6574\u914d\u7f6e<\/h2>\n<p>reqwest \u5e95\u5c42\u5c01\u88c5\u4e86 hyper\uff0c\u76f4\u63a5\u5229\u7528\u4e86 hyper \u7684\u8fde\u63a5\u6c60\u3002\u5728 <code>reqwest::Client<\/code> \u7684\u6587\u6863\u4e2d\u6709\u8fd9\u4e48\u4e00\u6bb5\u8bdd\uff1a<\/p>\n<blockquote>\n<p>The Client holds a connection pool internally, so it is advised that you create one and reuse it.<br \/>\nYou do not have to wrap the Client in an [Rc] or Arc to reuse it, because it already uses an Arc internally.<\/p>\n<\/blockquote>\n<p>\u53ef\u89c1\uff0c\u5b98\u65b9\u662f\u63a8\u8350\u6211\u4eec\u590d\u7528 client \u7684\uff0c\u8fd9\u6837\u53ef\u4ee5\u5145\u5206\u5229\u7528\u5176\u5185\u5efa\u8fde\u63a5\u6c60\u7279\u6027\u7684\u3002\u5728\u4e1a\u52a1\u7cfb\u7edf\u4e2d\u8c03\u7528\u4e0a\u4e0b\u6e38 api \u65f6\uff0c\u901a\u8fc7\u957f\u671f\u6301\u6709\u4e00\u4e2a client\uff0c\u53ef\u4ee5\u901a\u8fc7\u8fde\u63a5\u590d\u7528\u7f13\u89e3\u5bf9\u65b9\u538b\u529b\uff0c\u4e5f\u80fd\u63d0\u9ad8\u6027\u80fd\u3002<\/p>\n<p>\u4e0d\u8fc7\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u9700\u8981\u786e\u8ba4\u670d\u52a1\u65b9\u7684 http keepalive \u8d85\u65f6\u914d\u7f6e\u4e0e\u4e00\u6761 TCP \u8fde\u63a5\u7684\u8bf7\u6c42\u590d\u7528\u6b21\u6570\u9650\u5236\u3002\u4ee5 NGINX \u4e3e\u4f8b\uff0c\u8fd9\u4e24\u4e2a\u914d\u7f6e\u5206\u522b\u662f <code>keepalive_timeout<\/code> \u548c <code>keepalive_request<\/code> \u3002\u5982\u679c\u8fde\u63a5\u6c60\u7684\u914d\u7f6e\u5927\u4e8e\u8fd9\u4e8c\u8005\uff0c\u5219\u53ef\u80fd\u51fa\u73b0\u9884\u671f\u5916\u7684\u8c03\u7528\u5931\u8d25\u3002<\/p>\n<h2>3. \u4e0d\u8981 select *<\/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>\u4e1a\u52a1\u517c\u5bb9\u4e0d\u4f73\uff0c\u672a\u6765\u65b0\u589e\u5b57\u6bb5\u4ee5\u540e\uff0c\u53ef\u80fd\u5f71\u54cd\u5230 sql \u7ed3\u679c\u89e3\u6790\u3002<\/li>\n<\/ul>\n<h2>4. \u591a\u4e2a insert \u7684\u90e8\u5206\u7528\u4e00\u884c sql \u89e3\u51b3<\/h2>\n<p>\u867d\u7136\u4f1a\u6d89\u53ca\u5230\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>5. \u591a\u601d\u8003\u81ea\u6062\u590d\u95ee\u9898<\/h2>\n<p>\u5bf9\u4e0a\u4e0b\u6e38\u4f9d\u8d56\u5b9e\u73b0\u4e86\u5bb9\u707e\u4ee5\u540e\uff0c\u7b49\u5230\u5bf9\u7aef\u6062\u590d\u4ee5\u540e\uff0c\u6211\u4eec\u600e\u4e48\u6062\u590d\u5230\u6700\u65b0\u7684\u6b63\u786e\u6570\u636e\uff1f<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. \u4e1a\u52a1\u903b\u8f91\u4e2d\u7684\u5f02\u6b65\u65e5\u5fd7 \u5728\u5e94\u7528\u7684\u521d\u671f\u7248\u672c\uff0c\u6240\u6709\u65e5\u5fd7\u53ef\u80fd\u90fd\u662f\u901a\u8fc7 tracing \u8f93\u51fa\u5230\u5916\u90e8\u6587\u4ef6\u7684\uff0c\u8fd9\u662f\u975e\u5e38 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-412","post","type-post","status-publish","format-standard","hentry","category-rust"],"_links":{"self":[{"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/posts\/412","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=412"}],"version-history":[{"count":0,"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/posts\/412\/revisions"}],"wp:attachment":[{"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/media?parent=412"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/categories?post=412"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cococat.top\/index.php\/wp-json\/wp\/v2\/tags?post=412"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}