{"id":1078,"date":"2023-03-12T11:00:28","date_gmt":"2023-03-12T03:00:28","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1078"},"modified":"2023-04-29T11:42:39","modified_gmt":"2023-04-29T03:42:39","slug":"reason-why-mysql-query-are-slow-despite-not-having-a-lot-of-data","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/12\/reason-why-mysql-query-are-slow-despite-not-having-a-lot-of-data\/","title":{"rendered":"MySQL\u67e5\u8be2\u6570\u636e\u4e0d\u591a\uff0c\u4f46\u5374\u5f88\u6162\u7684\u539f\u56e0"},"content":{"rendered":"<p>\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u8bb0\u5f55\uff0c\u9020\u6210\u67e5\u8be2\u6570\u91cf\u4e0d\u5927\u7684\u60c5\u51b5\u4e0b\uff0c\u9020\u6210\u67e5\u8be2\u7f13\u6162\u7684\u539f\u56e0\uff0c\u4ee5\u53ca\u76f8\u5e94\u7684\u89e3\u51b3\u65b9\u6cd5\u3002<\/p>\n<p>\u73af\u5883\u8bf4\u660e\uff1a<\/p>\n<ul>\n<li>MySQL \u7248\u672c 5.7.27<\/li>\n<li>\u9694\u79bb\u7ea7\u522b\uff1aRR<\/li>\n<\/ul>\n<p><!-- more --><\/p>\n<h2>\u9501\u7b49\u5f85\u9020\u6210\u67e5\u8be2\u901f\u5ea6\u5f88\u6162<\/h2>\n<h3>MDL \u9501<\/h3>\n<p>\u5982\u5f00\u542f\u5982\u4e0b\u4e8b\u52a1\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: left;\">Session A<\/th>\n<th style=\"text-align: left;\">Session B<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: left;\">lock table t write;<\/td>\n<td style=\"text-align: left;\"><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><\/td>\n<td style=\"text-align: left;\">begin;<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><\/td>\n<td style=\"text-align: left;\">select * from t where id=1;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Session A \u6301\u6709\u8868 t MDL \u5199\u9501\u3002Session B \u9700\u8981 MDL \u8bfb\u9501\u3002\u8fd9\u65f6\u8bfb\u5199\u9501\u4e92\u65a5\uff0cSession B \u88ab\u963b\u585e\u3002<\/p>\n<h3>flush \u963b\u585e<\/h3>\n<p>flush \u64cd\u4f5c\u4e00\u822c\u6765\u8bf4\u5f88\u5feb\u5c31\u80fd\u6267\u884c\u5b8c\uff0c\u5f53\u901a\u8fc7\u67e5\u8be2\u8fdb\u7a0b\u72b6\u6001\u540e\uff0c\u770b\u5230\u88ab flush \u64cd\u4f5c\u963b\u585e\uff0c\u4e00\u822c\u90fd\u662f\u7531\u5176\u4ed6\u8bed\u53e5\u5f15\u8d77\u7684\u3002<\/p>\n<p>\u5982\u4e0b\u9762\u4e8b\u52a1\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: left;\">Session A<\/th>\n<th style=\"text-align: left;\">Session B<\/th>\n<th style=\"text-align: left;\">Session C<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: left;\">select sleep(1) from t;<\/td>\n<td style=\"text-align: left;\"><\/td>\n<td style=\"text-align: left;\"><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><\/td>\n<td style=\"text-align: left;\">flush tables t;<\/td>\n<td style=\"text-align: left;\"><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><\/td>\n<td style=\"text-align: left;\"><\/td>\n<td style=\"text-align: left;\">select * from t where id =1;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Session A \u5728\u626b\u63cf\u6bcf\u4e00\u884c\u65f6\u4f1a\u4f11\u77201s\uff0c\u800c Session B \u7684<code>flush tables t;<\/code>\u9700\u8981\u5173\u95ed\u8868 t\uff0c\u8981\u7b49 Session A \u7ed3\u675f\u3002\u540e\u9762\u7684 Session C \u88ab Session B \u963b\u585e\u4e86\u3002<\/p>\n<p>flush \u7684\u64cd\u4f5c\u793a\u8303\uff1a<\/p>\n<pre><code class=\"language-sql\">#  flush \u8868 t\nflush tables t with read lock;\n# flush \u6240\u6709\u8868\nflush tables with read lock;<\/code><\/pre>\n<p>flush \u7684\u4f5c\u7528\u5728 <a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/www.appblog.cn\/2021\/07\/23\/\u6570\u636e\u5e93\u5e76\u53d1\u5904\u7406%20-%20MySQL\u9501\u7b80\u4ecb\/#\u5168\u5c40\u9501\" title=\"\u5168\u5c40\u9501\">\u5168\u5c40\u9501<\/a> \u6587\u7ae0\u4e2d\u5df2\u7ecf\u4ecb\u7ecd\u8fc7\uff0cFTWRL \u4e3b\u8981\u7528\u4e8e MyISAM \u8fd9\u6837\u4e0d\u652f\u6301\u4e8b\u52a1\u7684\u5f15\u64ce\uff0c\u4fdd\u8bc1\u5728\u5907\u4efd\u65f6\u89c6\u56fe\u6570\u636e\u4e00\u81f4\u6027\u3002<\/p>\n<h3>\u884c\u9501<\/h3>\n<p>\u8fd9\u91cc\u7684\u884c\u9501\uff0c\u7528\u4e24\u9636\u6bb5\u9501\u6765\u4f53\u73b0\u3002\u5176\u4ed6\u5982\u95f4\u6b47\u9501\u548c next-key \u9501\u90fd\u4f1a\u53ef\u4ee5\u9020\u6210\u8fd9\u6837\u7684\u73b0\u8c61\u3002<\/p>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: left;\">Session A<\/th>\n<th style=\"text-align: left;\">Session B<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: left;\">begin;<\/td>\n<td style=\"text-align: left;\"><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">update t set c=c+1 where id=1<\/td>\n<td style=\"text-align: left;\"><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><\/td>\n<td style=\"text-align: left;\">select * from t where id=1 lock in share mode;<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Session A \u62e5\u6709 id=1 \u8fd9\u884c\u7684\u5199\u9501\uff0cSession B \u60f3\u8981\u62e5\u6709\u8fd9\u4e00\u884c\u7684\u8bfb\u9501\uff0c\u8bfb\u5199\u9501\u4e92\u65a5\u3002<\/p>\n<h3>\u89e3\u51b3\u65b9\u6cd5<\/h3>\n<p>\u9996\u5148\u901a\u8fc7<code>show processlist;<\/code>\u547d\u4ee4\u67e5\u8be2\u88ab\u963b\u585e\u7684\u72b6\u6001\u4fe1\u606f\u3002\u5982\u9700\u8fdb\u4e00\u6b65\u5206\u6790\u7684\u8bdd\uff0c\u53ef\u4ee5\u5c06<code>performance_schema=on<\/code>\u6253\u5f00\uff0c\u901a\u8fc7\u67e5\u8be2<code>select blocking_pid from sys.schema_table_lock_waits;<\/code>\u5177\u4f53\u9020\u6210\u9501\u7b49\u5f85\u7684\u539f\u56e0\u3002\u7136\u540e kill\u6389\u76f8\u5e94\u7684 session<\/p>\n<blockquote>\n<p>\u5728\u6253\u5f00<code>performance_schema<\/code>\u4f1a\u6709\u4e00\u5b9a\u7684\u6027\u80fd\u635f\u5931\u3002<\/p>\n<\/blockquote>\n<h2>\u67e5\u8be2\u786e\u5b9e\u6162<\/h2>\n<h3>\u6ca1\u6709\u8bbe\u7f6e\u5408\u9002\u7684\u7d22\u5f15<\/h3>\n<p>\u5982\u679c\u6ca1\u6709\u8bbe\u7f6e\u5408\u9002\u7684\u7d22\u5f15\uff0c\u5bfc\u81f4\u626b\u63cf\u884c\u6570\u8fc7\u591a\uff0c\u65f6\u95f4\u81ea\u7136\u5c31\u6162\u4e86\u3002\u5bf9\u4e8e\u8fd9\u79cd\u60c5\u51b5\uff0c\u53ef\u4ee5\u5f00\u542f\u6162\u67e5\u8be2\u65e5\u5fd7\uff0c\u67e5\u770b\u8bed\u53e5\u7684\u6267\u884c\u8fc7\u7a0b\uff0c\u7136\u540e\u8fdb\u884c\u5206\u6790\u3002<\/p>\n<p>\u9ed8\u8ba4\u60c5\u51b5\uff0c\u6162\u67e5\u8be2\u65e5\u5fd7\u65f6\u5173\u95ed\u7684\uff0c\u6253\u5f00\u65b9\u5f0f\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-sql\"># \u67e5\u8be2\u6162\u67e5\u8be2\u65e5\u5fd7\u72b6\u6001\u53ca\u5b58\u50a8\u4f4d\u7f6e\n# show variables  like &#039;%slow_query_log%&#039;;\n\n# \u67e5\u8be2\u6162\u67e5\u8be2\u65e5\u5fd7\u7684\u8bbe\u7f6e\u65f6\u95f4\nshow variables  like &#039;%long_query%&#039;;\n\n# \u4e34\u65f6\u6253\u5f00\u6162\u67e5\u8be2\u65e5\u5fd7\uff0cMySQL \u91cd\u542f\u540e\u5931\u6548\nset global slow_query_log=1;\n\n# \u6539\u53d8\u65f6\u95f4\nset long_query_time=0;<\/code><\/pre>\n<h3>\u4e8b\u52a1\u9694\u79bb\u7684\u5f71\u54cd<\/h3>\n<p>\u5728 <a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/www.appblog.cn\/2021\/07\/22\/\u4e8b\u52a1\u7a76\u7adf\u6709\u6ca1\u6709\u88ab\u9694\u79bb\/\" title=\"\u4e8b\u52a1\u7a76\u7adf\u6709\u6ca1\u6709\u88ab\u9694\u79bb\">\u4e8b\u52a1\u7a76\u7adf\u6709\u6ca1\u6709\u88ab\u9694\u79bb<\/a> \u8fd9\u7bc7\u4e2d\uff0c\u6211\u4eec\u77e5\u9053\u8868\u4e2d\u7684\u6bcf\u884c\u6570\u636e\u90fd\u6709\u591a\u4e2a\u7248\u672c\uff0c\u5728\u4e00\u81f4\u6027\u89c6\u56fe\u5f00\u542f\u540e\uff0c\u89c6\u56fe\u7684\u4e00\u81f4\u6027\u8bfb\u7684\u7ed3\u679c\u5c31\u662f\u901a\u8fc7\u548c\u6570\u636e\u884c\u7684\u7248\u672c\u6bd4\u8f83\u8fdb\u800c\u663e\u793a\u7684\u7ed3\u679c\u3002<\/p>\n<table>\n<thead>\n<tr>\n<th style=\"text-align: left;\">Session A<\/th>\n<th style=\"text-align: left;\">Session B<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td style=\"text-align: left;\">start transaction with consistent snapshot;<\/td>\n<td style=\"text-align: left;\"><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">update t set c=c+1 where id=1;<\/td>\n<td style=\"text-align: left;\"><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\"><\/td>\n<td style=\"text-align: left;\">update set c=c+1 where id=1; \/\/ 100 \u4e07\u6b21<\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">select * from t where id=1;<\/td>\n<td style=\"text-align: left;\"><\/td>\n<\/tr>\n<tr>\n<td style=\"text-align: left;\">select * from t where id=1 lock in share mode;<\/td>\n<td style=\"text-align: left;\"><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u8fd9\u65f6\u7b2c\u4e00\u4e2a select \u8bed\u53e5\u5c31\u4f1a\u6bd4\u7b2c\u4e8c\u4e2a\u52a0\u9501\u7684 select \u8bed\u53e5\u8fd8\u8981\u6162\u3002<\/p>\n<p>\u56e0\u4e3a\u7b2c\u4e00\u4e2a select \u8bed\u53e5\u662f\u4e00\u81f4\u6027\u8bfb\uff0c\u9700\u8981\u4ece 100 \u4e07\u6761\u56de\u6eda\u65e5\u5fd7\u4e2d\u6bd4\u8f83\u76f4\u5230\u627e\u5230\u9002\u5408\u7684\u7248\u672c\u3002<\/p>\n<p>\u800c\u7b2c\u4e8c\u4e2a select \u8bed\u53e5\u662f\u5f53\u524d\u8bfb\uff0c\u76f4\u63a5\u8bfb\u53d6\u6700\u65b0\u7248\u672c\u5c31\u53ef\u4ee5\u4e86\u3002\u6240\u4ee5\u82b1\u8d39\u7684\u65f6\u95f4\u4e0d\u4e00\u6837\u3002<\/p>\n<h2>\u603b\u7ed3<\/h2>\n<p>\u9020\u6210\u67e5\u8be2\u5c0f\u6570\u636e\u91cf\uff0c\u5374\u5f88\u7f13\u6162\u7684\u539f\u56e0\u4e00\u822c\u6709\u4e24\u79cd\uff0c\u7b2c\u4e00\u79cd\u53ef\u80fd\u662f\u6240\u67e5\u6570\u636e\u88ab\u9501\u4f4f\u3002\u53e6\u4e00\u79cd\u786e\u5b9e\u662f\u67e5\u627e\u8fc7\u7a0b\u662f\u771f\u7684\u5f88\u6162\u3002<\/p>\n<p>\u5bf9\u4e8e\u6570\u636e\u88ab\u9501\u4f4f\u7684\u60c5\u51b5\uff0c\u4e00\u822c\u4f1a\u7531 MDL \u9501\uff0cFLUSH \u64cd\u4f5c\u88ab\u963b\u585e\uff0c\u884c\u9501\u9020\u6210\u3002<\/p>\n<p>\u5bf9\u4e8e\u67e5\u8be2\u786e\u5b9e\u5f88\u6162\u6765\u8bf4\uff0c\u8003\u8651\u4e0b\u7d22\u5f15\u662f\u5426\u8bbe\u7f6e\u7684\u5408\u9002\u3002\u5e76\u6ce8\u610f\u5728 RR \u7ea7\u522b\u4e0b\uff0c\u662f\u5426\u7531\u4e8e\u4e00\u81f4\u6027\u8bfb\u548c\u5f53\u524d\u8bfb\u7684\u4e0d\u540c\u800c\u9020\u6210\u67e5\u8be2\u901f\u5ea6\u4e0d\u4e00\u81f4\u7684\u60c5\u51b5\u3002<\/p>\n<p>\u5728\u5206\u6790\u539f\u56e0\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u8fdb\u7a0b\u72b6\u6001\u4ee5\u53ca<code>sys.innodb_lock_waits<\/code>\u4e2d\u7684\u4fe1\u606f\uff0c\u6765\u505a\u51fa\u76f8\u5e94\u7684\u5904\u7406\u3002<\/p>\n<h2>\u53c2\u8003<\/h2>\n<ul>\n<li><a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/dev.mysql.com\/doc\/refman\/5.7\/en\/flush.html\">https:\/\/dev.mysql.com\/doc\/refman\/5.7\/en\/flush.html<\/a><\/li>\n<li><a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/dev.mysql.com\/doc\/internals\/en\/flush-tables.html\">https:\/\/dev.mysql.com\/doc\/internals\/en\/flush-tables.html<\/a><\/li>\n<\/ul>\n<blockquote>\n<p>\u8f6c\u8f7d\u81f3\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/www.cnblogs.com\/michael9\/p\/12330392.html\">https:\/\/www.cnblogs.com\/michael9\/p\/12330392.html<\/a><\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u8bb0\u5f55\uff0c\u9020\u6210\u67e5\u8be2\u6570\u91cf\u4e0d\u5927\u7684\u60c5\u51b5\u4e0b\uff0c\u9020\u6210\u67e5\u8be2\u7f13\u6162\u7684\u539f\u56e0\uff0c\u4ee5\u53ca\u76f8\u5e94\u7684\u89e3\u51b3\u65b9\u6cd5\u3002 \u73af\u5883\u8bf4\u660e\uff1a MySQL  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[],"class_list":["post-1078","post","type-post","status-publish","format-standard","hentry","category-mysql"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1078","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1078"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1078\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1078"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1078"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1078"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}