3.3 long_query_time<\/strong><\/span><\/div>\n\u5f00\u542f\u4e86\u6162\u67e5\u8be2\u65e5\u5fd7\u540e\uff0c\u4ec0\u4e48\u6837\u7684SQL\u624d\u4f1a\u8bb0\u5f55\u5230\u6162\u67e5\u8be2\u65e5\u5fd7\u91cc\u9762\u5462\uff1f<\/p>\n
\u8fd9\u4e2a\u662f\u7531\u53c2\u6570long_query_time\u63a7\u5236\uff0c\u9ed8\u8ba4\u60c5\u51b5\u4e0blong_query_time\u7684\u503c\u4e3a10\u79d2\uff0c\u53ef\u4ee5\u4f7f\u7528\u547d\u4ee4\u4fee\u6539\uff0c\u4e5f\u53ef\u4ee5\u5728my.cnf\u53c2\u6570\u91cc\u9762\u4fee\u6539\u3002<\/p>\n
\u5173\u4e8e\u8fd0\u884c\u65f6\u95f4\u6b63\u597d\u7b49\u4e8elong_query_time\u7684\u60c5\u51b5\uff0c\u5e76\u4e0d\u4f1a\u88ab\u8bb0\u5f55\u4e0b\u6765\u3002<\/p>\n
\u4e5f\u5c31\u662f\u8bf4\uff0c\u5728mysql\u6e90\u7801\u91cc\u662f\u5224\u65ad\u5927\u4e8elong_query_time\uff0c\u800c\u975e\u5927\u4e8e\u7b49\u4e8e\u3002<\/p>\n
\u4eceMySQL 5.1\u5f00\u59cb\uff0clong_query_time\u5f00\u59cb\u4ee5\u5fae\u79d2\u8bb0\u5f55SQL\u8bed\u53e5\u8fd0\u884c\u65f6\u95f4\uff0c\u4e4b\u524d\u4ec5\u7528\u79d2\u4e3a\u5355\u4f4d\u8bb0\u5f55\u3002<\/p>\n
\u5982\u679c\u8bb0\u5f55\u5230\u8868\u91cc\u9762\uff0c\u53ea\u4f1a\u8bb0\u5f55\u6574\u6570\u90e8\u5206\uff0c\u4e0d\u4f1a\u8bb0\u5f55\u5fae\u79d2\u90e8\u5206\u3002<\/p>\n
\r\nmysql> show variables like 'long_query_time%';\r\n +-----------------+-----------+\r\n | Variable_name | Value |\r\n +-----------------+-----------+\r\n | long_query_time | 10.000000 |\r\n +-----------------+-----------+\r\n 1 row in set (0.00 sec)\r\n \r\nmysql> set global long_query_time=4;\r\n Query OK, 0 rows affected (0.00 sec)\r\n \r\nmysql> show variables like 'long_query_time';\r\n +-----------------+-----------+\r\n | Variable_name | Value |\r\n +-----------------+-----------+\r\n | long_query_time | 10.000000 |\r\n +-----------------+-----------+\r\n 1 row in set (0.00 sec)\r\n<\/pre>\n\u5982\u4e0a\u6240\u793a\uff0c\u6211\u4fee\u6539\u4e86\u53d8\u91cflong_query_time\uff0c\u4f46\u662f\u67e5\u8be2\u53d8\u91cflong_query_time\u7684\u503c\u8fd8\u662f10\uff0c\u96be\u9053\u6ca1\u6709\u4fee\u6539\u5230\u5462\uff1f<\/p>\n
\u6ce8\u610f\uff1a\u4f7f\u7528\u547d\u4ee4 set global long_query_time=4<\/code>\u4fee\u6539\u540e\uff0c\u9700\u8981\u91cd\u65b0\u8fde\u63a5\u6216\u65b0\u5f00\u4e00\u4e2a\u4f1a\u8bdd\u624d\u80fd\u770b\u5230\u4fee\u6539\u503c\u3002<\/p>\n\u7528show variables like 'long_query_time' <\/code>\u67e5\u770b\u662f\u5f53\u524d\u4f1a\u8bdd\u7684\u53d8\u91cf\u503c\u3002<\/p>\n\u4e5f\u53ef\u4ee5\u4e0d\u7528\u91cd\u65b0\u8fde\u63a5\u4f1a\u8bdd\uff0c\u800c\u662f\u7528 show global variables like 'long_query_time';<\/code>\u3002<\/p>\n3.4 log_output<\/strong><\/span><\/div>\nlog_output\u53c2\u6570\u6307\u5b9a\u65e5\u5fd7\u7684\u5b58\u50a8\u65b9\u5f0f\u3002<\/p>\n
log_output='FILE'\u8868\u793a\u5c06\u65e5\u5fd7\u5b58\u5165\u6587\u4ef6\uff0c\u9ed8\u8ba4\u503c\u4e5f\u662f'FILE'\u3002<\/p>\n
log_output='TABLE'\u8868\u793a\u5c06\u65e5\u5fd7\u5b58\u5165\u6570\u636e\u5e93\uff0c\u8fd9\u6837\u65e5\u5fd7\u4fe1\u606f\u5c31\u4f1a\u88ab\u5199\u5165\u5230mysql.slow_log\u8868\u4e2d\u3002<\/p>\n
\u540c\u65f6\u4e5f\u652f\u6301\u4e24\u79cd\u65e5\u5fd7\u5b58\u50a8\u65b9\u5f0f\uff0c\u914d\u7f6e\u7684\u65f6\u5019\u4ee5\u9017\u53f7\u9694\u5f00\u5373\u53ef\uff0c\u5982\uff1alog_output='FILE,TABLE'\u3002<\/p>\n
\u65e5\u5fd7\u8bb0\u5f55\u5230\u7cfb\u7edf\u7684\u4e13\u7528\u65e5\u5fd7\u8868\u4e2d\uff0c\u8981\u6bd4\u8bb0\u5f55\u5230\u6587\u4ef6\u8017\u8d39\u66f4\u591a\u7684\u7cfb\u7edf\u8d44\u6e90\u3002<\/p>\n
\u56e0\u6b64\u5bf9\u4e8e\u9700\u8981\u542f\u7528\u6162\u67e5\u8be2\u65e5\u5fd7\uff0c\u53c8\u9700\u8981\u80fd\u591f\u83b7\u5f97\u66f4\u9ad8\u7684\u7cfb\u7edf\u6027\u80fd\uff0c\u90a3\u4e48\u5efa\u8bae\u4f18\u5148\u8bb0\u5f55\u5230\u6587\u4ef6\u3002<\/p>\n
\r\nmysql> show variables like '%log_output%';\r\n +---------------+-------+\r\n | Variable_name | Value |\r\n +---------------+-------+\r\n | log_output | FILE |\r\n +---------------+-------+\r\n 1 row in set (0.00 sec)\r\n \r\nmysql> set global log_output='TABLE';\r\n Query OK, 0 rows affected (0.00 sec)\r\n \r\nmysql> show variables like '%log_output%';\r\n +---------------+-------+\r\n | Variable_name | Value |\r\n +---------------+-------+\r\n | log_output | TABLE |\r\n +---------------+-------+\r\n 1 row in set (0.00 sec)\r\n \r\nmysql> select sleep(5) ;\r\n +----------+\r\n | sleep(5) |\r\n +----------+\r\n | 0 |\r\n +----------+\r\n 1 row in set (5.00 sec)\r\n \r\nmysql>\r\n \r\nmysql> select * from mysql.slow_log;\r\n +---------------------+---------------------------+------------+-----------+-----------+---------------+----+----------------+-----------+-----------+-----------------+-----------+\r\n | start_time | user_host | query_time | lock_time | rows_sent | rows_examined | db | last_insert_id | insert_id | server_id | sql_text | thread_id |\r\n +---------------------+---------------------------+------------+-----------+-----------+---------------+----+----------------+-----------+-----------+-----------------+-----------+\r\n | 2016-06-16 17:37:53 | root[root] @ localhost [] | 00:00:03 | 00:00:00 | 1 | 0 | | 0 | 0 | 1 | select sleep(3) | 5 |\r\n | 2016-06-16 21:45:23 | root[root] @ localhost [] | 00:00:05 | 00:00:00 | 1 | 0 | | 0 | 0 | 1 | select sleep(5) | 2 |\r\n +---------------------+---------------------------+------------+-----------+-----------+---------------+----+----------------+-----------+-----------+-----------------+-----------+\r\n 2 rows in set (0.00 sec)\r\n<\/pre>\n3.5 log-queries-not-using-indexes<\/strong><\/span><\/div>\n\u8be5\u7cfb\u7edf\u53d8\u91cf\u6307\u5b9a\u672a\u4f7f\u7528\u7d22\u5f15\u7684\u67e5\u8be2\u4e5f\u88ab\u8bb0\u5f55\u5230\u6162\u67e5\u8be2\u65e5\u5fd7\u4e2d\uff08\u53ef\u9009\u9879\uff09\u3002<\/p>\n
\u5982\u679c\u8c03\u4f18\u7684\u8bdd\uff0c\u5efa\u8bae\u5f00\u542f\u8fd9\u4e2a\u9009\u9879\u3002<\/p>\n
\u53e6\u5916\uff0c\u5f00\u542f\u4e86\u8fd9\u4e2a\u53c2\u6570\uff0c\u5176\u5b9e\u4f7f\u7528full index scan\u7684SQL\u4e5f\u4f1a\u88ab\u8bb0\u5f55\u5230\u6162\u67e5\u8be2\u65e5\u5fd7\u3002<\/p>\n
\r\nmysql> show variables like 'log_queries_not_using_indexes';\r\n +-------------------------------+-------+\r\n | Variable_name | Value |\r\n +-------------------------------+-------+\r\n | log_queries_not_using_indexes | OFF |\r\n +-------------------------------+-------+\r\n 1 row in set (0.00 sec)\r\n \r\nmysql> set global log_queries_not_using_indexes=1;\r\n Query OK, 0 rows affected (0.00 sec)\r\n \r\nmysql> show variables like 'log_queries_not_using_indexes';\r\n +-------------------------------+-------+\r\n | Variable_name | Value |\r\n +-------------------------------+-------+\r\n | log_queries_not_using_indexes | ON |\r\n +-------------------------------+-------+\r\n 1 row in set (0.00 sec)\r\n3.6 log_slow_admin_statements\r\n<\/pre>\n\u8fd9\u4e2a\u7cfb\u7edf\u53d8\u91cf\u8868\u793a\uff0c\u662f\u5426\u5c06\u6162\u7ba1\u7406\u8bed\u53e5\u4f8b\u5982ANALYZE TABLE\u548cALTER TABLE\u7b49\u8bb0\u5165\u6162\u67e5\u8be2\u65e5\u5fd7\u3002<\/p>\n
\r\nmysql> show variables like 'log_slow_admin_statements';\r\n +---------------------------+-------+\r\n | Variable_name | Value |\r\n +---------------------------+-------+\r\n | log_slow_admin_statements | OFF |\r\n +---------------------------+-------+\r\n 1 row in set (0.00 sec)\r\n3.7 Slow_queries\r\n<\/pre>\n\u5982\u679c\u4f60\u60f3\u67e5\u8be2\u6709\u591a\u5c11\u6761\u6162\u67e5\u8be2\u8bb0\u5f55\uff0c\u53ef\u4ee5\u4f7f\u7528Slow_queries\u7cfb\u7edf\u53d8\u91cf\u3002<\/p>\n
\r\nmysql> show global status like '%Slow_queries%';\r\n +---------------+-------+\r\n | Variable_name | Value |\r\n +---------------+-------+\r\n | Slow_queries | 2104 |\r\n +---------------+-------+\r\n 1 row in set (0.00 sec)\r\n<\/pre>\n\u53e6\u5916\uff0c\u8fd8\u6709log_slow_slave_statements \u548c --log-short-format \u53c2\u6570\uff0c\u53ef\u5230MySQL\u7f51\u7ad9\u4e86\u89e3\u3002<\/p>\n
4 mysqldumpslow\u5de5\u5177<\/strong><\/div>\n\u5728\u751f\u4ea7\u73af\u5883\u4e2d\uff0c\u5982\u679c\u8981\u624b\u5de5\u5206\u6790\u65e5\u5fd7\uff0c\u67e5\u627e\u3001\u5206\u6790SQL\uff0c\u663e\u7136\u662f\u4e2a\u4f53\u529b\u6d3b\u3002<\/p>\n
MySQL\u63d0\u4f9b\u4e86\u65e5\u5fd7\u5206\u6790\u5de5\u5177mysqldumpslow<\/p>\n
\u67e5\u770bmysqldumpslow\u7684\u5e2e\u52a9\u4fe1\u606f\uff1a<\/p>\n
\r\n[root@DB-Server ~]# mysqldumpslow --help\r\n Usage: mysqldumpslow [ OPTS... ] [ LOGS... ]\r\n \r\nParse and summarize the MySQL slow query log. Options are\r\n \r\n --verbose verbose\r\n --debug debug\r\n --help write this text to standard output\r\n \r\n -v verbose\r\n -d debug\r\n -s ORDER what to sort by (al, at, ar, c, l, r, t), 'at' is default\uff08\u6392\u5e8f\u65b9\u5f0f\uff09\r\n al: average lock time\uff08\u5e73\u5747\u9501\u5b9a\u65f6\u95f4\uff09\r\n ar: average rows sent\uff08\u5e73\u5747\u8fd4\u56de\u8bb0\u5f55\u6570\uff09\r\n at: average query time\uff08\u5e73\u5747\u67e5\u8be2\u65f6\u95f4\uff09\r\n c: count\uff08\u8bbf\u95ee\u8ba1\u6570\uff09\r\n l: lock time\uff08\u9501\u5b9a\u65f6\u95f4\uff09\r\n r: rows sent\uff08\u8fd4\u56de\u8bb0\u5f55\uff09\r\n t: query time\uff08\u67e5\u8be2\u65f6\u95f4\uff09\r\n -r reverse the sort order (largest last instead of first)\r\n -t NUM just show the top n queries\uff08\u8fd4\u56de\u524d\u9762n\u6761\u6570\u636e\uff09\r\n -a don't abstract all numbers to N and strings to 'S'\r\n -n NUM abstract numbers with at least n digits within names\r\n -g PATTERN grep: only consider stmts that include this string\uff08\u6b63\u5219\u5339\u914d\u6a21\u5f0f\uff0c\u5927\u5c0f\u5199\u4e0d\u654f\u611f\uff09\r\n -h HOSTNAME hostname of db server for *-slow.log filename (can be wildcard),\r\n default is '*', i.e. match all\r\n -i NAME name of server instance (if using mysql.server startup script)\r\n -l don't subtract lock time from total time\r\n \r\n<\/pre>\n\u6bd4\u5982\uff0c\u5f97\u5230\u8fd4\u56de\u8bb0\u5f55\u96c6\u6700\u591a\u768410\u4e2aSQL\u3002<\/p>\n
\r\nmysqldumpslow -s r -t 10 \/database\/mysql\/mysql06_slow.log\r\n<\/pre>\n\u5f97\u5230\u8bbf\u95ee\u6b21\u6570\u6700\u591a\u768410\u4e2aSQL<\/p>\n
\r\nmysqldumpslow -s c -t 10 \/database\/mysql\/mysql06_slow.log\r\n<\/pre>\n\r\n\u5f97\u5230\u6309\u7167\u65f6\u95f4\u6392\u5e8f\u7684\u524d10\u6761\u91cc\u9762\u542b\u6709\u5de6\u8fde\u63a5\u7684\u67e5\u8be2\u8bed\u53e5\u3002\r\n<\/pre>\n\r\nmysqldumpslow -s t -t 10 -g \u201cleft join\u201d \/database\/mysql\/mysql06_slow.log\r\n<\/pre>\n\u53e6\u5916\u5efa\u8bae\u5728\u4f7f\u7528\u8fd9\u4e9b\u547d\u4ee4\u65f6\u7ed3\u5408 | \u548cmore \u4f7f\u7528 \uff0c\u5426\u5219\u6709\u53ef\u80fd\u51fa\u73b0\u5237\u5c4f\u7684\u60c5\u51b5\u3002<\/p>\n
\r\nmysqldumpslow -s r -t 20 \/mysqldata\/mysql\/mysql06-slow.log | more\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"MySQL\u7684\u6162\u67e5\u8be2\uff0c\u5168\u540d\u662f\u6162\u67e5\u8be2\u65e5\u5fd7\uff0c\u662fMySQL\u63d0\u4f9b\u7684\u4e00\u79cd\u65e5\u5fd7\u8bb0\u5f55\uff0c\u7528\u6765\u8bb0\u5f55\u5728MySQL\u4e2d\u54cd\u5e94\u65f6\u95f4\u8d85\u8fc7\u9600\u503c\u7684 […]<\/p>\n","protected":false},"author":1903,"featured_media":97903,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-204245","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/204245","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1903"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=204245"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/204245\/revisions"}],"predecessor-version":[{"id":204286,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/204245\/revisions\/204286"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/97903"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=204245"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=204245"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=204245"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}