{"id":3831,"date":"2025-07-06T14:39:10","date_gmt":"2025-07-06T06:39:10","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=3831"},"modified":"2025-07-06T14:39:10","modified_gmt":"2025-07-06T06:39:10","slug":"%e5%9c%a8-mysql-%e4%b8%ad%e8%bf%9b%e8%a1%8c%e6%9f%a5%e8%af%a2%e8%b0%83%e8%af%95%e6%88%96%e8%bf%87%e7%a8%8b%e5%bc%80%e5%8f%91%e6%97%b6%e7%9a%84%e5%b8%b8%e8%a7%81%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/07\/06\/%e5%9c%a8-mysql-%e4%b8%ad%e8%bf%9b%e8%a1%8c%e6%9f%a5%e8%af%a2%e8%b0%83%e8%af%95%e6%88%96%e8%bf%87%e7%a8%8b%e5%bc%80%e5%8f%91%e6%97%b6%e7%9a%84%e5%b8%b8%e8%a7%81%e9%97%ae%e9%a2%98\/","title":{"rendered":"\u5728 MySQL \u4e2d\u8fdb\u884c\u67e5\u8be2\u8c03\u8bd5\u6216\u8fc7\u7a0b\u5f00\u53d1\u65f6\u7684\u5e38\u89c1\u95ee\u9898"},"content":{"rendered":"\n<p>\u5728 MySQL \u4e2d\u8fdb\u884c\u67e5\u8be2\u8c03\u8bd5\u6216\u8fc7\u7a0b\uff08\u5982\u5b58\u50a8\u8fc7\u7a0b\u3001\u51fd\u6570\u3001\u89e6\u53d1\u5668\uff09\u5f00\u53d1\u65f6\uff0c\u5e38\u89c1\u95ee\u9898\u901a\u5e38\u5305\u62ec\u8bed\u6cd5\u9519\u8bef\u3001\u8fd0\u884c\u65f6\u9519\u8bef\uff08\u5982\u8868\u4e0d\u5b58\u5728\u3001\u53d8\u91cf\u672a\u58f0\u660e\u3001\u903b\u8f91\u9519\u8bef\uff09\uff0c\u800c MySQL <strong>\u539f\u751f\u4e0d\u652f\u6301\u4f20\u7edf\u7f16\u7a0b\u8bed\u8a00\u4e2d\u7684\u8c03\u8bd5\u5668<\/strong>\uff0c\u4f46\u6211\u4eec\u53ef\u4ee5\u501f\u52a9\u4ee5\u4e0b<strong>\u5b9e\u7528\u65b9\u6cd5\u3001\u6280\u5de7\u4e0e\u5de5\u5177<\/strong>\u6765\u5b8c\u6210\u8c03\u8bd5\uff1a<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e00\u3001\u8c03\u8bd5 MySQL \u67e5\u8be2\u8bed\u53e5\uff08SQL SELECT\/UPDATE \u7b49\uff09<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u4f7f\u7528 <code>EXPLAIN<\/code> \u5206\u6790\u67e5\u8be2\u8ba1\u5212<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>EXPLAIN SELECT * FROM users WHERE age &gt; 30;<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u53ef\u67e5\u770b\u662f\u5426\u4f7f\u7528\u7d22\u5f15\uff08<code>key<\/code> \u5217\uff09<\/li>\n\n\n\n<li><code>rows<\/code> \u9884\u4f30\u626b\u63cf\u884c\u6570<\/li>\n\n\n\n<li><code>Extra<\/code> \u63d0\u793a\u5982 &#8220;Using where&#8221;, &#8220;Using temporary&#8221; \u7b49<\/li>\n<\/ul>\n\n\n\n<p>\ud83d\udc49 <strong>\u4f18\u5316\u7528\u9014<\/strong>\uff1a\u5224\u65ad\u67e5\u8be2\u662f\u5426\u8d70\u4e86\u5168\u8868\u626b\u63cf\u3001\u6392\u5e8f\u3001\u4e34\u65f6\u8868\u7b49\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u4f7f\u7528 <code>SHOW WARNINGS<\/code> \u67e5\u770b\u6700\u8fd1\u8bed\u6cd5\u8b66\u544a<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT * FROM user WHERE name = NULL;\nSHOW WARNINGS;<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>SQL \u4e2d <code>name = NULL<\/code> \u4f1a\u5931\u8d25\uff0c\u5e94\u4f7f\u7528 <code>IS NULL<\/code><\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">3. \u7528 <code>SELECT<\/code> \u6d4b\u8bd5\u8868\u8fbe\u5f0f\u903b\u8f91<\/h3>\n\n\n\n<p>\u6d4b\u8bd5\u53d8\u91cf\u503c\u3001\u8868\u8fbe\u5f0f\u903b\u8f91\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT NOW(), IF(1=1, 'YES', 'NO') AS result;<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">4. \u4f7f\u7528 <code>SQL_CALC_FOUND_ROWS<\/code> \u83b7\u53d6\u53d7\u5f71\u54cd\u884c\u6570<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT SQL_CALC_FOUND_ROWS * FROM users LIMIT 10;\nSELECT FOUND_ROWS();  -- \u5f97\u5230\u672aLIMIT\u524d\u7684\u603b\u884c\u6570<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e8c\u3001\u8c03\u8bd5\u5b58\u50a8\u8fc7\u7a0b\uff08Stored Procedure\uff09\u548c\u51fd\u6570<\/h2>\n\n\n\n<p>MySQL \u4e0d\u652f\u6301 <code>PRINT<\/code>\u3001<code>DEBUGGER<\/code>\uff0c\u6211\u4eec\u53ef\u4f7f\u7528 <strong>\u53d8\u91cf + \u4e34\u65f6\u8868 + \u9519\u8bef\u5904\u7406\u673a\u5236<\/strong> \u8f85\u52a9\u8c03\u8bd5\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">1. \u4f7f\u7528\u4e34\u65f6\u8868\u8f93\u51fa\u201c\u65e5\u5fd7\u201d<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE TEMPORARY TABLE debug_log (\n  id INT AUTO_INCREMENT PRIMARY KEY,\n  msg TEXT,\n  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\n-- \u5728\u8fc7\u7a0b\u4e2d\u63d2\u5165\u8c03\u8bd5\u4fe1\u606f\nINSERT INTO debug_log(msg) VALUES (CONCAT('step1 value: ', my_var));<\/code><\/pre>\n\n\n\n<p>\ud83d\udc49 \u9002\u7528\u4e8e\u8fc7\u7a0b\u8c03\u8bd5\u4e2d\u67e5\u770b\u53d8\u91cf\u7684\u4e2d\u95f4\u72b6\u6001\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">2. \u4f7f\u7528 <code>DECLARE HANDLER<\/code> \u6355\u83b7\u8fd0\u884c\u65f6\u9519\u8bef<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>DECLARE CONTINUE HANDLER FOR SQLEXCEPTION\n  BEGIN\n    INSERT INTO debug_log(msg) VALUES ('Runtime SQL Error occurred');\n  END;<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u7c7b\u578b<\/th><th>\u8bf4\u660e<\/th><\/tr><\/thead><tbody><tr><td><code>SQLEXCEPTION<\/code><\/td><td>\u6355\u83b7 SQL \u6267\u884c\u9519\u8bef\uff08\u5982\u8bed\u6cd5\uff09<\/td><\/tr><tr><td><code>SQLWARNING<\/code><\/td><td>\u6355\u83b7\u8b66\u544a<\/td><\/tr><tr><td><code>NOT FOUND<\/code><\/td><td>\u6355\u83b7\u672a\u627e\u5230\uff08\u6e38\u6807\u5230\u7ed3\u5c3e\uff09<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">3. \u7528 <code>SELECT<\/code> \u8f93\u51fa\u4e2d\u95f4\u53d8\u91cf\u503c\uff08\u8c03\u8bd5\u65f6\u5728 CLI \u6216\u5b58\u50a8\u51fd\u6570\u4e2d\uff09<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT 'Checkpoint1', var_a, var_b;<\/code><\/pre>\n\n\n\n<p>\u5982\u679c\u4f60\u5728 <code>mysql<\/code> \u547d\u4ee4\u884c\u8fd0\u884c\uff0c\u53ef\u4ee5\u7acb\u5373\u770b\u5230\u8f93\u51fa\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">4. \u8bed\u6cd5\u6821\u9a8c\u7528 <code>SHOW CREATE PROCEDURE<\/code><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>SHOW CREATE PROCEDURE your_proc_name\\G<\/code><\/pre>\n\n\n\n<p>\u7528\u4e8e\u786e\u8ba4\u8fc7\u7a0b\u662f\u5426\u5df2\u521b\u5efa\uff0c\u662f\u5426\u8bed\u6cd5\u6b63\u786e\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">5. \u4f7f\u7528 <code>mysql.error_log<\/code> \u6216 <code>general_log<\/code><\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>SET GLOBAL general_log = 'ON';\nSET GLOBAL log_output = 'TABLE';<\/code><\/pre>\n\n\n\n<p>\u67e5\u770b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT * FROM mysql.general_log ORDER BY event_time DESC LIMIT 10;<\/code><\/pre>\n\n\n\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u26a0\ufe0f <strong>\u6ce8\u610f\uff1a\u6027\u80fd\u6d88\u8017\u8f83\u5927\uff0c\u8c03\u8bd5\u540e\u5173\u95ed\uff1a<\/strong><\/p>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code\"><code>SET GLOBAL general_log = 'OFF';<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e09\u3001\u8c03\u8bd5\u89e6\u53d1\u5668\uff08TRIGGER\uff09<\/h2>\n\n\n\n<p>\u89e6\u53d1\u5668\u4e5f\u4e0d\u80fd <code>SELECT<\/code>\u3001<code>PRINT<\/code>\uff0c\u53ef\u7528<strong>\u4e34\u65f6\u65e5\u5fd7\u8868\u8c03\u8bd5<\/strong>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CREATE TABLE trigger_log (\n  id INT AUTO_INCREMENT PRIMARY KEY,\n  msg TEXT,\n  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP\n);\n\n-- \u5728\u89e6\u53d1\u5668\u4e2d\u8bb0\u5f55\u65e5\u5fd7\nINSERT INTO trigger_log(msg) VALUES (CONCAT('BEFORE INSERT: ', NEW.id));<\/code><\/pre>\n\n\n\n<p>\u26a0\ufe0f \u4e0d\u652f\u6301\u4f7f\u7528\u4e8b\u52a1\u56de\u6eda\u540e\u5199\u5165\u6301\u4e45\u65e5\u5fd7\uff08\u56e0\u4e3a\u65e5\u5fd7\u4e5f\u4f1a\u88ab\u56de\u6eda\uff09\uff0c\u5982\u9700\u53ef\u7528 <strong>\u5916\u90e8\u65e5\u5fd7\u8868 + <code>autocommit<\/code>=1<\/strong>\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u56db\u3001\u5e38\u89c1\u8c03\u8bd5\u9519\u8bef\u7c7b\u578b\u548c\u8bf4\u660e<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u9519\u8bef\u4ee3\u7801<\/th><th>\u793a\u4f8b<\/th><th>\u539f\u56e0<\/th><\/tr><\/thead><tbody><tr><td>1064<\/td><td><code>You have an error in your SQL syntax<\/code><\/td><td>SQL\u8bed\u6cd5\u9519\u8bef\uff08\u5982\u6f0f\u5206\u53f7\u3001\u9519\u62fc\u5199\uff09<\/td><\/tr><tr><td>1054<\/td><td><code>Unknown column 'foo' in field list<\/code><\/td><td>\u5b57\u6bb5\u4e0d\u5b58\u5728<\/td><\/tr><tr><td>1329<\/td><td><code>No data - zero rows fetched<\/code><\/td><td>\u6e38\u6807\u6ca1\u6709\u6570\u636e<\/td><\/tr><tr><td>1172<\/td><td><code>Result consisted of more than one row<\/code><\/td><td>\u5b50\u67e5\u8be2\u8fd4\u56de\u591a\u884c\uff0c\u4f46\u53ea\u652f\u6301\u4e00\u884c<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u4e94\u3001\u63a8\u8350\u5de5\u5177\u4e0e\u6269\u5c55<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">MySQL Workbench\uff08\u5b98\u65b9\u56fe\u5f62\u5316\u5de5\u5177\uff09<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u53ef\u9010\u6b65\u8fd0\u884c\u67e5\u8be2<\/li>\n\n\n\n<li>\u53ef\u67e5\u770b <code>stored procedure<\/code> \u548c\u6267\u884c\u7ed3\u679c<\/li>\n\n\n\n<li>\u53ef\u914d\u5408 <code>general_log<\/code> \u548c <code>performance_schema<\/code><\/li>\n<\/ul>\n\n\n\n<p>\ud83d\udd17 <a href=\"https:\/\/www.mysql.com\/products\/workbench\/\">https:\/\/www.mysql.com\/products\/workbench\/<\/a><\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u516d\u3001\u5feb\u901f\u8c03\u8bd5\u6a21\u677f\uff08\u5b58\u50a8\u8fc7\u7a0b\uff09<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>DELIMITER \/\/\n\nCREATE PROCEDURE test_proc()\nBEGIN\n  DECLARE v_user_count INT DEFAULT 0;\n\n  DECLARE CONTINUE HANDLER FOR SQLEXCEPTION\n  BEGIN\n    INSERT INTO debug_log(msg) VALUES ('ERROR in test_proc');\n  END;\n\n  SELECT COUNT(*) INTO v_user_count FROM users;\n  INSERT INTO debug_log(msg) VALUES (CONCAT('User count is ', v_user_count));\nEND \/\/\n\nDELIMITER ;\n\nCALL test_proc();\nSELECT * FROM debug_log;<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u603b\u7ed3\uff1aMySQL \u4e2d\u8c03\u8bd5\u7684 6 \u5927\u65b9\u6cd5<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u65b9\u6cd5<\/th><th>\u8bf4\u660e<\/th><\/tr><\/thead><tbody><tr><td><code>EXPLAIN<\/code><\/td><td>\u67e5\u8be2\u8ba1\u5212\u5206\u6790\uff0c\u8c03\u8bd5\u6162\u67e5\u8be2<\/td><\/tr><tr><td><code>SHOW WARNINGS<\/code><\/td><td>\u67e5\u770b\u8b66\u544a\u4e0e\u9519\u8bef\u4fe1\u606f<\/td><\/tr><tr><td><code>DECLARE HANDLER<\/code><\/td><td>\u6355\u83b7\u5f02\u5e38<\/td><\/tr><tr><td><code>debug_log<\/code> \u8868<\/td><td>\u8bb0\u5f55\u53d8\u91cf\u503c\u4e0e\u8fc7\u7a0b\u65e5\u5fd7<\/td><\/tr><tr><td><code>general_log<\/code><\/td><td>\u8bb0\u5f55\u6240\u6709 SQL \u8bf7\u6c42\uff08\u9700\u5173\u95ed\uff09<\/td><\/tr><tr><td>MySQL Workbench \u5de5\u5177<\/td><td>\u56fe\u5f62\u5316\u8f85\u52a9\uff0c\u4fbf\u4e8e\u8c03\u8bd5\u51fd\u6570\/\u8fc7\u7a0b\u7b49<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\ud83d\udcda \u5b98\u65b9\u6587\u6863\u53c2\u8003<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/declare-handler.html\">MySQL 8.0 &#8211; DECLARE HANDLER<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/show-warnings.html\">MySQL &#8211; SHOW WARNINGS<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/dev.mysql.com\/doc\/refman\/8.0\/en\/server-system-variables.html#sysvar_general_log\">MySQL &#8211; general_log \u914d\u7f6e<\/a><\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u66f4\u591a\u8be6\u7ec6\u5185\u5bb9\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 MySQL \u4e2d\u8fdb\u884c\u67e5\u8be2\u8c03\u8bd5\u6216\u8fc7\u7a0b\uff08\u5982\u5b58\u50a8\u8fc7\u7a0b\u3001\u51fd\u6570\u3001\u89e6\u53d1\u5668\uff09\u5f00\u53d1\u65f6\uff0c\u5e38\u89c1\u95ee\u9898\u901a\u5e38\u5305\u62ec\u8bed\u6cd5\u9519\u8bef\u3001\u8fd0\u884c\u65f6\u9519\u8bef\uff08 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3832,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[34],"tags":[],"class_list":["post-3831","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-34"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3831","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=3831"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3831\/revisions"}],"predecessor-version":[{"id":3833,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/3831\/revisions\/3833"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media\/3832"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=3831"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=3831"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=3831"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}