{"id":488,"date":"2013-12-31T23:54:40","date_gmt":"2013-12-31T15:54:40","guid":{"rendered":"http:\/\/www.gpfeng.com\/?p=488"},"modified":"2014-01-01T00:42:28","modified_gmt":"2013-12-31T16:42:28","slug":"innodb%e5%b9%b6%e5%8f%91%e6%8e%a7%e5%88%b6%e6%a8%a1%e5%9e%8b","status":"publish","type":"post","link":"http:\/\/www.gpfeng.com\/?p=488","title":{"rendered":"InnoDB\u5e76\u53d1\u63a7\u5236\u6a21\u578b"},"content":{"rendered":"<p>\u8d76\u57282014\u4e4b\u524d\uff0c\u5199\u4e0b2013\u6700\u540e\u4e00\u7bc7\u6280\u672f\u535a\u5ba2\uff0c\u4ecb\u7ecd\u4e00\u4e0bInnDB\u7684\u5e76\u53d1\u63a7\u5236\u6a21\u578b<\/p>\n<h3>\u80cc\u666f\u77e5\u8bc6<\/h3>\n<p>InnoDB\u4e2d\u4e3a\u4ec0\u4e48\u9700\u8981\u5e76\u53d1\u63a7\u5236\uff1f\u8981\u56de\u7b54\u8fd9\u4e2a\u95ee\u9898\uff0c\u9700\u8981\u5148\u804a\u804acontext switch\uff0c\u5f53\u540c\u65f6\u6267\u884c\u7684\u8fdb\u7a0b\/\u7ebf\u7a0b\u8d85\u8fc7cpu\u6838\u5fc3\u6570\uff08\u8d85\u7ebf\u7a0b\u7684\u6570\u76ee\uff09\u65f6\uff0ccpu\u4ee5\u65f6\u95f4\u7247\u8f6e\u8f6c\u8c03\u5ea6\u6267\u884c\u591a\u4e2a\u8fdb\u7a0b\u7684\u65f6\u5019\u5c31\u6709context switch\u4e86\uff0c\u5f53\u8fdb\u7a0bA\u7684\u65f6\u95f4\u7247\u7528\u5b8c\u9700\u8981\u8c03\u5ea6\u53e6\u5916\u8fdb\u7a0bB\u65f6\uff0c\u9700\u8981\u5c06\u8fdb\u7a0bA\u7684\u72b6\u6001\u548c\u53c2\u6570\u4fdd\u5b58\u5728\u5806\u6808\u4e2d\uff0c\u7b49\u518d\u6b21\u8c03\u5ea6\u5230A\u65f6\uff0c\u518d\u4ece\u5806\u6808\u4e2d\u6062\u590d\u72b6\u6001\u548c\u53c2\u6570\uff0c\u8fd9\u4e2a\u5207\u6362\u7684\u4ee3\u4ef7\u5c31\u662fcontext switch\uff0ccontext switch\u7684\u4ee3\u4ef7\u6839\u636eCPU\u6027\u80fd\u53ca\u8fdb\u7a0b\u7a7a\u95f4\u5927\u5c0f\u7684\u4e0d\u540c\uff0c\u5f00\u9500\u4ece\u51e0\u5fae\u79d2\uff08us\uff09\u5230\u51e0\u6beb\u79d2\uff08ms\uff09\u7ea7\u522b\uff08\u53c2\u8003\uff1a<a href=http:\/\/www.cs.rochester.edu\/u\/cli\/research\/switch.pdf>Quantifying The Cost of Context Switch<\/a>\uff0c<a href=http:\/\/blog.tsunanet.net\/2010\/11\/how-long-does-it-take-to-make-context.html>how-long-does-it-take-to-make-context<\/a>\uff09\uff0c\u76f8\u5bf9\u4e8e\u4e00\u6b21\u5185\u5b58\u8bfb\u5199\u64cd\u4f5c\uff08100ns\u5de6\u53f3\uff09\uff0ccontext switch\u4ee3\u4ef7\u4e0d\u53ef\u5c0f\u89c6\uff08\u6309\u7167100us\u8ba1\u7b97\u7684\u8bdd\uff0c\u76f8\u5f53\u4e8e1000\u6b21\u5185\u5b58\u8bbf\u95ee\uff09<br \/>\n<!--more--><\/p>\n<h3>innodb_thread_concurrency<\/h3>\n<p>\u8fd9\u4e2a\u53c2\u6570\u5c31\u662f\u63a7\u5236\u8fdb\u5165\u5230InnoDB\u5c42\u7684\u5e76\u53d1\u7ebf\u7a0b\u6570\u76ee\uff0c\u9ed8\u8ba4\u4e3a0\uff0c\u8868\u793a\u4e0d\u53d7\u9650\u5236\uff0c\u7ebf\u4e0a\u6211\u4eec\u4e00\u822c\u8bbe\u7f6e\u4e3a32\u6216\u800564<\/p>\n<p>\u5f53\u540c\u65f6\u5e76\u53d1\uff08\u903b\u8f91\u4e0a\uff09\u7684\u8fdb\u7a0b\/\u7ebf\u7a0b\u6570\u76ee\u8fdc\u8fdc\u8d85\u8fc7cpu\u6838\u5fc3\u6570\u65f6\uff0ccontext switch\u5c31\u4f1a\u975e\u5e38\u9891\u7e41\uff0c\u6700\u7ec8\u5bfc\u81f4\u6027\u80fd\u95ee\u9898\uff08\u4e00\u8f6econtext switch\u7684\u65f6\u95f4\u4e5f\u8bb8\u591f\u5904\u7406\u4e00\u4e2a\u4efb\u52a1\u4e86\uff09\uff0c\u8fd9\u65f6\u5019\u5982\u679c\u9650\u5236\u5e76\u53d1\u8fd0\u884c\u7684\u8fdb\u7a0b\/\u7ebf\u7a0b\u6570\u76ee\u5728cpu\u6838\u5fc3\u6570\uff0c\u5c31\u4f1a\u6781\u5927\u63d0\u9ad8\u6027\u80fd\uff0c\u770b\u6548\u679c\u56fe\uff1a<\/p>\n<p><a href=\"https:\/\/i0.wp.com\/www.gpfeng.com\/wp-content\/uploads\/2014\/01\/innodb_thread_concurrency.jpg\"><img decoding=\"async\" src=\"https:\/\/i0.wp.com\/www.gpfeng.com\/wp-content\/uploads\/2014\/01\/innodb_thread_concurrency.jpg?w=660\" alt=\"innodb_thread_concurrency_test\" data-recalc-dims=\"1\" \/><\/a><\/p>\n<p>\u5f53innodb_thread_concurrency\u8bbe\u7f6e\u4e3a0\u65f6\uff0cinnodb\u5e76\u53d1\u63a7\u5236\u4e0d\u8d77\u4f5c\u7528\uff0c\u5bfc\u81f4\u8fdb\u5165\u5230innodb\u5c42\u7684\u7ebf\u7a0b\u6570\u4e0d\u53d7\u63a7\u5236\uff0c\u53ef\u4ee5\u4ece\u56fe\u4e2d\u89c2\u5bdf\u5230QPS\u6025\u5267\u4e0b\u964d\uff0c\u5f53\u5e76\u53d1\u7ebf\u7a0b\u8d85\u8fc74096\u65f6\uff0cMySQL\u57fa\u672c\u4e0d\u80fd\u54cd\u5e94\u4e86\uff08\u673a\u5668\u786c\u4ef6\uff1a2\u4e2acpu\uff0c12\u6838\u5fc3\/cpu\uff0c192G\u5185\u5b58\uff09\uff0c\u7136\u800c\u8bbe\u7f6einnodb_thread_concurrency\u4e3a32\u65f6\uff0c\u6027\u80fd\u4e0b\u964d\u5f97\u6ca1\u6709\u90a3\u4e48\u5389\u5bb3<\/p>\n<h3>InnoDB\u5e76\u53d1\u63a7\u5236\u5b9e\u73b0<\/h3>\n<p>\u7b97\u6cd5\u4ecb\u7ecd\uff08innodb_thread_concurrency > 0\uff09\uff1a<br \/>\n1\u3001server\u5c42\u5230innodb\u5c42\u8bfb\u5199\u6570\u636e\u662f\u4e00\u6761\u4e00\u6761\u8bb0\u5f55\u8fdb\u884c\u7684\uff0c\u6bcf\u6b21\u8bfb\u5199\u90fd\u4f1a\u8fdb\/\u51fa\u4e00\u6b21InnoDB\u5c42\uff08row_search_for_mysql\uff09\uff0c\u8fdb\u5165InnoDB\u5c42\u7684\u65f6\u5019\u4f1a\u68c0\u67e5\u5f53\u524d\u5e76\u53d1\u7ebf\u7a0b\u6570\u76ee\uff0c\u5f53\u8d85\u8fc7innodb_thread_concurrency\u65f6\uff0c\u7ebf\u7a0b\u5c06\u5c1d\u8bd5spin\u548csleep\u5e76\u518d\u6b21\u68c0\u67e5\uff0c\u5982\u679c\u5e76\u53d1\u6570\u8fd8\u662f\u8d85\u8fc7innodb_thread_concurrency\uff0c\u7ebf\u7a0b\u5c06\u8fdb\u5165\u5230\u4e00\u4e2aFIFO\u4e2d\u7b49\u5f85\u88ab\u5524\u9192\uff0c\u8bfb\u5199\u8bb0\u5f55\u7ed3\u675f\u540e\u9000\u51faInnoDB\u5c42\u65f6\u4f1a\u5c06\u5f53\u524d\u5e76\u53d1\u7ebf\u7a0b\u6570\u51cf1\uff0c\u5e76\u68c0\u67e5\u5176\u662f\u5426\u4f4e\u4e8einnodb_thread_concurrency\uff0c\u5982\u679c\u662f\u7684\u8bdd\uff0c\u4eceFIFO\u4e2d\u5524\u9192\u4e00\u4e2a\u7b49\u5f85\u7684\u7ebf\u7a0b\uff0c\u4fdd\u8bc1\u5e76\u53d1\u7ebf\u7a0b\u4e0d\u4f1a\u8d85\u8fc7innodb_thread_concurrency\u53c2\u6570<br \/>\n2\u3001\u5f53\u7ebf\u7a0b\u8fdb\u5165InnoDB\u5c42\u540e\uff0c\u4f46\u5728\u83b7\u53d6\u6570\u636e\u65f6\u7531\u4e8e\u9501\u8bf7\u6c42\u65e0\u6cd5\u5f97\u5230\u6ee1\u8db3\u800c\u9700\u8981\u6302\u8d77\u65f6\uff0c\u7ebf\u7a0b\u5c06\u5f3a\u5236\u9000\u51faInnoDB\u5c42\uff0c\u5f53\u9501\u8bf7\u6c42\u6ee1\u8db3\u540e\uff0c\u7ebf\u7a0b\u7ee7\u7eed\u8fd0\u884c\u5e76\u5f3a\u5236\u8fdb\u5165\u5230InnoDB\u5c42\uff0c\u8fd9\u4f1a\u5bfc\u81f4\u5b9e\u9645\u5e76\u53d1\u7ebf\u7a0b\u6570\u4e0d\u662f\u4e25\u683c\u63a7\u5236\u5728innodb_thread_concurrency\u4e4b\u5185<\/p>\n<h3>\u4ee3\u7801\u8c03\u7528\u903b\u8f91<\/h3>\n<h4>\u4e3b\u8981\u51fd\u6570\uff1a<\/h4>\n<p>innodb_srv_conc_enter_innodb<br \/>\ninnodb_srv_conc_exit_innodb<br \/>\nsrv_conc_force_enter_innodb<br \/>\nsrv_conc_force_exit_innodb<\/p>\n<h4>\u8c03\u7528\u903b\u8f91<\/h4>\n<p>\u8fdb\u5165\/\u9000\u51faInnoDB\u5c42<\/p>\n<pre lang=\"c\">\r\n\r\nha_innobase::index_read\r\nha_innobase::general_fetch\r\nrow_check_index_for_mysql\r\n\t...\r\n\tinnodb_srv_conc_enter_innodb(prebuilt->trx);\r\n\r\n\tret = row_search_for_mysql((byte*) buf, mode, prebuilt,\r\n\t\t\t\t\t   match_mode, 0);\r\n\r\n\tinnodb_srv_conc_exit_innodb(prebuilt->trx);\r\n\t...\r\n<\/pre>\n<p>\u9501\u7b49\u5f85\u903b\u8f91\uff1a<\/p>\n<pre lang=\"c\">\r\nsrv_suspend_mysql_thread\r\n\t...\r\n\tif (trx->declared_to_be_inside_innodb) {\r\n\r\n\t\twas_declared_inside_innodb = TRUE;\r\n\r\n\t\t\/* We must declare this OS thread to exit InnoDB, since a\r\n\t\tpossible other thread holding a lock which this thread waits\r\n\t\tfor must be allowed to enter, sooner or later *\/\r\n\t\tsrv_conc_force_exit_innodb(trx);\r\n\t}\r\n\r\n\t\/* Suspend this thread and wait for the event. *\/\r\n\r\n\tthd_wait_begin(trx->mysql_thd, THD_WAIT_ROW_LOCK);\r\n\tos_event_wait(event);\r\n\tthd_wait_end(trx->mysql_thd);\r\n\r\n\tif (was_declared_inside_innodb) {\r\n\r\n\t\t\/* Return back inside InnoDB *\/\r\n\t\tsrv_conc_force_enter_innodb(trx);\r\n\t}\r\n<\/pre>\n<p>\u81f3\u4e8e\u66f4\u52a0\u5177\u4f53\u7684\u5b9e\u73b0\u7ec6\u8282\uff0c\u611f\u5174\u8da3\u7684\u8bdd\u8fd8\u9700\u9605\u8bfb\u4ee3\u7801<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8d76\u57282014\u4e4b\u524d\uff0c\u5199\u4e0b2013\u6700\u540e\u4e00\u7bc7\u6280\u672f\u535a\u5ba2\uff0c\u4ecb\u7ecd\u4e00\u4e0bInnDB\u7684\u5e76\u53d1\u63a7\u5236\u6a21\u578b \u80cc\u666f\u77e5\u8bc6 InnoDB\u4e2d\u4e3a\u4ec0 &hellip; <a href=\"http:\/\/www.gpfeng.com\/?p=488\" class=\"more-link\">\u7ee7\u7eed\u9605\u8bfb<span class=\"screen-reader-text\">InnoDB\u5e76\u53d1\u63a7\u5236\u6a21\u578b<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[1],"tags":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p3tPZp-7S","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"http:\/\/www.gpfeng.com\/index.php?rest_route=\/wp\/v2\/posts\/488"}],"collection":[{"href":"http:\/\/www.gpfeng.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.gpfeng.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.gpfeng.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.gpfeng.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=488"}],"version-history":[{"count":7,"href":"http:\/\/www.gpfeng.com\/index.php?rest_route=\/wp\/v2\/posts\/488\/revisions"}],"predecessor-version":[{"id":491,"href":"http:\/\/www.gpfeng.com\/index.php?rest_route=\/wp\/v2\/posts\/488\/revisions\/491"}],"wp:attachment":[{"href":"http:\/\/www.gpfeng.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=488"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.gpfeng.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=488"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.gpfeng.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=488"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}